math.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  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_floor(double x);
  401. static double
  402. freebsd_ceil(double x);
  403. static double
  404. freebsd_fabs(double x);
  405. static double
  406. freebsd_rint(double x);
  407. static int
  408. freebsd_isnan(double x);
  409. static double
  410. freebsd_atan(double x);
  411. static double
  412. freebsd_atan2(double y, double x);
  413. static double
  414. freebsd_atan(double x)
  415. {
  416. double w, s1, s2, z;
  417. int32_t ix, hx, id;
  418. GET_HIGH_WORD(hx, x);
  419. ix = hx & 0x7fffffff;
  420. if (ix >= 0x44100000) { /* if |x| >= 2^66 */
  421. u_int32_t low;
  422. GET_LOW_WORD(low, x);
  423. if (ix > 0x7ff00000 || (ix == 0x7ff00000 && (low != 0)))
  424. return x + x; /* NaN */
  425. if (hx > 0)
  426. return atanhi[3] + *(volatile double *)&atanlo[3];
  427. else
  428. return -atanhi[3] - *(volatile double *)&atanlo[3];
  429. }
  430. if (ix < 0x3fdc0000) { /* |x| < 0.4375 */
  431. if (ix < 0x3e400000) { /* |x| < 2^-27 */
  432. if (huge + x > one)
  433. return x; /* raise inexact */
  434. }
  435. id = -1;
  436. }
  437. else {
  438. x = freebsd_fabs(x);
  439. if (ix < 0x3ff30000) { /* |x| < 1.1875 */
  440. if (ix < 0x3fe60000) { /* 7/16 <=|x|<11/16 */
  441. id = 0;
  442. x = (2.0 * x - one) / (2.0 + x);
  443. }
  444. else { /* 11/16<=|x|< 19/16 */
  445. id = 1;
  446. x = (x - one) / (x + one);
  447. }
  448. }
  449. else {
  450. if (ix < 0x40038000) { /* |x| < 2.4375 */
  451. id = 2;
  452. x = (x - 1.5) / (one + 1.5 * x);
  453. }
  454. else { /* 2.4375 <= |x| < 2^66 */
  455. id = 3;
  456. x = -1.0 / x;
  457. }
  458. }
  459. }
  460. /* end of argument reduction */
  461. z = x * x;
  462. w = z * z;
  463. /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */
  464. s1 = z
  465. * (aT[0]
  466. + w
  467. * (aT[2]
  468. + w * (aT[4] + w * (aT[6] + w * (aT[8] + w * aT[10])))));
  469. s2 = w * (aT[1] + w * (aT[3] + w * (aT[5] + w * (aT[7] + w * aT[9]))));
  470. if (id < 0)
  471. return x - x * (s1 + s2);
  472. else {
  473. z = atanhi[id] - ((x * (s1 + s2) - atanlo[id]) - x);
  474. return (hx < 0) ? -z : z;
  475. }
  476. }
  477. static double
  478. freebsd_atan2(double y, double x)
  479. {
  480. double z;
  481. int32_t k, m, hx, hy, ix, iy;
  482. u_int32_t lx, ly;
  483. EXTRACT_WORDS(hx, lx, x);
  484. ix = hx & 0x7fffffff;
  485. EXTRACT_WORDS(hy, ly, y);
  486. iy = hy & 0x7fffffff;
  487. if (((ix | ((lx | -lx) >> 31)) > 0x7ff00000)
  488. || ((iy | ((ly | -ly) >> 31)) > 0x7ff00000)) /* x or y is NaN */
  489. return x + y;
  490. if (hx == 0x3ff00000 && lx == 0)
  491. return freebsd_atan(y); /* x=1.0 */
  492. m = ((hy >> 31) & 1) | ((hx >> 30) & 2); /* 2*sign(x)+sign(y) */
  493. /* when y = 0 */
  494. if ((iy | ly) == 0) {
  495. switch (m) {
  496. case 0:
  497. case 1:
  498. return y; /* atan(+-0,+anything)=+-0 */
  499. case 2:
  500. return pi + tiny; /* atan(+0,-anything) = pi */
  501. case 3:
  502. default:
  503. return -pi - tiny; /* atan(-0,-anything) =-pi */
  504. }
  505. }
  506. /* when x = 0 */
  507. if ((ix | lx) == 0)
  508. return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny;
  509. /* when x is INF */
  510. if (ix == 0x7ff00000) {
  511. if (iy == 0x7ff00000) {
  512. switch (m) {
  513. case 0:
  514. return pi_o_4 + tiny; /* atan(+INF,+INF) */
  515. case 1:
  516. return -pi_o_4 - tiny; /* atan(-INF,+INF) */
  517. case 2:
  518. return 3.0 * pi_o_4 + tiny; /*atan(+INF,-INF)*/
  519. case 3:
  520. default:
  521. return -3.0 * pi_o_4 - tiny; /*atan(-INF,-INF)*/
  522. }
  523. }
  524. else {
  525. switch (m) {
  526. case 0:
  527. return zero; /* atan(+...,+INF) */
  528. case 1:
  529. return -zero; /* atan(-...,+INF) */
  530. case 2:
  531. return pi + tiny; /* atan(+...,-INF) */
  532. case 3:
  533. default:
  534. return -pi - tiny; /* atan(-...,-INF) */
  535. }
  536. }
  537. }
  538. /* when y is INF */
  539. if (iy == 0x7ff00000)
  540. return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny;
  541. /* compute y/x */
  542. k = (iy - ix) >> 20;
  543. if (k > 60) { /* |y/x| > 2**60 */
  544. z = pi_o_2 + 0.5 * pi_lo;
  545. m &= 1;
  546. }
  547. else if (hx < 0 && k < -60)
  548. z = 0.0; /* 0 > |y|/x > -2**-60 */
  549. else
  550. z = freebsd_atan(fabs(y / x)); /* safe to do y/x */
  551. switch (m) {
  552. case 0:
  553. return z; /* atan(+,+) */
  554. case 1:
  555. return -z; /* atan(-,+) */
  556. case 2:
  557. return pi - (z - pi_lo); /* atan(+,-) */
  558. default: /* case 3 */
  559. return (z - pi_lo) - pi; /* atan(-,-) */
  560. }
  561. }
  562. #ifndef BH_HAS_SQRTF
  563. static float
  564. freebsd_sqrtf(float x)
  565. {
  566. float z;
  567. int32_t sign = (int)0x80000000;
  568. int32_t ix, s, q, m, t, i;
  569. u_int32_t r;
  570. GET_FLOAT_WORD(ix, x);
  571. /* take care of Inf and NaN */
  572. if ((ix & 0x7f800000) == 0x7f800000) {
  573. return x * x + x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
  574. sqrt(-inf)=sNaN */
  575. }
  576. /* take care of zero */
  577. if (ix <= 0) {
  578. if ((ix & (~sign)) == 0)
  579. return x; /* sqrt(+-0) = +-0 */
  580. else if (ix < 0)
  581. return (x - x) / (x - x); /* sqrt(-ve) = sNaN */
  582. }
  583. /* normalize x */
  584. m = (ix >> 23);
  585. if (m == 0) { /* subnormal x */
  586. for (i = 0; (ix & 0x00800000) == 0; i++)
  587. ix <<= 1;
  588. m -= i - 1;
  589. }
  590. m -= 127; /* unbias exponent */
  591. ix = (ix & 0x007fffff) | 0x00800000;
  592. if (m & 1) /* odd m, double x to make it even */
  593. ix += ix;
  594. m >>= 1; /* m = [m/2] */
  595. /* generate sqrt(x) bit by bit */
  596. ix += ix;
  597. q = s = 0; /* q = sqrt(x) */
  598. r = 0x01000000; /* r = moving bit from right to left */
  599. while (r != 0) {
  600. t = s + r;
  601. if (t <= ix) {
  602. s = t + r;
  603. ix -= t;
  604. q += r;
  605. }
  606. ix += ix;
  607. r >>= 1;
  608. }
  609. /* use floating add to find out rounding direction */
  610. if (ix != 0) {
  611. z = one - tiny; /* trigger inexact flag */
  612. if (z >= one) {
  613. z = one + tiny;
  614. if (z > one)
  615. q += 2;
  616. else
  617. q += (q & 1);
  618. }
  619. }
  620. ix = (q >> 1) + 0x3f000000;
  621. ix += (m << 23);
  622. SET_FLOAT_WORD(z, ix);
  623. return z;
  624. }
  625. #endif /* end of BH_HAS_SQRTF */
  626. #ifndef BH_HAS_SQRT
  627. static double
  628. freebsd_sqrt(double x) /* wrapper sqrt */
  629. {
  630. double z;
  631. int32_t sign = (int)0x80000000;
  632. int32_t ix0, s0, q, m, t, i;
  633. u_int32_t r, t1, s1, ix1, q1;
  634. EXTRACT_WORDS(ix0, ix1, x);
  635. /* take care of Inf and NaN */
  636. if ((ix0 & 0x7ff00000) == 0x7ff00000) {
  637. return x * x + x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
  638. sqrt(-inf)=sNaN */
  639. }
  640. /* take care of zero */
  641. if (ix0 <= 0) {
  642. if (((ix0 & (~sign)) | ix1) == 0)
  643. return x; /* sqrt(+-0) = +-0 */
  644. else if (ix0 < 0)
  645. return (x - x) / (x - x); /* sqrt(-ve) = sNaN */
  646. }
  647. /* normalize x */
  648. m = (ix0 >> 20);
  649. if (m == 0) { /* subnormal x */
  650. while (ix0 == 0) {
  651. m -= 21;
  652. ix0 |= (ix1 >> 11);
  653. ix1 <<= 21;
  654. }
  655. for (i = 0; (ix0 & 0x00100000) == 0; i++)
  656. ix0 <<= 1;
  657. m -= i - 1;
  658. ix0 |= (ix1 >> (32 - i));
  659. ix1 <<= i;
  660. }
  661. m -= 1023; /* unbias exponent */
  662. ix0 = (ix0 & 0x000fffff) | 0x00100000;
  663. if (m & 1) { /* odd m, double x to make it even */
  664. ix0 += ix0 + ((ix1 & sign) >> 31);
  665. ix1 += ix1;
  666. }
  667. m >>= 1; /* m = [m/2] */
  668. /* generate sqrt(x) bit by bit */
  669. ix0 += ix0 + ((ix1 & sign) >> 31);
  670. ix1 += ix1;
  671. q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */
  672. r = 0x00200000; /* r = moving bit from right to left */
  673. while (r != 0) {
  674. t = s0 + r;
  675. if (t <= ix0) {
  676. s0 = t + r;
  677. ix0 -= t;
  678. q += r;
  679. }
  680. ix0 += ix0 + ((ix1 & sign) >> 31);
  681. ix1 += ix1;
  682. r >>= 1;
  683. }
  684. r = sign;
  685. while (r != 0) {
  686. t1 = s1 + r;
  687. t = s0;
  688. if ((t < ix0) || ((t == ix0) && (t1 <= ix1))) {
  689. s1 = t1 + r;
  690. if (((t1 & sign) == sign) && (s1 & sign) == 0)
  691. s0 += 1;
  692. ix0 -= t;
  693. if (ix1 < t1)
  694. ix0 -= 1;
  695. ix1 -= t1;
  696. q1 += r;
  697. }
  698. ix0 += ix0 + ((ix1 & sign) >> 31);
  699. ix1 += ix1;
  700. r >>= 1;
  701. }
  702. /* use floating add to find out rounding direction */
  703. if ((ix0 | ix1) != 0) {
  704. z = one - tiny; /* trigger inexact flag */
  705. if (z >= one) {
  706. z = one + tiny;
  707. if (q1 == (u_int32_t)0xffffffff) {
  708. q1 = 0;
  709. q += 1;
  710. }
  711. else if (z > one) {
  712. if (q1 == (u_int32_t)0xfffffffe)
  713. q += 1;
  714. q1 += 2;
  715. }
  716. else
  717. q1 += (q1 & 1);
  718. }
  719. }
  720. ix0 = (q >> 1) + 0x3fe00000;
  721. ix1 = q1 >> 1;
  722. if ((q & 1) == 1)
  723. ix1 |= sign;
  724. ix0 += (m << 20);
  725. INSERT_WORDS(z, ix0, ix1);
  726. return z;
  727. }
  728. #endif /* end of BH_HAS_SQRT */
  729. static double
  730. freebsd_floor(double x)
  731. {
  732. int32_t i0, i1, j0;
  733. u_int32_t i, j;
  734. EXTRACT_WORDS(i0, i1, x);
  735. j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
  736. if (j0 < 20) {
  737. if (j0 < 0) { /* raise inexact if x != 0 */
  738. if (huge + x > 0.0) { /* return 0*sign(x) if |x|<1 */
  739. if (i0 >= 0) {
  740. i0 = i1 = 0;
  741. }
  742. else if (((i0 & 0x7fffffff) | i1) != 0) {
  743. i0 = 0xbff00000;
  744. i1 = 0;
  745. }
  746. }
  747. }
  748. else {
  749. i = (0x000fffff) >> j0;
  750. if (((i0 & i) | i1) == 0)
  751. return x; /* x is integral */
  752. if (huge + x > 0.0) { /* raise inexact flag */
  753. if (i0 < 0)
  754. i0 += (0x00100000) >> j0;
  755. i0 &= (~i);
  756. i1 = 0;
  757. }
  758. }
  759. }
  760. else if (j0 > 51) {
  761. if (j0 == 0x400)
  762. return x + x; /* inf or NaN */
  763. else
  764. return x; /* x is integral */
  765. }
  766. else {
  767. i = ((u_int32_t)(0xffffffff)) >> (j0 - 20);
  768. if ((i1 & i) == 0)
  769. return x; /* x is integral */
  770. if (huge + x > 0.0) { /* raise inexact flag */
  771. if (i0 < 0) {
  772. if (j0 == 20)
  773. i0 += 1;
  774. else {
  775. j = i1 + (1 << (52 - j0));
  776. if (j < i1)
  777. i0 += 1; /* got a carry */
  778. i1 = j;
  779. }
  780. }
  781. i1 &= (~i);
  782. }
  783. }
  784. INSERT_WORDS(x, i0, i1);
  785. return x;
  786. }
  787. static double
  788. freebsd_ceil(double x)
  789. {
  790. int32_t i0, i1, j0;
  791. u_int32_t i, j;
  792. EXTRACT_WORDS(i0, i1, x);
  793. j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
  794. if (j0 < 20) {
  795. if (j0 < 0) { /* raise inexact if x != 0 */
  796. if (huge + x > 0.0) { /* return 0*sign(x) if |x|<1 */
  797. if (i0 < 0) {
  798. i0 = 0x80000000;
  799. i1 = 0;
  800. }
  801. else if ((i0 | i1) != 0) {
  802. i0 = 0x3ff00000;
  803. i1 = 0;
  804. }
  805. }
  806. }
  807. else {
  808. i = (0x000fffff) >> j0;
  809. if (((i0 & i) | i1) == 0)
  810. return x; /* x is integral */
  811. if (huge + x > 0.0) { /* raise inexact flag */
  812. if (i0 > 0)
  813. i0 += (0x00100000) >> j0;
  814. i0 &= (~i);
  815. i1 = 0;
  816. }
  817. }
  818. }
  819. else if (j0 > 51) {
  820. if (j0 == 0x400)
  821. return x + x; /* inf or NaN */
  822. else
  823. return x; /* x is integral */
  824. }
  825. else {
  826. i = ((u_int32_t)(0xffffffff)) >> (j0 - 20);
  827. if ((i1 & i) == 0)
  828. return x; /* x is integral */
  829. if (huge + x > 0.0) { /* raise inexact flag */
  830. if (i0 > 0) {
  831. if (j0 == 20)
  832. i0 += 1;
  833. else {
  834. j = i1 + (1 << (52 - j0));
  835. if (j < i1)
  836. i0 += 1; /* got a carry */
  837. i1 = j;
  838. }
  839. }
  840. i1 &= (~i);
  841. }
  842. }
  843. INSERT_WORDS(x, i0, i1);
  844. return x;
  845. }
  846. static double
  847. freebsd_rint(double x)
  848. {
  849. int32_t i0, j0, sx;
  850. u_int32_t i, i1;
  851. double w, t;
  852. EXTRACT_WORDS(i0, i1, x);
  853. sx = (i0 >> 31) & 1;
  854. j0 = ((i0 >> 20) & 0x7ff) - 0x3ff;
  855. if (j0 < 20) {
  856. if (j0 < 0) {
  857. if (((i0 & 0x7fffffff) | i1) == 0)
  858. return x;
  859. i1 |= (i0 & 0x0fffff);
  860. i0 &= 0xfffe0000;
  861. i0 |= ((i1 | -i1) >> 12) & 0x80000;
  862. SET_HIGH_WORD(x, i0);
  863. STRICT_ASSIGN(double, w, TWO52[sx] + x);
  864. t = w - TWO52[sx];
  865. GET_HIGH_WORD(i0, t);
  866. SET_HIGH_WORD(t, (i0 & 0x7fffffff) | (sx << 31));
  867. return t;
  868. }
  869. else {
  870. i = (0x000fffff) >> j0;
  871. if (((i0 & i) | i1) == 0)
  872. return x; /* x is integral */
  873. i >>= 1;
  874. if (((i0 & i) | i1) != 0) {
  875. /*
  876. * Some bit is set after the 0.5 bit. To avoid the
  877. * possibility of errors from double rounding in
  878. * w = TWO52[sx]+x, adjust the 0.25 bit to a lower
  879. * guard bit. We do this for all j0<=51. The
  880. * adjustment is trickiest for j0==18 and j0==19
  881. * since then it spans the word boundary.
  882. */
  883. if (j0 == 19)
  884. i1 = 0x40000000;
  885. else if (j0 == 18)
  886. i1 = 0x80000000;
  887. else
  888. i0 = (i0 & (~i)) | ((0x20000) >> j0);
  889. }
  890. }
  891. }
  892. else if (j0 > 51) {
  893. if (j0 == 0x400)
  894. return x + x; /* inf or NaN */
  895. else
  896. return x; /* x is integral */
  897. }
  898. else {
  899. i = ((u_int32_t)(0xffffffff)) >> (j0 - 20);
  900. if ((i1 & i) == 0)
  901. return x; /* x is integral */
  902. i >>= 1;
  903. if ((i1 & i) != 0)
  904. i1 = (i1 & (~i)) | ((0x40000000) >> (j0 - 20));
  905. }
  906. INSERT_WORDS(x, i0, i1);
  907. STRICT_ASSIGN(double, w, TWO52[sx] + x);
  908. return w - TWO52[sx];
  909. }
  910. static int
  911. freebsd_isnan(double d)
  912. {
  913. if (is_little_endian()) {
  914. IEEEd2bits_L u;
  915. u.d = d;
  916. return (u.bits.exp == 2047 && (u.bits.manl != 0 || u.bits.manh != 0));
  917. }
  918. else {
  919. IEEEd2bits_B u;
  920. u.d = d;
  921. return (u.bits.exp == 2047 && (u.bits.manl != 0 || u.bits.manh != 0));
  922. }
  923. }
  924. static float
  925. freebsd_fabsf(float x)
  926. {
  927. u_int32_t ix;
  928. GET_FLOAT_WORD(ix, x);
  929. SET_FLOAT_WORD(x, ix & 0x7fffffff);
  930. return x;
  931. }
  932. static double
  933. freebsd_fabs(double x)
  934. {
  935. u_int32_t high;
  936. GET_HIGH_WORD(high, x);
  937. SET_HIGH_WORD(x, high & 0x7fffffff);
  938. return x;
  939. }
  940. static const float huge_f = 1.0e30F;
  941. static const float TWO23[2] = {
  942. 8.3886080000e+06, /* 0x4b000000 */
  943. -8.3886080000e+06, /* 0xcb000000 */
  944. };
  945. static float
  946. freebsd_truncf(float x)
  947. {
  948. int32_t i0, j0;
  949. u_int32_t i;
  950. GET_FLOAT_WORD(i0, x);
  951. j0 = ((i0 >> 23) & 0xff) - 0x7f;
  952. if (j0 < 23) {
  953. if (j0 < 0) { /* raise inexact if x != 0 */
  954. if (huge_f + x > 0.0F) /* |x|<1, so return 0*sign(x) */
  955. i0 &= 0x80000000;
  956. }
  957. else {
  958. i = (0x007fffff) >> j0;
  959. if ((i0 & i) == 0)
  960. return x; /* x is integral */
  961. if (huge_f + x > 0.0F) /* raise inexact flag */
  962. i0 &= (~i);
  963. }
  964. }
  965. else {
  966. if (j0 == 0x80)
  967. return x + x; /* inf or NaN */
  968. else
  969. return x; /* x is integral */
  970. }
  971. SET_FLOAT_WORD(x, i0);
  972. return x;
  973. }
  974. static float
  975. freebsd_rintf(float x)
  976. {
  977. int32_t i0, j0, sx;
  978. float w, t;
  979. GET_FLOAT_WORD(i0, x);
  980. sx = (i0 >> 31) & 1;
  981. j0 = ((i0 >> 23) & 0xff) - 0x7f;
  982. if (j0 < 23) {
  983. if (j0 < 0) {
  984. if ((i0 & 0x7fffffff) == 0)
  985. return x;
  986. STRICT_ASSIGN(float, w, TWO23[sx] + x);
  987. t = w - TWO23[sx];
  988. GET_FLOAT_WORD(i0, t);
  989. SET_FLOAT_WORD(t, (i0 & 0x7fffffff) | (sx << 31));
  990. return t;
  991. }
  992. STRICT_ASSIGN(float, w, TWO23[sx] + x);
  993. return w - TWO23[sx];
  994. }
  995. if (j0 == 0x80)
  996. return x + x; /* inf or NaN */
  997. else
  998. return x; /* x is integral */
  999. }
  1000. static float
  1001. freebsd_ceilf(float x)
  1002. {
  1003. int32_t i0, j0;
  1004. u_int32_t i;
  1005. GET_FLOAT_WORD(i0, x);
  1006. j0 = ((i0 >> 23) & 0xff) - 0x7f;
  1007. if (j0 < 23) {
  1008. if (j0 < 0) { /* raise inexact if x != 0 */
  1009. if (huge_f + x > (float)0.0) { /* return 0*sign(x) if |x|<1 */
  1010. if (i0 < 0) {
  1011. i0 = 0x80000000;
  1012. }
  1013. else if (i0 != 0) {
  1014. i0 = 0x3f800000;
  1015. }
  1016. }
  1017. }
  1018. else {
  1019. i = (0x007fffff) >> j0;
  1020. if ((i0 & i) == 0)
  1021. return x; /* x is integral */
  1022. if (huge_f + x > (float)0.0) { /* raise inexact flag */
  1023. if (i0 > 0)
  1024. i0 += (0x00800000) >> j0;
  1025. i0 &= (~i);
  1026. }
  1027. }
  1028. }
  1029. else {
  1030. if (j0 == 0x80)
  1031. return x + x; /* inf or NaN */
  1032. else
  1033. return x; /* x is integral */
  1034. }
  1035. SET_FLOAT_WORD(x, i0);
  1036. return x;
  1037. }
  1038. static float
  1039. freebsd_floorf(float x)
  1040. {
  1041. int32_t i0, j0;
  1042. u_int32_t i;
  1043. GET_FLOAT_WORD(i0, x);
  1044. j0 = ((i0 >> 23) & 0xff) - 0x7f;
  1045. if (j0 < 23) {
  1046. if (j0 < 0) { /* raise inexact if x != 0 */
  1047. if (huge_f + x > (float)0.0) { /* return 0*sign(x) if |x|<1 */
  1048. if (i0 >= 0) {
  1049. i0 = 0;
  1050. }
  1051. else if ((i0 & 0x7fffffff) != 0) {
  1052. i0 = 0xbf800000;
  1053. }
  1054. }
  1055. }
  1056. else {
  1057. i = (0x007fffff) >> j0;
  1058. if ((i0 & i) == 0)
  1059. return x; /* x is integral */
  1060. if (huge_f + x > (float)0.0) { /* raise inexact flag */
  1061. if (i0 < 0)
  1062. i0 += (0x00800000) >> j0;
  1063. i0 &= (~i);
  1064. }
  1065. }
  1066. }
  1067. else {
  1068. if (j0 == 0x80)
  1069. return x + x; /* inf or NaN */
  1070. else
  1071. return x; /* x is integral */
  1072. }
  1073. SET_FLOAT_WORD(x, i0);
  1074. return x;
  1075. }
  1076. static float
  1077. freebsd_fminf(float x, float y)
  1078. {
  1079. if (is_little_endian()) {
  1080. IEEEf2bits_L u[2] = { 0 };
  1081. u[0].f = x;
  1082. u[1].f = y;
  1083. /* Check for NaNs to avoid raising spurious exceptions. */
  1084. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  1085. return (y);
  1086. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  1087. return (x);
  1088. /* Handle comparisons of signed zeroes. */
  1089. if (u[0].bits.sign != u[1].bits.sign)
  1090. return (u[u[1].bits.sign].f);
  1091. }
  1092. else {
  1093. IEEEf2bits_B u[2] = { 0 };
  1094. u[0].f = x;
  1095. u[1].f = y;
  1096. /* Check for NaNs to avoid raising spurious exceptions. */
  1097. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  1098. return (y);
  1099. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  1100. return (x);
  1101. /* Handle comparisons of signed zeroes. */
  1102. if (u[0].bits.sign != u[1].bits.sign)
  1103. return (u[u[1].bits.sign].f);
  1104. }
  1105. return (x < y ? x : y);
  1106. }
  1107. static float
  1108. freebsd_fmaxf(float x, float y)
  1109. {
  1110. if (is_little_endian()) {
  1111. IEEEf2bits_L u[2] = { 0 };
  1112. u[0].f = x;
  1113. u[1].f = y;
  1114. /* Check for NaNs to avoid raising spurious exceptions. */
  1115. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  1116. return (y);
  1117. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  1118. return (x);
  1119. /* Handle comparisons of signed zeroes. */
  1120. if (u[0].bits.sign != u[1].bits.sign)
  1121. return (u[u[0].bits.sign].f);
  1122. }
  1123. else {
  1124. IEEEf2bits_B u[2] = { 0 };
  1125. u[0].f = x;
  1126. u[1].f = y;
  1127. /* Check for NaNs to avoid raising spurious exceptions. */
  1128. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  1129. return (y);
  1130. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  1131. return (x);
  1132. /* Handle comparisons of signed zeroes. */
  1133. if (u[0].bits.sign != u[1].bits.sign)
  1134. return (u[u[0].bits.sign].f);
  1135. }
  1136. return (x > y ? x : y);
  1137. }
  1138. static double
  1139. freebsd_copysign(double x, double y)
  1140. {
  1141. u_int32_t hx, hy;
  1142. GET_HIGH_WORD(hx, x);
  1143. GET_HIGH_WORD(hy, y);
  1144. SET_HIGH_WORD(x, (hx & 0x7fffffff) | (hy & 0x80000000));
  1145. return x;
  1146. }
  1147. static double
  1148. freebsd_scalbn(double x, int n)
  1149. {
  1150. int32_t k, hx, lx;
  1151. EXTRACT_WORDS(hx, lx, x);
  1152. k = (hx & 0x7ff00000) >> 20; /* extract exponent */
  1153. if (k == 0) { /* 0 or subnormal x */
  1154. if ((lx | (hx & 0x7fffffff)) == 0)
  1155. return x; /* +-0 */
  1156. x *= two54;
  1157. GET_HIGH_WORD(hx, x);
  1158. k = ((hx & 0x7ff00000) >> 20) - 54;
  1159. if (n < -50000)
  1160. return tiny * x; /*underflow*/
  1161. }
  1162. if (k == 0x7ff)
  1163. return x + x; /* NaN or Inf */
  1164. k = k + n;
  1165. if (k > 0x7fe)
  1166. return huge * freebsd_copysign(huge, x); /* overflow */
  1167. if (k > 0) /* normal result */
  1168. {
  1169. SET_HIGH_WORD(x, (hx & 0x800fffff) | (k << 20));
  1170. return x;
  1171. }
  1172. if (k <= -54) {
  1173. if (n > 50000) /* in case integer overflow in n+k */
  1174. return huge * freebsd_copysign(huge, x); /*overflow*/
  1175. else
  1176. return tiny * freebsd_copysign(tiny, x); /*underflow*/
  1177. }
  1178. k += 54; /* subnormal result */
  1179. SET_HIGH_WORD(x, (hx & 0x800fffff) | (k << 20));
  1180. return x * twom54;
  1181. }
  1182. static double
  1183. freebsd_pow(double x, double y)
  1184. {
  1185. double z, ax, z_h, z_l, p_h, p_l;
  1186. double y1, t1, t2, r, s, t, u, v, w;
  1187. int32_t i, j, k, yisint, n;
  1188. int32_t hx, hy, ix, iy;
  1189. u_int32_t lx, ly;
  1190. EXTRACT_WORDS(hx, lx, x);
  1191. EXTRACT_WORDS(hy, ly, y);
  1192. ix = hx & 0x7fffffff;
  1193. iy = hy & 0x7fffffff;
  1194. /* y==zero: x**0 = 1 */
  1195. if ((iy | ly) == 0)
  1196. return one;
  1197. /* x==1: 1**y = 1, even if y is NaN */
  1198. if (hx == 0x3ff00000 && lx == 0)
  1199. return one;
  1200. /* y!=zero: result is NaN if either arg is NaN */
  1201. if (ix > 0x7ff00000 || ((ix == 0x7ff00000) && (lx != 0)) || iy > 0x7ff00000
  1202. || ((iy == 0x7ff00000) && (ly != 0)))
  1203. return (x + 0.0) + (y + 0.0);
  1204. /* determine if y is an odd int when x < 0
  1205. * yisint = 0 ... y is not an integer
  1206. * yisint = 1 ... y is an odd int
  1207. * yisint = 2 ... y is an even int
  1208. */
  1209. yisint = 0;
  1210. if (hx < 0) {
  1211. if (iy >= 0x43400000)
  1212. yisint = 2; /* even integer y */
  1213. else if (iy >= 0x3ff00000) {
  1214. k = (iy >> 20) - 0x3ff; /* exponent */
  1215. if (k > 20) {
  1216. j = ly >> (52 - k);
  1217. if ((j << (52 - k)) == ly)
  1218. yisint = 2 - (j & 1);
  1219. }
  1220. else if (ly == 0) {
  1221. j = iy >> (20 - k);
  1222. if ((j << (20 - k)) == iy)
  1223. yisint = 2 - (j & 1);
  1224. }
  1225. }
  1226. }
  1227. /* special value of y */
  1228. if (ly == 0) {
  1229. if (iy == 0x7ff00000) { /* y is +-inf */
  1230. if (((ix - 0x3ff00000) | lx) == 0)
  1231. return one; /* (-1)**+-inf is NaN */
  1232. else if (ix >= 0x3ff00000) /* (|x|>1)**+-inf = inf,0 */
  1233. return (hy >= 0) ? y : zero;
  1234. else /* (|x|<1)**-,+inf = inf,0 */
  1235. return (hy < 0) ? -y : zero;
  1236. }
  1237. if (iy == 0x3ff00000) { /* y is +-1 */
  1238. if (hy < 0)
  1239. return one / x;
  1240. else
  1241. return x;
  1242. }
  1243. if (hy == 0x40000000)
  1244. return x * x; /* y is 2 */
  1245. if (hy == 0x40080000)
  1246. return x * x * x; /* y is 3 */
  1247. if (hy == 0x40100000) { /* y is 4 */
  1248. u = x * x;
  1249. return u * u;
  1250. }
  1251. if (hy == 0x3fe00000) { /* y is 0.5 */
  1252. if (hx >= 0) /* x >= +0 */
  1253. return sqrt(x);
  1254. }
  1255. }
  1256. ax = fabs(x);
  1257. /* special value of x */
  1258. if (lx == 0) {
  1259. if (ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000) {
  1260. z = ax; /*x is +-0,+-inf,+-1*/
  1261. if (hy < 0)
  1262. z = one / z; /* z = (1/|x|) */
  1263. if (hx < 0) {
  1264. if (((ix - 0x3ff00000) | yisint) == 0) {
  1265. z = (z - z) / (z - z); /* (-1)**non-int is NaN */
  1266. }
  1267. else if (yisint == 1)
  1268. z = -z; /* (x<0)**odd = -(|x|**odd) */
  1269. }
  1270. return z;
  1271. }
  1272. }
  1273. /* CYGNUS LOCAL + fdlibm-5.3 fix: This used to be
  1274. n = (hx>>31)+1;
  1275. but ANSI C says a right shift of a signed negative quantity is
  1276. implementation defined. */
  1277. n = ((u_int32_t)hx >> 31) - 1;
  1278. /* (x<0)**(non-int) is NaN */
  1279. if ((n | yisint) == 0)
  1280. return (x - x) / (x - x);
  1281. s = one; /* s (sign of result -ve**odd) = -1 else = 1 */
  1282. if ((n | (yisint - 1)) == 0)
  1283. s = -one; /* (-ve)**(odd int) */
  1284. /* |y| is huge */
  1285. if (iy > 0x41e00000) { /* if |y| > 2**31 */
  1286. if (iy > 0x43f00000) { /* if |y| > 2**64, must o/uflow */
  1287. if (ix <= 0x3fefffff)
  1288. return (hy < 0) ? huge * huge : tiny * tiny;
  1289. if (ix >= 0x3ff00000)
  1290. return (hy > 0) ? huge * huge : tiny * tiny;
  1291. }
  1292. /* over/underflow if x is not close to one */
  1293. if (ix < 0x3fefffff)
  1294. return (hy < 0) ? s * huge * huge : s * tiny * tiny;
  1295. if (ix > 0x3ff00000)
  1296. return (hy > 0) ? s * huge * huge : s * tiny * tiny;
  1297. /* now |1-x| is tiny <= 2**-20, suffice to compute
  1298. log(x) by x-x^2/2+x^3/3-x^4/4 */
  1299. t = ax - one; /* t has 20 trailing zeros */
  1300. w = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25));
  1301. u = ivln2_h * t; /* ivln2_h has 21 sig. bits */
  1302. v = t * ivln2_l - w * ivln2;
  1303. t1 = u + v;
  1304. SET_LOW_WORD(t1, 0);
  1305. t2 = v - (t1 - u);
  1306. }
  1307. else {
  1308. double ss, s2, s_h, s_l, t_h, t_l;
  1309. n = 0;
  1310. /* take care subnormal number */
  1311. if (ix < 0x00100000) {
  1312. ax *= two53;
  1313. n -= 53;
  1314. GET_HIGH_WORD(ix, ax);
  1315. }
  1316. n += ((ix) >> 20) - 0x3ff;
  1317. j = ix & 0x000fffff;
  1318. /* determine interval */
  1319. ix = j | 0x3ff00000; /* normalize ix */
  1320. if (j <= 0x3988E)
  1321. k = 0; /* |x|<sqrt(3/2) */
  1322. else if (j < 0xBB67A)
  1323. k = 1; /* |x|<sqrt(3) */
  1324. else {
  1325. k = 0;
  1326. n += 1;
  1327. ix -= 0x00100000;
  1328. }
  1329. SET_HIGH_WORD(ax, ix);
  1330. /* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
  1331. u = ax - bp[k]; /* bp[0]=1.0, bp[1]=1.5 */
  1332. v = one / (ax + bp[k]);
  1333. ss = u * v;
  1334. s_h = ss;
  1335. SET_LOW_WORD(s_h, 0);
  1336. /* t_h=ax+bp[k] High */
  1337. t_h = zero;
  1338. SET_HIGH_WORD(t_h, ((ix >> 1) | 0x20000000) + 0x00080000 + (k << 18));
  1339. t_l = ax - (t_h - bp[k]);
  1340. s_l = v * ((u - s_h * t_h) - s_h * t_l);
  1341. /* compute log(ax) */
  1342. s2 = ss * ss;
  1343. r = s2 * s2
  1344. * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
  1345. r += s_l * (s_h + ss);
  1346. s2 = s_h * s_h;
  1347. t_h = 3.0 + s2 + r;
  1348. SET_LOW_WORD(t_h, 0);
  1349. t_l = r - ((t_h - 3.0) - s2);
  1350. /* u+v = ss*(1+...) */
  1351. u = s_h * t_h;
  1352. v = s_l * t_h + t_l * ss;
  1353. /* 2/(3log2)*(ss+...) */
  1354. p_h = u + v;
  1355. SET_LOW_WORD(p_h, 0);
  1356. p_l = v - (p_h - u);
  1357. z_h = cp_h * p_h; /* cp_h+cp_l = 2/(3*log2) */
  1358. z_l = cp_l * p_h + p_l * cp + dp_l[k];
  1359. /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */
  1360. t = (double)n;
  1361. t1 = (((z_h + z_l) + dp_h[k]) + t);
  1362. SET_LOW_WORD(t1, 0);
  1363. t2 = z_l - (((t1 - t) - dp_h[k]) - z_h);
  1364. }
  1365. /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
  1366. y1 = y;
  1367. SET_LOW_WORD(y1, 0);
  1368. p_l = (y - y1) * t1 + y * t2;
  1369. p_h = y1 * t1;
  1370. z = p_l + p_h;
  1371. EXTRACT_WORDS(j, i, z);
  1372. if (j >= 0x40900000) { /* z >= 1024 */
  1373. if (((j - 0x40900000) | i) != 0) /* if z > 1024 */
  1374. return s * huge * huge; /* overflow */
  1375. else {
  1376. if (p_l + ovt > z - p_h)
  1377. return s * huge * huge; /* overflow */
  1378. }
  1379. }
  1380. else if ((j & 0x7fffffff) >= 0x4090cc00) { /* z <= -1075 */
  1381. if (((j - 0xc090cc00) | i) != 0) /* z < -1075 */
  1382. return s * tiny * tiny; /* underflow */
  1383. else {
  1384. if (p_l <= z - p_h)
  1385. return s * tiny * tiny; /* underflow */
  1386. }
  1387. }
  1388. /*
  1389. * compute 2**(p_h+p_l)
  1390. */
  1391. i = j & 0x7fffffff;
  1392. k = (i >> 20) - 0x3ff;
  1393. n = 0;
  1394. if (i > 0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */
  1395. n = j + (0x00100000 >> (k + 1));
  1396. k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */
  1397. t = zero;
  1398. SET_HIGH_WORD(t, n & ~(0x000fffff >> k));
  1399. n = ((n & 0x000fffff) | 0x00100000) >> (20 - k);
  1400. if (j < 0)
  1401. n = -n;
  1402. p_h -= t;
  1403. }
  1404. t = p_l + p_h;
  1405. SET_LOW_WORD(t, 0);
  1406. u = t * lg2_h;
  1407. v = (p_l - (t - p_h)) * lg2 + t * lg2_l;
  1408. z = u + v;
  1409. w = v - (z - u);
  1410. t = z * z;
  1411. t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
  1412. r = (z * t1) / (t1 - two) - (w + z * w);
  1413. z = one - (r - z);
  1414. GET_HIGH_WORD(j, z);
  1415. j += (n << 20);
  1416. if ((j >> 20) <= 0)
  1417. z = freebsd_scalbn(z, n); /* subnormal output */
  1418. else
  1419. SET_HIGH_WORD(z, j);
  1420. return s * z;
  1421. }
  1422. double
  1423. atan(double x)
  1424. {
  1425. return freebsd_atan(x);
  1426. }
  1427. double
  1428. atan2(double y, double x)
  1429. {
  1430. return freebsd_atan2(y, x);
  1431. }
  1432. #ifndef BH_HAS_SQRT
  1433. double
  1434. sqrt(double x)
  1435. {
  1436. return freebsd_sqrt(x);
  1437. }
  1438. #endif
  1439. double
  1440. floor(double x)
  1441. {
  1442. return freebsd_floor(x);
  1443. }
  1444. double
  1445. ceil(double x)
  1446. {
  1447. return freebsd_ceil(x);
  1448. }
  1449. double
  1450. fmin(double x, double y)
  1451. {
  1452. return x < y ? x : y;
  1453. }
  1454. double
  1455. fmax(double x, double y)
  1456. {
  1457. return x > y ? x : y;
  1458. }
  1459. double
  1460. rint(double x)
  1461. {
  1462. return freebsd_rint(x);
  1463. }
  1464. double
  1465. fabs(double x)
  1466. {
  1467. return freebsd_fabs(x);
  1468. }
  1469. int
  1470. isnan(double x)
  1471. {
  1472. return freebsd_isnan(x);
  1473. }
  1474. double
  1475. trunc(double x)
  1476. {
  1477. return (x > 0) ? freebsd_floor(x) : freebsd_ceil(x);
  1478. }
  1479. int
  1480. signbit(double x)
  1481. {
  1482. return ((__HI(x) & 0x80000000) >> 31);
  1483. }
  1484. float
  1485. fabsf(float x)
  1486. {
  1487. return freebsd_fabsf(x);
  1488. }
  1489. float
  1490. truncf(float x)
  1491. {
  1492. return freebsd_truncf(x);
  1493. }
  1494. float
  1495. rintf(float x)
  1496. {
  1497. return freebsd_rintf(x);
  1498. }
  1499. float
  1500. ceilf(float x)
  1501. {
  1502. return freebsd_ceilf(x);
  1503. }
  1504. float
  1505. floorf(float x)
  1506. {
  1507. return freebsd_floorf(x);
  1508. }
  1509. float
  1510. fminf(float x, float y)
  1511. {
  1512. return freebsd_fminf(x, y);
  1513. }
  1514. float
  1515. fmaxf(float x, float y)
  1516. {
  1517. return freebsd_fmaxf(x, y);
  1518. }
  1519. #ifndef BH_HAS_SQRTF
  1520. float
  1521. sqrtf(float x)
  1522. {
  1523. return freebsd_sqrtf(x);
  1524. }
  1525. #endif
  1526. double
  1527. pow(double x, double y)
  1528. {
  1529. return freebsd_pow(x, y);
  1530. }
  1531. double
  1532. scalbn(double x, int n)
  1533. {
  1534. return freebsd_scalbn(x, n);
  1535. }