math.c 45 KB

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