math.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2004 David Schultz <das@FreeBSD.ORG>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. *
  28. * $FreeBSD$
  29. */
  30. #include "platform_common.h"
  31. #define __FDLIBM_STDC__
  32. #ifndef FLT_EVAL_METHOD
  33. #define FLT_EVAL_METHOD 0
  34. #endif
  35. typedef uint32_t u_int32_t;
  36. typedef uint64_t u_int64_t;
  37. typedef union u32double_tag {
  38. int *pint;
  39. double *pdouble;
  40. } U32DOUBLE;
  41. static inline int *
  42. pdouble2pint(double *pdouble)
  43. {
  44. U32DOUBLE u;
  45. u.pdouble = pdouble;
  46. return u.pint;
  47. }
  48. typedef union {
  49. double value;
  50. struct {
  51. u_int32_t lsw;
  52. u_int32_t msw;
  53. } parts;
  54. struct {
  55. u_int64_t w;
  56. } xparts;
  57. } ieee_double_shape_type_little;
  58. typedef union {
  59. double value;
  60. struct {
  61. u_int32_t msw;
  62. u_int32_t lsw;
  63. } parts;
  64. struct {
  65. u_int64_t w;
  66. } xparts;
  67. } ieee_double_shape_type_big;
  68. typedef union {
  69. double d;
  70. struct {
  71. unsigned int manl : 32;
  72. unsigned int manh : 20;
  73. unsigned int exp : 11;
  74. unsigned int sign : 1;
  75. } bits;
  76. } IEEEd2bits_L;
  77. typedef union {
  78. double d;
  79. struct {
  80. unsigned int sign : 1;
  81. unsigned int exp : 11;
  82. unsigned int manh : 20;
  83. unsigned int manl : 32;
  84. } bits;
  85. } IEEEd2bits_B;
  86. typedef union {
  87. float f;
  88. struct {
  89. unsigned int man : 23;
  90. unsigned int exp : 8;
  91. unsigned int sign : 1;
  92. } bits;
  93. } IEEEf2bits_L;
  94. typedef union {
  95. float f;
  96. struct {
  97. unsigned int sign : 1;
  98. unsigned int exp : 8;
  99. unsigned int man : 23;
  100. } bits;
  101. } IEEEf2bits_B;
  102. static union {
  103. int a;
  104. char b;
  105. } __ue = { .a = 1 };
  106. #define is_little_endian() (__ue.b == 1)
  107. #define __HIL(x) *(1 + pdouble2pint(&x))
  108. #define __LOL(x) *(pdouble2pint(&x))
  109. #define __HIB(x) *(pdouble2pint(&x))
  110. #define __LOB(x) *(1 + pdouble2pint(&x))
  111. /* Get two 32 bit ints from a double. */
  112. #define EXTRACT_WORDS_L(ix0, ix1, d) \
  113. do { \
  114. ieee_double_shape_type_little ew_u; \
  115. ew_u.value = (d); \
  116. (ix0) = ew_u.parts.msw; \
  117. (ix1) = ew_u.parts.lsw; \
  118. } while (0)
  119. /* Set a double from two 32 bit ints. */
  120. #define INSERT_WORDS_L(d, ix0, ix1) \
  121. do { \
  122. ieee_double_shape_type_little iw_u; \
  123. iw_u.parts.msw = (ix0); \
  124. iw_u.parts.lsw = (ix1); \
  125. (d) = iw_u.value; \
  126. } while (0)
  127. /* Get two 32 bit ints from a double. */
  128. #define EXTRACT_WORDS_B(ix0, ix1, d) \
  129. do { \
  130. ieee_double_shape_type_big ew_u; \
  131. ew_u.value = (d); \
  132. (ix0) = ew_u.parts.msw; \
  133. (ix1) = ew_u.parts.lsw; \
  134. } while (0)
  135. /* Set a double from two 32 bit ints. */
  136. #define INSERT_WORDS_B(d, ix0, ix1) \
  137. do { \
  138. ieee_double_shape_type_big iw_u; \
  139. iw_u.parts.msw = (ix0); \
  140. iw_u.parts.lsw = (ix1); \
  141. (d) = iw_u.value; \
  142. } while (0)
  143. /* Get the more significant 32 bit int from a double. */
  144. #define GET_HIGH_WORD_L(i, d) \
  145. do { \
  146. ieee_double_shape_type_little gh_u; \
  147. gh_u.value = (d); \
  148. (i) = gh_u.parts.msw; \
  149. } while (0)
  150. /* Get the more significant 32 bit int from a double. */
  151. #define GET_HIGH_WORD_B(i, d) \
  152. do { \
  153. ieee_double_shape_type_big gh_u; \
  154. gh_u.value = (d); \
  155. (i) = gh_u.parts.msw; \
  156. } while (0)
  157. /* Set the more significant 32 bits of a double from an int. */
  158. #define SET_HIGH_WORD_L(d, v) \
  159. do { \
  160. ieee_double_shape_type_little sh_u; \
  161. sh_u.value = (d); \
  162. sh_u.parts.msw = (v); \
  163. (d) = sh_u.value; \
  164. } while (0)
  165. /* Set the more significant 32 bits of a double from an int. */
  166. #define SET_HIGH_WORD_B(d, v) \
  167. do { \
  168. ieee_double_shape_type_big sh_u; \
  169. sh_u.value = (d); \
  170. sh_u.parts.msw = (v); \
  171. (d) = sh_u.value; \
  172. } while (0)
  173. /* Set the less significant 32 bits of a double from an int. */
  174. #define SET_LOW_WORD_L(d, v) \
  175. do { \
  176. ieee_double_shape_type_little sh_u; \
  177. sh_u.value = (d); \
  178. sh_u.parts.lsw = (v); \
  179. (d) = sh_u.value; \
  180. } while (0)
  181. /* Set the more significant 32 bits of a double from an int. */
  182. #define SET_LOW_WORD_B(d, v) \
  183. do { \
  184. ieee_double_shape_type_big sh_u; \
  185. sh_u.value = (d); \
  186. sh_u.parts.lsw = (v); \
  187. (d) = sh_u.value; \
  188. } while (0)
  189. /* Get the less significant 32 bit int from a double. */
  190. #define GET_LOW_WORD_L(i, d) \
  191. do { \
  192. ieee_double_shape_type_little gl_u; \
  193. gl_u.value = (d); \
  194. (i) = gl_u.parts.lsw; \
  195. } while (0)
  196. /* Get the less significant 32 bit int from a double. */
  197. #define GET_LOW_WORD_B(i, d) \
  198. do { \
  199. ieee_double_shape_type_big gl_u; \
  200. gl_u.value = (d); \
  201. (i) = gl_u.parts.lsw; \
  202. } while (0)
  203. /*
  204. * A union which permits us to convert between a float and a 32 bit
  205. * int.
  206. */
  207. typedef union {
  208. float value;
  209. /* FIXME: Assumes 32 bit int. */
  210. unsigned int word;
  211. } ieee_float_shape_type;
  212. /* Get a 32 bit int from a float. */
  213. #define GET_FLOAT_WORD(i, d) \
  214. do { \
  215. ieee_float_shape_type gf_u; \
  216. gf_u.value = (d); \
  217. (i) = gf_u.word; \
  218. } while (0)
  219. /* Set a float from a 32 bit int. */
  220. #define SET_FLOAT_WORD(d, i) \
  221. do { \
  222. ieee_float_shape_type sf_u; \
  223. sf_u.word = (i); \
  224. (d) = sf_u.value; \
  225. } while (0)
  226. /* Macro wrappers. */
  227. #define EXTRACT_WORDS(ix0, ix1, d) \
  228. do { \
  229. if (is_little_endian()) \
  230. EXTRACT_WORDS_L(ix0, ix1, d); \
  231. else \
  232. EXTRACT_WORDS_B(ix0, ix1, d); \
  233. } while (0)
  234. #define INSERT_WORDS(d, ix0, ix1) \
  235. do { \
  236. if (is_little_endian()) \
  237. INSERT_WORDS_L(d, ix0, ix1); \
  238. else \
  239. INSERT_WORDS_B(d, ix0, ix1); \
  240. } while (0)
  241. #define GET_HIGH_WORD(i, d) \
  242. do { \
  243. if (is_little_endian()) \
  244. GET_HIGH_WORD_L(i, d); \
  245. else \
  246. GET_HIGH_WORD_B(i, d); \
  247. } while (0)
  248. #define SET_HIGH_WORD(d, v) \
  249. do { \
  250. if (is_little_endian()) \
  251. SET_HIGH_WORD_L(d, v); \
  252. else \
  253. SET_HIGH_WORD_B(d, v); \
  254. } while (0)
  255. #define GET_LOW_WORD(d, v) \
  256. do { \
  257. if (is_little_endian()) \
  258. GET_LOW_WORD_L(d, v); \
  259. else \
  260. GET_LOW_WORD_B(d, v); \
  261. } while (0)
  262. #define SET_LOW_WORD(d, v) \
  263. do { \
  264. if (is_little_endian()) \
  265. SET_LOW_WORD_L(d, v); \
  266. else \
  267. SET_LOW_WORD_B(d, v); \
  268. } while (0)
  269. #define __HI(x) (is_little_endian() ? __HIL(x) : __HIB(x))
  270. #define __LO(x) (is_little_endian() ? __LOL(x) : __LOB(x))
  271. /*
  272. * Attempt to get strict C99 semantics for assignment with non-C99 compilers.
  273. */
  274. #if FLT_EVAL_METHOD == 0 || __GNUC__ == 0
  275. #define STRICT_ASSIGN(type, lval, rval) ((lval) = (rval))
  276. #else
  277. #define STRICT_ASSIGN(type, lval, rval) \
  278. do { \
  279. volatile type __lval; \
  280. \
  281. if (sizeof(type) >= sizeof(long double)) \
  282. (lval) = (rval); \
  283. else { \
  284. __lval = (rval); \
  285. (lval) = __lval; \
  286. } \
  287. } while (0)
  288. #endif
  289. #ifdef __FDLIBM_STDC__
  290. static const double huge = 1.0e300;
  291. #else
  292. static double huge = 1.0e300;
  293. #endif
  294. #ifdef __STDC__
  295. static const double
  296. #else
  297. static double
  298. #endif
  299. tiny = 1.0e-300;
  300. #ifdef __STDC__
  301. static const double
  302. #else
  303. static double
  304. #endif
  305. one = 1.00000000000000000000e+00; /* 0x3FF00000, 0x00000000 */
  306. #ifdef __STDC__
  307. static const double
  308. #else
  309. static double
  310. #endif
  311. TWO52[2] = {
  312. 4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */
  313. -4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */
  314. };
  315. #ifdef __STDC__
  316. static const double
  317. #else
  318. static double
  319. #endif
  320. atanhi[] = {
  321. 4.63647609000806093515e-01, /* atan(0.5)hi 0x3FDDAC67, 0x0561BB4F */
  322. 7.85398163397448278999e-01, /* atan(1.0)hi 0x3FE921FB, 0x54442D18 */
  323. 9.82793723247329054082e-01, /* atan(1.5)hi 0x3FEF730B, 0xD281F69B */
  324. 1.57079632679489655800e+00, /* atan(inf)hi 0x3FF921FB, 0x54442D18 */
  325. };
  326. #ifdef __STDC__
  327. static const double
  328. #else
  329. static double
  330. #endif
  331. atanlo[] = {
  332. 2.26987774529616870924e-17, /* atan(0.5)lo 0x3C7A2B7F, 0x222F65E2 */
  333. 3.06161699786838301793e-17, /* atan(1.0)lo 0x3C81A626, 0x33145C07 */
  334. 1.39033110312309984516e-17, /* atan(1.5)lo 0x3C700788, 0x7AF0CBBD */
  335. 6.12323399573676603587e-17, /* atan(inf)lo 0x3C91A626, 0x33145C07 */
  336. };
  337. #ifdef __STDC__
  338. static const double
  339. #else
  340. static double
  341. #endif
  342. aT[] = {
  343. 3.33333333333329318027e-01, /* 0x3FD55555, 0x5555550D */
  344. -1.99999999998764832476e-01, /* 0xBFC99999, 0x9998EBC4 */
  345. 1.42857142725034663711e-01, /* 0x3FC24924, 0x920083FF */
  346. -1.11111104054623557880e-01, /* 0xBFBC71C6, 0xFE231671 */
  347. 9.09088713343650656196e-02, /* 0x3FB745CD, 0xC54C206E */
  348. -7.69187620504482999495e-02, /* 0xBFB3B0F2, 0xAF749A6D */
  349. 6.66107313738753120669e-02, /* 0x3FB10D66, 0xA0D03D51 */
  350. -5.83357013379057348645e-02, /* 0xBFADDE2D, 0x52DEFD9A */
  351. 4.97687799461593236017e-02, /* 0x3FA97B4B, 0x24760DEB */
  352. -3.65315727442169155270e-02, /* 0xBFA2B444, 0x2C6A6C2F */
  353. 1.62858201153657823623e-02, /* 0x3F90AD3A, 0xE322DA11 */
  354. };
  355. #ifdef __STDC__
  356. static const double
  357. #else
  358. static double
  359. #endif
  360. zero = 0.0,
  361. pi_o_4 = 7.8539816339744827900E-01, /* 0x3FE921FB, 0x54442D18 */
  362. pi_o_2 = 1.5707963267948965580E+00, /* 0x3FF921FB, 0x54442D18 */
  363. pi = 3.1415926535897931160E+00, /* 0x400921FB, 0x54442D18 */
  364. pi_lo = 1.2246467991473531772E-16; /* 0x3CA1A626, 0x33145C07 */
  365. #ifdef __STDC__
  366. static const double
  367. #else
  368. static double
  369. #endif
  370. bp[] = {1.0, 1.5,},
  371. dp_h[] = { 0.0, 5.84962487220764160156e-01,}, /* 0x3FE2B803, 0x40000000 */
  372. dp_l[] = { 0.0, 1.35003920212974897128e-08,}, /* 0x3E4CFDEB, 0x43CFD006 */
  373. two = 2.0,
  374. two53 = 9007199254740992.0, /* 0x43400000, 0x00000000 */
  375. two54 = 1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
  376. twom54 = 5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
  377. /* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */
  378. L1 = 5.99999999999994648725e-01, /* 0x3FE33333, 0x33333303 */
  379. L2 = 4.28571428578550184252e-01, /* 0x3FDB6DB6, 0xDB6FABFF */
  380. L3 = 3.33333329818377432918e-01, /* 0x3FD55555, 0x518F264D */
  381. L4 = 2.72728123808534006489e-01, /* 0x3FD17460, 0xA91D4101 */
  382. L5 = 2.30660745775561754067e-01, /* 0x3FCD864A, 0x93C9DB65 */
  383. L6 = 2.06975017800338417784e-01, /* 0x3FCA7E28, 0x4A454EEF */
  384. P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */
  385. P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */
  386. P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */
  387. P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */
  388. P5 = 4.13813679705723846039e-08, /* 0x3E663769, 0x72BEA4D0 */
  389. lg2 = 6.93147180559945286227e-01, /* 0x3FE62E42, 0xFEFA39EF */
  390. lg2_h = 6.93147182464599609375e-01, /* 0x3FE62E43, 0x00000000 */
  391. lg2_l = -1.90465429995776804525e-09, /* 0xBE205C61, 0x0CA86C39 */
  392. ovt = 8.0085662595372944372e-0017, /* -(1024-log2(ovfl+.5ulp)) */
  393. cp = 9.61796693925975554329e-01, /* 0x3FEEC709, 0xDC3A03FD =2/(3ln2) */
  394. cp_h = 9.61796700954437255859e-01, /* 0x3FEEC709, 0xE0000000 =(float)cp */
  395. cp_l = -7.02846165095275826516e-09, /* 0xBE3E2FE0, 0x145B01F5 =tail of cp_h*/
  396. ivln2 = 1.44269504088896338700e+00, /* 0x3FF71547, 0x652B82FE =1/ln2 */
  397. ivln2_h = 1.44269502162933349609e+00, /* 0x3FF71547, 0x60000000 =24b 1/ln2*/
  398. ivln2_l = 1.92596299112661746887e-08; /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/
  399. static double
  400. freebsd_sqrt(double x);
  401. static double
  402. freebsd_floor(double x);
  403. static double
  404. freebsd_ceil(double x);
  405. static double
  406. freebsd_fabs(double x);
  407. static double
  408. freebsd_rint(double x);
  409. static int
  410. freebsd_isnan(double x);
  411. static double
  412. freebsd_atan(double x);
  413. static double
  414. freebsd_atan2(double y, double x);
  415. static double
  416. freebsd_atan(double x)
  417. {
  418. double w, s1, s2, z;
  419. int32_t ix, hx, id;
  420. GET_HIGH_WORD(hx, x);
  421. ix = hx & 0x7fffffff;
  422. if (ix >= 0x44100000) { /* if |x| >= 2^66 */
  423. u_int32_t low;
  424. GET_LOW_WORD(low, x);
  425. if (ix > 0x7ff00000 || (ix == 0x7ff00000 && (low != 0)))
  426. return x + x; /* NaN */
  427. if (hx > 0)
  428. return atanhi[3] + *(volatile double *)&atanlo[3];
  429. else
  430. return -atanhi[3] - *(volatile double *)&atanlo[3];
  431. }
  432. if (ix < 0x3fdc0000) { /* |x| < 0.4375 */
  433. if (ix < 0x3e400000) { /* |x| < 2^-27 */
  434. if (huge + x > one)
  435. return x; /* raise inexact */
  436. }
  437. id = -1;
  438. }
  439. else {
  440. x = freebsd_fabs(x);
  441. if (ix < 0x3ff30000) { /* |x| < 1.1875 */
  442. if (ix < 0x3fe60000) { /* 7/16 <=|x|<11/16 */
  443. id = 0;
  444. x = (2.0 * x - one) / (2.0 + x);
  445. }
  446. else { /* 11/16<=|x|< 19/16 */
  447. id = 1;
  448. x = (x - one) / (x + one);
  449. }
  450. }
  451. else {
  452. if (ix < 0x40038000) { /* |x| < 2.4375 */
  453. id = 2;
  454. x = (x - 1.5) / (one + 1.5 * x);
  455. }
  456. else { /* 2.4375 <= |x| < 2^66 */
  457. id = 3;
  458. x = -1.0 / x;
  459. }
  460. }
  461. }
  462. /* end of argument reduction */
  463. z = x * x;
  464. w = z * z;
  465. /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
  466. s1 = z
  467. * (aT[0]
  468. + w
  469. * (aT[2]
  470. + w * (aT[4] + w * (aT[6] + w * (aT[8] + w * aT[10])))));
  471. s2 = w * (aT[1] + w * (aT[3] + w * (aT[5] + w * (aT[7] + w * aT[9]))));
  472. if (id < 0)
  473. return x - x * (s1 + s2);
  474. else {
  475. z = atanhi[id] - ((x * (s1 + s2) - atanlo[id]) - x);
  476. return (hx < 0) ? -z : z;
  477. }
  478. }
  479. static double
  480. freebsd_atan2(double y, double x)
  481. {
  482. double z;
  483. int32_t k, m, hx, hy, ix, iy;
  484. u_int32_t lx, ly;
  485. EXTRACT_WORDS(hx, lx, x);
  486. ix = hx & 0x7fffffff;
  487. EXTRACT_WORDS(hy, ly, y);
  488. iy = hy & 0x7fffffff;
  489. if (((ix | ((lx | -lx) >> 31)) > 0x7ff00000)
  490. || ((iy | ((ly | -ly) >> 31)) > 0x7ff00000)) /* x or y is NaN */
  491. return x + y;
  492. if (hx == 0x3ff00000 && lx == 0)
  493. return freebsd_atan(y); /* x=1.0 */
  494. m = ((hy >> 31) & 1) | ((hx >> 30) & 2); /* 2*sign(x)+sign(y) */
  495. /* when y = 0 */
  496. if ((iy | ly) == 0) {
  497. switch (m) {
  498. case 0:
  499. case 1:
  500. return y; /* atan(+-0,+anything)=+-0 */
  501. case 2:
  502. return pi + tiny; /* atan(+0,-anything) = pi */
  503. case 3:
  504. default:
  505. return -pi - tiny; /* atan(-0,-anything) =-pi */
  506. }
  507. }
  508. /* when x = 0 */
  509. if ((ix | lx) == 0)
  510. return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny;
  511. /* when x is INF */
  512. if (ix == 0x7ff00000) {
  513. if (iy == 0x7ff00000) {
  514. switch (m) {
  515. case 0:
  516. return pi_o_4 + tiny; /* atan(+INF,+INF) */
  517. case 1:
  518. return -pi_o_4 - tiny; /* atan(-INF,+INF) */
  519. case 2:
  520. return 3.0 * pi_o_4 + tiny; /*atan(+INF,-INF)*/
  521. case 3:
  522. default:
  523. return -3.0 * pi_o_4 - tiny; /*atan(-INF,-INF)*/
  524. }
  525. }
  526. else {
  527. switch (m) {
  528. case 0:
  529. return zero; /* atan(+...,+INF) */
  530. case 1:
  531. return -zero; /* atan(-...,+INF) */
  532. case 2:
  533. return pi + tiny; /* atan(+...,-INF) */
  534. case 3:
  535. default:
  536. return -pi - tiny; /* atan(-...,-INF) */
  537. }
  538. }
  539. }
  540. /* when y is INF */
  541. if (iy == 0x7ff00000)
  542. return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny;
  543. /* compute y/x */
  544. k = (iy - ix) >> 20;
  545. if (k > 60) { /* |y/x| > 2**60 */
  546. z = pi_o_2 + 0.5 * pi_lo;
  547. m &= 1;
  548. }
  549. else if (hx < 0 && k < -60)
  550. z = 0.0; /* 0 > |y|/x > -2**-60 */
  551. else
  552. z = freebsd_atan(fabs(y / x)); /* safe to do y/x */
  553. switch (m) {
  554. case 0:
  555. return z; /* atan(+,+) */
  556. case 1:
  557. return -z; /* atan(-,+) */
  558. case 2:
  559. return pi - (z - pi_lo); /* atan(+,-) */
  560. default: /* case 3 */
  561. return (z - pi_lo) - pi; /* atan(-,-) */
  562. }
  563. }
  564. static double
  565. freebsd_sqrt(double x) /* wrapper sqrt */
  566. {
  567. double z;
  568. int32_t sign = (int)0x80000000;
  569. int32_t ix0, s0, q, m, t, i;
  570. u_int32_t r, t1, s1, ix1, q1;
  571. EXTRACT_WORDS(ix0, ix1, x);
  572. /* take care of Inf and NaN */
  573. if ((ix0 & 0x7ff00000) == 0x7ff00000) {
  574. return x * x + x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
  575. sqrt(-inf)=sNaN */
  576. }
  577. /* take care of zero */
  578. if (ix0 <= 0) {
  579. if (((ix0 & (~sign)) | ix1) == 0)
  580. return x; /* sqrt(+-0) = +-0 */
  581. else if (ix0 < 0)
  582. return (x - x) / (x - x); /* sqrt(-ve) = sNaN */
  583. }
  584. /* normalize x */
  585. m = (ix0 >> 20);
  586. if (m == 0) { /* subnormal x */
  587. while (ix0 == 0) {
  588. m -= 21;
  589. ix0 |= (ix1 >> 11);
  590. ix1 <<= 21;
  591. }
  592. for (i = 0; (ix0 & 0x00100000) == 0; i++)
  593. ix0 <<= 1;
  594. m -= i - 1;
  595. ix0 |= (ix1 >> (32 - i));
  596. ix1 <<= i;
  597. }
  598. m -= 1023; /* unbias exponent */
  599. ix0 = (ix0 & 0x000fffff) | 0x00100000;
  600. if (m & 1) { /* odd m, double x to make it even */
  601. ix0 += ix0 + ((ix1 & sign) >> 31);
  602. ix1 += ix1;
  603. }
  604. m >>= 1; /* m = [m/2] */
  605. /* generate sqrt(x) bit by bit */
  606. ix0 += ix0 + ((ix1 & sign) >> 31);
  607. ix1 += ix1;
  608. q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */
  609. r = 0x00200000; /* r = moving bit from right to left */
  610. while (r != 0) {
  611. t = s0 + r;
  612. if (t <= ix0) {
  613. s0 = t + r;
  614. ix0 -= t;
  615. q += r;
  616. }
  617. ix0 += ix0 + ((ix1 & sign) >> 31);
  618. ix1 += ix1;
  619. r >>= 1;
  620. }
  621. r = sign;
  622. while (r != 0) {
  623. t1 = s1 + r;
  624. t = s0;
  625. if ((t < ix0) || ((t == ix0) && (t1 <= ix1))) {
  626. s1 = t1 + r;
  627. if (((t1 & sign) == sign) && (s1 & sign) == 0)
  628. s0 += 1;
  629. ix0 -= t;
  630. if (ix1 < t1)
  631. ix0 -= 1;
  632. ix1 -= t1;
  633. q1 += r;
  634. }
  635. ix0 += ix0 + ((ix1 & sign) >> 31);
  636. ix1 += ix1;
  637. r >>= 1;
  638. }
  639. /* use floating add to find out rounding direction */
  640. if ((ix0 | ix1) != 0) {
  641. z = one - tiny; /* trigger inexact flag */
  642. if (z >= one) {
  643. z = one + tiny;
  644. if (q1 == (u_int32_t)0xffffffff) {
  645. q1 = 0;
  646. q += 1;
  647. }
  648. else if (z > one) {
  649. if (q1 == (u_int32_t)0xfffffffe)
  650. q += 1;
  651. q1 += 2;
  652. }
  653. else
  654. q1 += (q1 & 1);
  655. }
  656. }
  657. ix0 = (q >> 1) + 0x3fe00000;
  658. ix1 = q1 >> 1;
  659. if ((q & 1) == 1)
  660. ix1 |= sign;
  661. ix0 += (m << 20);
  662. INSERT_WORDS(z, ix0, ix1);
  663. return z;
  664. }
  665. static double
  666. freebsd_floor(double x)
  667. {
  668. int32_t i0, i1, j0;
  669. u_int32_t i, j;
  670. EXTRACT_WORDS(i0, i1, x);
  671. j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
  672. if (j0 < 20) {
  673. if (j0 < 0) { /* raise inexact if x != 0 */
  674. if (huge + x > 0.0) { /* return 0*sign(x) if |x|<1 */
  675. if (i0 >= 0) {
  676. i0 = i1 = 0;
  677. }
  678. else if (((i0 & 0x7fffffff) | i1) != 0) {
  679. i0 = 0xbff00000;
  680. i1 = 0;
  681. }
  682. }
  683. }
  684. else {
  685. i = (0x000fffff) >> j0;
  686. if (((i0 & i) | i1) == 0)
  687. return x; /* x is integral */
  688. if (huge + x > 0.0) { /* raise inexact flag */
  689. if (i0 < 0)
  690. i0 += (0x00100000) >> j0;
  691. i0 &= (~i);
  692. i1 = 0;
  693. }
  694. }
  695. }
  696. else if (j0 > 51) {
  697. if (j0 == 0x400)
  698. return x + x; /* inf or NaN */
  699. else
  700. return x; /* x is integral */
  701. }
  702. else {
  703. i = ((u_int32_t)(0xffffffff)) >> (j0 - 20);
  704. if ((i1 & i) == 0)
  705. return x; /* x is integral */
  706. if (huge + x > 0.0) { /* raise inexact flag */
  707. if (i0 < 0) {
  708. if (j0 == 20)
  709. i0 += 1;
  710. else {
  711. j = i1 + (1 << (52 - j0));
  712. if (j < i1)
  713. i0 += 1; /* got a carry */
  714. i1 = j;
  715. }
  716. }
  717. i1 &= (~i);
  718. }
  719. }
  720. INSERT_WORDS(x, i0, i1);
  721. return x;
  722. }
  723. static double
  724. freebsd_ceil(double x)
  725. {
  726. int32_t i0, i1, j0;
  727. u_int32_t i, j;
  728. EXTRACT_WORDS(i0, i1, x);
  729. j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
  730. if (j0 < 20) {
  731. if (j0 < 0) { /* raise inexact if x != 0 */
  732. if (huge + x > 0.0) { /* return 0*sign(x) if |x|<1 */
  733. if (i0 < 0) {
  734. i0 = 0x80000000;
  735. i1 = 0;
  736. }
  737. else if ((i0 | i1) != 0) {
  738. i0 = 0x3ff00000;
  739. i1 = 0;
  740. }
  741. }
  742. }
  743. else {
  744. i = (0x000fffff) >> j0;
  745. if (((i0 & i) | i1) == 0)
  746. return x; /* x is integral */
  747. if (huge + x > 0.0) { /* raise inexact flag */
  748. if (i0 > 0)
  749. i0 += (0x00100000) >> j0;
  750. i0 &= (~i);
  751. i1 = 0;
  752. }
  753. }
  754. }
  755. else if (j0 > 51) {
  756. if (j0 == 0x400)
  757. return x + x; /* inf or NaN */
  758. else
  759. return x; /* x is integral */
  760. }
  761. else {
  762. i = ((u_int32_t)(0xffffffff)) >> (j0 - 20);
  763. if ((i1 & i) == 0)
  764. return x; /* x is integral */
  765. if (huge + x > 0.0) { /* raise inexact flag */
  766. if (i0 > 0) {
  767. if (j0 == 20)
  768. i0 += 1;
  769. else {
  770. j = i1 + (1 << (52 - j0));
  771. if (j < i1)
  772. i0 += 1; /* got a carry */
  773. i1 = j;
  774. }
  775. }
  776. i1 &= (~i);
  777. }
  778. }
  779. INSERT_WORDS(x, i0, i1);
  780. return x;
  781. }
  782. static double
  783. freebsd_rint(double x)
  784. {
  785. int32_t i0, j0, sx;
  786. u_int32_t i, i1;
  787. double w, t;
  788. EXTRACT_WORDS(i0, i1, x);
  789. sx = (i0 >> 31) & 1;
  790. j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
  791. if (j0 < 20) {
  792. if (j0 < 0) {
  793. if (((i0 & 0x7fffffff) | i1) == 0)
  794. return x;
  795. i1 |= (i0 & 0x0fffff);
  796. i0 &= 0xfffe0000;
  797. i0 |= ((i1 | -i1) >> 12) & 0x80000;
  798. SET_HIGH_WORD(x, i0);
  799. STRICT_ASSIGN(double, w, TWO52[sx] + x);
  800. t = w - TWO52[sx];
  801. GET_HIGH_WORD(i0, t);
  802. SET_HIGH_WORD(t, (i0 & 0x7fffffff) | (sx << 31));
  803. return t;
  804. }
  805. else {
  806. i = (0x000fffff) >> j0;
  807. if (((i0 & i) | i1) == 0)
  808. return x; /* x is integral */
  809. i >>= 1;
  810. if (((i0 & i) | i1) != 0) {
  811. /*
  812. * Some bit is set after the 0.5 bit. To avoid the
  813. * possibility of errors from double rounding in
  814. * w = TWO52[sx]+x, adjust the 0.25 bit to a lower
  815. * guard bit. We do this for all j0<=51. The
  816. * adjustment is trickiest for j0==18 and j0==19
  817. * since then it spans the word boundary.
  818. */
  819. if (j0 == 19)
  820. i1 = 0x40000000;
  821. else if (j0 == 18)
  822. i1 = 0x80000000;
  823. else
  824. i0 = (i0 & (~i)) | ((0x20000) >> j0);
  825. }
  826. }
  827. }
  828. else if (j0 > 51) {
  829. if (j0 == 0x400)
  830. return x + x; /* inf or NaN */
  831. else
  832. return x; /* x is integral */
  833. }
  834. else {
  835. i = ((u_int32_t)(0xffffffff)) >> (j0 - 20);
  836. if ((i1 & i) == 0)
  837. return x; /* x is integral */
  838. i >>= 1;
  839. if ((i1 & i) != 0)
  840. i1 = (i1 & (~i)) | ((0x40000000) >> (j0 - 20));
  841. }
  842. INSERT_WORDS(x, i0, i1);
  843. STRICT_ASSIGN(double, w, TWO52[sx] + x);
  844. return w - TWO52[sx];
  845. }
  846. static int
  847. freebsd_isnan(double d)
  848. {
  849. if (is_little_endian()) {
  850. IEEEd2bits_L u;
  851. u.d = d;
  852. return (u.bits.exp == 2047 && (u.bits.manl != 0 || u.bits.manh != 0));
  853. }
  854. else {
  855. IEEEd2bits_B u;
  856. u.d = d;
  857. return (u.bits.exp == 2047 && (u.bits.manl != 0 || u.bits.manh != 0));
  858. }
  859. }
  860. static double
  861. freebsd_fabs(double x)
  862. {
  863. u_int32_t high;
  864. GET_HIGH_WORD(high, x);
  865. SET_HIGH_WORD(x, high & 0x7fffffff);
  866. return x;
  867. }
  868. static const float huge_f = 1.0e30F;
  869. static const float TWO23[2] = {
  870. 8.3886080000e+06, /* 0x4b000000 */
  871. -8.3886080000e+06, /* 0xcb000000 */
  872. };
  873. static float
  874. freebsd_truncf(float x)
  875. {
  876. int32_t i0, j0;
  877. u_int32_t i;
  878. GET_FLOAT_WORD(i0, x);
  879. j0 = ((i0 >> 23) & 0xff) - 0x7f;
  880. if (j0 < 23) {
  881. if (j0 < 0) { /* raise inexact if x != 0 */
  882. if (huge_f + x > 0.0F) /* |x|<1, so return 0*sign(x) */
  883. i0 &= 0x80000000;
  884. }
  885. else {
  886. i = (0x007fffff) >> j0;
  887. if ((i0 & i) == 0)
  888. return x; /* x is integral */
  889. if (huge_f + x > 0.0F) /* raise inexact flag */
  890. i0 &= (~i);
  891. }
  892. }
  893. else {
  894. if (j0 == 0x80)
  895. return x + x; /* inf or NaN */
  896. else
  897. return x; /* x is integral */
  898. }
  899. SET_FLOAT_WORD(x, i0);
  900. return x;
  901. }
  902. static float
  903. freebsd_rintf(float x)
  904. {
  905. int32_t i0, j0, sx;
  906. float w, t;
  907. GET_FLOAT_WORD(i0, x);
  908. sx = (i0 >> 31) & 1;
  909. j0 = ((i0 >> 23) & 0xff) - 0x7f;
  910. if (j0 < 23) {
  911. if (j0 < 0) {
  912. if ((i0 & 0x7fffffff) == 0)
  913. return x;
  914. STRICT_ASSIGN(float, w, TWO23[sx] + x);
  915. t = w - TWO23[sx];
  916. GET_FLOAT_WORD(i0, t);
  917. SET_FLOAT_WORD(t, (i0 & 0x7fffffff) | (sx << 31));
  918. return t;
  919. }
  920. STRICT_ASSIGN(float, w, TWO23[sx] + x);
  921. return w - TWO23[sx];
  922. }
  923. if (j0 == 0x80)
  924. return x + x; /* inf or NaN */
  925. else
  926. return x; /* x is integral */
  927. }
  928. static float
  929. freebsd_ceilf(float x)
  930. {
  931. int32_t i0, j0;
  932. u_int32_t i;
  933. GET_FLOAT_WORD(i0, x);
  934. j0 = ((i0 >> 23) & 0xff) - 0x7f;
  935. if (j0 < 23) {
  936. if (j0 < 0) { /* raise inexact if x != 0 */
  937. if (huge_f + x > (float)0.0) { /* return 0*sign(x) if |x|<1 */
  938. if (i0 < 0) {
  939. i0 = 0x80000000;
  940. }
  941. else if (i0 != 0) {
  942. i0 = 0x3f800000;
  943. }
  944. }
  945. }
  946. else {
  947. i = (0x007fffff) >> j0;
  948. if ((i0 & i) == 0)
  949. return x; /* x is integral */
  950. if (huge_f + x > (float)0.0) { /* raise inexact flag */
  951. if (i0 > 0)
  952. i0 += (0x00800000) >> j0;
  953. i0 &= (~i);
  954. }
  955. }
  956. }
  957. else {
  958. if (j0 == 0x80)
  959. return x + x; /* inf or NaN */
  960. else
  961. return x; /* x is integral */
  962. }
  963. SET_FLOAT_WORD(x, i0);
  964. return x;
  965. }
  966. static float
  967. freebsd_floorf(float x)
  968. {
  969. int32_t i0, j0;
  970. u_int32_t i;
  971. GET_FLOAT_WORD(i0, x);
  972. j0 = ((i0 >> 23) & 0xff) - 0x7f;
  973. if (j0 < 23) {
  974. if (j0 < 0) { /* raise inexact if x != 0 */
  975. if (huge_f + x > (float)0.0) { /* return 0*sign(x) if |x|<1 */
  976. if (i0 >= 0) {
  977. i0 = 0;
  978. }
  979. else if ((i0 & 0x7fffffff) != 0) {
  980. i0 = 0xbf800000;
  981. }
  982. }
  983. }
  984. else {
  985. i = (0x007fffff) >> j0;
  986. if ((i0 & i) == 0)
  987. return x; /* x is integral */
  988. if (huge_f + x > (float)0.0) { /* raise inexact flag */
  989. if (i0 < 0)
  990. i0 += (0x00800000) >> j0;
  991. i0 &= (~i);
  992. }
  993. }
  994. }
  995. else {
  996. if (j0 == 0x80)
  997. return x + x; /* inf or NaN */
  998. else
  999. return x; /* x is integral */
  1000. }
  1001. SET_FLOAT_WORD(x, i0);
  1002. return x;
  1003. }
  1004. static float
  1005. freebsd_fminf(float x, float y)
  1006. {
  1007. if (is_little_endian()) {
  1008. IEEEf2bits_L u[2];
  1009. u[0].f = x;
  1010. u[1].f = y;
  1011. /* Check for NaNs to avoid raising spurious exceptions. */
  1012. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  1013. return (y);
  1014. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  1015. return (x);
  1016. /* Handle comparisons of signed zeroes. */
  1017. if (u[0].bits.sign != u[1].bits.sign)
  1018. return (u[u[1].bits.sign].f);
  1019. }
  1020. else {
  1021. IEEEf2bits_B u[2];
  1022. u[0].f = x;
  1023. u[1].f = y;
  1024. /* Check for NaNs to avoid raising spurious exceptions. */
  1025. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  1026. return (y);
  1027. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  1028. return (x);
  1029. /* Handle comparisons of signed zeroes. */
  1030. if (u[0].bits.sign != u[1].bits.sign)
  1031. return (u[u[1].bits.sign].f);
  1032. }
  1033. return (x < y ? x : y);
  1034. }
  1035. static float
  1036. freebsd_fmaxf(float x, float y)
  1037. {
  1038. if (is_little_endian()) {
  1039. IEEEf2bits_L u[2];
  1040. u[0].f = x;
  1041. u[1].f = y;
  1042. /* Check for NaNs to avoid raising spurious exceptions. */
  1043. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  1044. return (y);
  1045. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  1046. return (x);
  1047. /* Handle comparisons of signed zeroes. */
  1048. if (u[0].bits.sign != u[1].bits.sign)
  1049. return (u[u[0].bits.sign].f);
  1050. }
  1051. else {
  1052. IEEEf2bits_B u[2];
  1053. u[0].f = x;
  1054. u[1].f = y;
  1055. /* Check for NaNs to avoid raising spurious exceptions. */
  1056. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  1057. return (y);
  1058. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  1059. return (x);
  1060. /* Handle comparisons of signed zeroes. */
  1061. if (u[0].bits.sign != u[1].bits.sign)
  1062. return (u[u[0].bits.sign].f);
  1063. }
  1064. return (x > y ? x : y);
  1065. }
  1066. static double
  1067. freebsd_copysign(double x, double y)
  1068. {
  1069. u_int32_t hx, hy;
  1070. GET_HIGH_WORD(hx, x);
  1071. GET_HIGH_WORD(hy, y);
  1072. SET_HIGH_WORD(x, (hx & 0x7fffffff) | (hy & 0x80000000));
  1073. return x;
  1074. }
  1075. static double
  1076. freebsd_scalbn(double x, int n)
  1077. {
  1078. int32_t k, hx, lx;
  1079. EXTRACT_WORDS(hx, lx, x);
  1080. k = (hx & 0x7ff00000) >> 20; /* extract exponent */
  1081. if (k == 0) { /* 0 or subnormal x */
  1082. if ((lx | (hx & 0x7fffffff)) == 0)
  1083. return x; /* +-0 */
  1084. x *= two54;
  1085. GET_HIGH_WORD(hx, x);
  1086. k = ((hx & 0x7ff00000) >> 20) - 54;
  1087. if (n < -50000)
  1088. return tiny * x; /*underflow*/
  1089. }
  1090. if (k == 0x7ff)
  1091. return x + x; /* NaN or Inf */
  1092. k = k + n;
  1093. if (k > 0x7fe)
  1094. return huge * freebsd_copysign(huge, x); /* overflow */
  1095. if (k > 0) /* normal result */
  1096. {
  1097. SET_HIGH_WORD(x, (hx & 0x800fffff) | (k << 20));
  1098. return x;
  1099. }
  1100. if (k <= -54) {
  1101. if (n > 50000) /* in case integer overflow in n+k */
  1102. return huge * freebsd_copysign(huge, x); /*overflow*/
  1103. else
  1104. return tiny * freebsd_copysign(tiny, x); /*underflow*/
  1105. }
  1106. k += 54; /* subnormal result */
  1107. SET_HIGH_WORD(x, (hx & 0x800fffff) | (k << 20));
  1108. return x * twom54;
  1109. }
  1110. static double
  1111. freebsd_pow(double x, double y)
  1112. {
  1113. double z, ax, z_h, z_l, p_h, p_l;
  1114. double y1, t1, t2, r, s, t, u, v, w;
  1115. int32_t i, j, k, yisint, n;
  1116. int32_t hx, hy, ix, iy;
  1117. u_int32_t lx, ly;
  1118. EXTRACT_WORDS(hx, lx, x);
  1119. EXTRACT_WORDS(hy, ly, y);
  1120. ix = hx & 0x7fffffff;
  1121. iy = hy & 0x7fffffff;
  1122. /* y==zero: x**0 = 1 */
  1123. if ((iy | ly) == 0)
  1124. return one;
  1125. /* x==1: 1**y = 1, even if y is NaN */
  1126. if (hx == 0x3ff00000 && lx == 0)
  1127. return one;
  1128. /* y!=zero: result is NaN if either arg is NaN */
  1129. if (ix > 0x7ff00000 || ((ix == 0x7ff00000) && (lx != 0)) || iy > 0x7ff00000
  1130. || ((iy == 0x7ff00000) && (ly != 0)))
  1131. return (x + 0.0) + (y + 0.0);
  1132. /* determine if y is an odd int when x < 0
  1133. * yisint = 0 ... y is not an integer
  1134. * yisint = 1 ... y is an odd int
  1135. * yisint = 2 ... y is an even int
  1136. */
  1137. yisint = 0;
  1138. if (hx < 0) {
  1139. if (iy >= 0x43400000)
  1140. yisint = 2; /* even integer y */
  1141. else if (iy >= 0x3ff00000) {
  1142. k = (iy >> 20) - 0x3ff; /* exponent */
  1143. if (k > 20) {
  1144. j = ly >> (52 - k);
  1145. if ((j << (52 - k)) == ly)
  1146. yisint = 2 - (j & 1);
  1147. }
  1148. else if (ly == 0) {
  1149. j = iy >> (20 - k);
  1150. if ((j << (20 - k)) == iy)
  1151. yisint = 2 - (j & 1);
  1152. }
  1153. }
  1154. }
  1155. /* special value of y */
  1156. if (ly == 0) {
  1157. if (iy == 0x7ff00000) { /* y is +-inf */
  1158. if (((ix - 0x3ff00000) | lx) == 0)
  1159. return one; /* (-1)**+-inf is NaN */
  1160. else if (ix >= 0x3ff00000) /* (|x|>1)**+-inf = inf,0 */
  1161. return (hy >= 0) ? y : zero;
  1162. else /* (|x|<1)**-,+inf = inf,0 */
  1163. return (hy < 0) ? -y : zero;
  1164. }
  1165. if (iy == 0x3ff00000) { /* y is +-1 */
  1166. if (hy < 0)
  1167. return one / x;
  1168. else
  1169. return x;
  1170. }
  1171. if (hy == 0x40000000)
  1172. return x * x; /* y is 2 */
  1173. if (hy == 0x40080000)
  1174. return x * x * x; /* y is 3 */
  1175. if (hy == 0x40100000) { /* y is 4 */
  1176. u = x * x;
  1177. return u * u;
  1178. }
  1179. if (hy == 0x3fe00000) { /* y is 0.5 */
  1180. if (hx >= 0) /* x >= +0 */
  1181. return sqrt(x);
  1182. }
  1183. }
  1184. ax = fabs(x);
  1185. /* special value of x */
  1186. if (lx == 0) {
  1187. if (ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000) {
  1188. z = ax; /*x is +-0,+-inf,+-1*/
  1189. if (hy < 0)
  1190. z = one / z; /* z = (1/|x|) */
  1191. if (hx < 0) {
  1192. if (((ix - 0x3ff00000) | yisint) == 0) {
  1193. z = (z - z) / (z - z); /* (-1)**non-int is NaN */
  1194. }
  1195. else if (yisint == 1)
  1196. z = -z; /* (x<0)**odd = -(|x|**odd) */
  1197. }
  1198. return z;
  1199. }
  1200. }
  1201. /* CYGNUS LOCAL + fdlibm-5.3 fix: This used to be
  1202. n = (hx>>31)+1;
  1203. but ANSI C says a right shift of a signed negative quantity is
  1204. implementation defined. */
  1205. n = ((u_int32_t)hx >> 31) - 1;
  1206. /* (x<0)**(non-int) is NaN */
  1207. if ((n | yisint) == 0)
  1208. return (x - x) / (x - x);
  1209. s = one; /* s (sign of result -ve**odd) = -1 else = 1 */
  1210. if ((n | (yisint - 1)) == 0)
  1211. s = -one; /* (-ve)**(odd int) */
  1212. /* |y| is huge */
  1213. if (iy > 0x41e00000) { /* if |y| > 2**31 */
  1214. if (iy > 0x43f00000) { /* if |y| > 2**64, must o/uflow */
  1215. if (ix <= 0x3fefffff)
  1216. return (hy < 0) ? huge * huge : tiny * tiny;
  1217. if (ix >= 0x3ff00000)
  1218. return (hy > 0) ? huge * huge : tiny * tiny;
  1219. }
  1220. /* over/underflow if x is not close to one */
  1221. if (ix < 0x3fefffff)
  1222. return (hy < 0) ? s * huge * huge : s * tiny * tiny;
  1223. if (ix > 0x3ff00000)
  1224. return (hy > 0) ? s * huge * huge : s * tiny * tiny;
  1225. /* now |1-x| is tiny <= 2**-20, suffice to compute
  1226. log(x) by x-x^2/2+x^3/3-x^4/4 */
  1227. t = ax - one; /* t has 20 trailing zeros */
  1228. w = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25));
  1229. u = ivln2_h * t; /* ivln2_h has 21 sig. bits */
  1230. v = t * ivln2_l - w * ivln2;
  1231. t1 = u + v;
  1232. SET_LOW_WORD(t1, 0);
  1233. t2 = v - (t1 - u);
  1234. }
  1235. else {
  1236. double ss, s2, s_h, s_l, t_h, t_l;
  1237. n = 0;
  1238. /* take care subnormal number */
  1239. if (ix < 0x00100000) {
  1240. ax *= two53;
  1241. n -= 53;
  1242. GET_HIGH_WORD(ix, ax);
  1243. }
  1244. n += ((ix) >> 20) - 0x3ff;
  1245. j = ix & 0x000fffff;
  1246. /* determine interval */
  1247. ix = j | 0x3ff00000; /* normalize ix */
  1248. if (j <= 0x3988E)
  1249. k = 0; /* |x|<sqrt(3/2) */
  1250. else if (j < 0xBB67A)
  1251. k = 1; /* |x|<sqrt(3) */
  1252. else {
  1253. k = 0;
  1254. n += 1;
  1255. ix -= 0x00100000;
  1256. }
  1257. SET_HIGH_WORD(ax, ix);
  1258. /* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
  1259. u = ax - bp[k]; /* bp[0]=1.0, bp[1]=1.5 */
  1260. v = one / (ax + bp[k]);
  1261. ss = u * v;
  1262. s_h = ss;
  1263. SET_LOW_WORD(s_h, 0);
  1264. /* t_h=ax+bp[k] High */
  1265. t_h = zero;
  1266. SET_HIGH_WORD(t_h, ((ix >> 1) | 0x20000000) + 0x00080000 + (k << 18));
  1267. t_l = ax - (t_h - bp[k]);
  1268. s_l = v * ((u - s_h * t_h) - s_h * t_l);
  1269. /* compute log(ax) */
  1270. s2 = ss * ss;
  1271. r = s2 * s2
  1272. * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
  1273. r += s_l * (s_h + ss);
  1274. s2 = s_h * s_h;
  1275. t_h = 3.0 + s2 + r;
  1276. SET_LOW_WORD(t_h, 0);
  1277. t_l = r - ((t_h - 3.0) - s2);
  1278. /* u+v = ss*(1+...) */
  1279. u = s_h * t_h;
  1280. v = s_l * t_h + t_l * ss;
  1281. /* 2/(3log2)*(ss+...) */
  1282. p_h = u + v;
  1283. SET_LOW_WORD(p_h, 0);
  1284. p_l = v - (p_h - u);
  1285. z_h = cp_h * p_h; /* cp_h+cp_l = 2/(3*log2) */
  1286. z_l = cp_l * p_h + p_l * cp + dp_l[k];
  1287. /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */
  1288. t = (double)n;
  1289. t1 = (((z_h + z_l) + dp_h[k]) + t);
  1290. SET_LOW_WORD(t1, 0);
  1291. t2 = z_l - (((t1 - t) - dp_h[k]) - z_h);
  1292. }
  1293. /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
  1294. y1 = y;
  1295. SET_LOW_WORD(y1, 0);
  1296. p_l = (y - y1) * t1 + y * t2;
  1297. p_h = y1 * t1;
  1298. z = p_l + p_h;
  1299. EXTRACT_WORDS(j, i, z);
  1300. if (j >= 0x40900000) { /* z >= 1024 */
  1301. if (((j - 0x40900000) | i) != 0) /* if z > 1024 */
  1302. return s * huge * huge; /* overflow */
  1303. else {
  1304. if (p_l + ovt > z - p_h)
  1305. return s * huge * huge; /* overflow */
  1306. }
  1307. }
  1308. else if ((j & 0x7fffffff) >= 0x4090cc00) { /* z <= -1075 */
  1309. if (((j - 0xc090cc00) | i) != 0) /* z < -1075 */
  1310. return s * tiny * tiny; /* underflow */
  1311. else {
  1312. if (p_l <= z - p_h)
  1313. return s * tiny * tiny; /* underflow */
  1314. }
  1315. }
  1316. /*
  1317. * compute 2**(p_h+p_l)
  1318. */
  1319. i = j & 0x7fffffff;
  1320. k = (i >> 20) - 0x3ff;
  1321. n = 0;
  1322. if (i > 0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */
  1323. n = j + (0x00100000 >> (k + 1));
  1324. k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */
  1325. t = zero;
  1326. SET_HIGH_WORD(t, n & ~(0x000fffff >> k));
  1327. n = ((n & 0x000fffff) | 0x00100000) >> (20 - k);
  1328. if (j < 0)
  1329. n = -n;
  1330. p_h -= t;
  1331. }
  1332. t = p_l + p_h;
  1333. SET_LOW_WORD(t, 0);
  1334. u = t * lg2_h;
  1335. v = (p_l - (t - p_h)) * lg2 + t * lg2_l;
  1336. z = u + v;
  1337. w = v - (z - u);
  1338. t = z * z;
  1339. t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
  1340. r = (z * t1) / (t1 - two) - (w + z * w);
  1341. z = one - (r - z);
  1342. GET_HIGH_WORD(j, z);
  1343. j += (n << 20);
  1344. if ((j >> 20) <= 0)
  1345. z = freebsd_scalbn(z, n); /* subnormal output */
  1346. else
  1347. SET_HIGH_WORD(z, j);
  1348. return s * z;
  1349. }
  1350. double
  1351. atan(double x)
  1352. {
  1353. return freebsd_atan(x);
  1354. }
  1355. double
  1356. atan2(double y, double x)
  1357. {
  1358. return freebsd_atan2(y, x);
  1359. }
  1360. double
  1361. sqrt(double x)
  1362. {
  1363. return freebsd_sqrt(x);
  1364. }
  1365. double
  1366. floor(double x)
  1367. {
  1368. return freebsd_floor(x);
  1369. }
  1370. double
  1371. ceil(double x)
  1372. {
  1373. return freebsd_ceil(x);
  1374. }
  1375. double
  1376. fmin(double x, double y)
  1377. {
  1378. return x < y ? x : y;
  1379. }
  1380. double
  1381. fmax(double x, double y)
  1382. {
  1383. return x > y ? x : y;
  1384. }
  1385. double
  1386. rint(double x)
  1387. {
  1388. return freebsd_rint(x);
  1389. }
  1390. double
  1391. fabs(double x)
  1392. {
  1393. return freebsd_fabs(x);
  1394. }
  1395. int
  1396. isnan(double x)
  1397. {
  1398. return freebsd_isnan(x);
  1399. }
  1400. double
  1401. trunc(double x)
  1402. {
  1403. return (x > 0) ? freebsd_floor(x) : freebsd_ceil(x);
  1404. }
  1405. int
  1406. signbit(double x)
  1407. {
  1408. return ((__HI(x) & 0x80000000) >> 31);
  1409. }
  1410. float
  1411. truncf(float x)
  1412. {
  1413. return freebsd_truncf(x);
  1414. }
  1415. float
  1416. rintf(float x)
  1417. {
  1418. return freebsd_rintf(x);
  1419. }
  1420. float
  1421. ceilf(float x)
  1422. {
  1423. return freebsd_ceilf(x);
  1424. }
  1425. float
  1426. floorf(float x)
  1427. {
  1428. return freebsd_floorf(x);
  1429. }
  1430. float
  1431. fminf(float x, float y)
  1432. {
  1433. return freebsd_fminf(x, y);
  1434. }
  1435. float
  1436. fmaxf(float x, float y)
  1437. {
  1438. return freebsd_fmaxf(x, y);
  1439. }
  1440. double
  1441. pow(double x, double y)
  1442. {
  1443. return freebsd_pow(x, y);
  1444. }
  1445. double
  1446. scalbn(double x, int n)
  1447. {
  1448. return freebsd_scalbn(x, n);
  1449. }