math.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  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 int
  925. freebsd_isnanf(float f)
  926. {
  927. if (is_little_endian()) {
  928. IEEEf2bits_L u;
  929. u.f = f;
  930. return (u.bits.exp == 0xff && u.bits.man != 0);
  931. }
  932. else {
  933. IEEEf2bits_B u;
  934. u.f = f;
  935. return (u.bits.exp == 0xff && u.bits.man != 0);
  936. }
  937. }
  938. static float
  939. freebsd_fabsf(float x)
  940. {
  941. u_int32_t ix;
  942. GET_FLOAT_WORD(ix, x);
  943. SET_FLOAT_WORD(x, ix & 0x7fffffff);
  944. return x;
  945. }
  946. static double
  947. freebsd_fabs(double x)
  948. {
  949. u_int32_t high;
  950. GET_HIGH_WORD(high, x);
  951. SET_HIGH_WORD(x, high & 0x7fffffff);
  952. return x;
  953. }
  954. static const float huge_f = 1.0e30F;
  955. static const float TWO23[2] = {
  956. 8.3886080000e+06, /* 0x4b000000 */
  957. -8.3886080000e+06, /* 0xcb000000 */
  958. };
  959. static float
  960. freebsd_truncf(float x)
  961. {
  962. int32_t i0, j0;
  963. u_int32_t i;
  964. GET_FLOAT_WORD(i0, x);
  965. j0 = ((i0 >> 23) & 0xff) - 0x7f;
  966. if (j0 < 23) {
  967. if (j0 < 0) { /* raise inexact if x != 0 */
  968. if (huge_f + x > 0.0F) /* |x|<1, so return 0*sign(x) */
  969. i0 &= 0x80000000;
  970. }
  971. else {
  972. i = (0x007fffff) >> j0;
  973. if ((i0 & i) == 0)
  974. return x; /* x is integral */
  975. if (huge_f + x > 0.0F) /* raise inexact flag */
  976. i0 &= (~i);
  977. }
  978. }
  979. else {
  980. if (j0 == 0x80)
  981. return x + x; /* inf or NaN */
  982. else
  983. return x; /* x is integral */
  984. }
  985. SET_FLOAT_WORD(x, i0);
  986. return x;
  987. }
  988. static float
  989. freebsd_rintf(float x)
  990. {
  991. int32_t i0, j0, sx;
  992. float w, t;
  993. GET_FLOAT_WORD(i0, x);
  994. sx = (i0 >> 31) & 1;
  995. j0 = ((i0 >> 23) & 0xff) - 0x7f;
  996. if (j0 < 23) {
  997. if (j0 < 0) {
  998. if ((i0 & 0x7fffffff) == 0)
  999. return x;
  1000. STRICT_ASSIGN(float, w, TWO23[sx] + x);
  1001. t = w - TWO23[sx];
  1002. GET_FLOAT_WORD(i0, t);
  1003. SET_FLOAT_WORD(t, (i0 & 0x7fffffff) | (sx << 31));
  1004. return t;
  1005. }
  1006. STRICT_ASSIGN(float, w, TWO23[sx] + x);
  1007. return w - TWO23[sx];
  1008. }
  1009. if (j0 == 0x80)
  1010. return x + x; /* inf or NaN */
  1011. else
  1012. return x; /* x is integral */
  1013. }
  1014. static float
  1015. freebsd_ceilf(float x)
  1016. {
  1017. int32_t i0, j0;
  1018. u_int32_t i;
  1019. GET_FLOAT_WORD(i0, x);
  1020. j0 = ((i0 >> 23) & 0xff) - 0x7f;
  1021. if (j0 < 23) {
  1022. if (j0 < 0) { /* raise inexact if x != 0 */
  1023. if (huge_f + x > (float)0.0) { /* return 0*sign(x) if |x|<1 */
  1024. if (i0 < 0) {
  1025. i0 = 0x80000000;
  1026. }
  1027. else if (i0 != 0) {
  1028. i0 = 0x3f800000;
  1029. }
  1030. }
  1031. }
  1032. else {
  1033. i = (0x007fffff) >> j0;
  1034. if ((i0 & i) == 0)
  1035. return x; /* x is integral */
  1036. if (huge_f + x > (float)0.0) { /* raise inexact flag */
  1037. if (i0 > 0)
  1038. i0 += (0x00800000) >> j0;
  1039. i0 &= (~i);
  1040. }
  1041. }
  1042. }
  1043. else {
  1044. if (j0 == 0x80)
  1045. return x + x; /* inf or NaN */
  1046. else
  1047. return x; /* x is integral */
  1048. }
  1049. SET_FLOAT_WORD(x, i0);
  1050. return x;
  1051. }
  1052. static float
  1053. freebsd_floorf(float x)
  1054. {
  1055. int32_t i0, j0;
  1056. u_int32_t i;
  1057. GET_FLOAT_WORD(i0, x);
  1058. j0 = ((i0 >> 23) & 0xff) - 0x7f;
  1059. if (j0 < 23) {
  1060. if (j0 < 0) { /* raise inexact if x != 0 */
  1061. if (huge_f + x > (float)0.0) { /* return 0*sign(x) if |x|<1 */
  1062. if (i0 >= 0) {
  1063. i0 = 0;
  1064. }
  1065. else if ((i0 & 0x7fffffff) != 0) {
  1066. i0 = 0xbf800000;
  1067. }
  1068. }
  1069. }
  1070. else {
  1071. i = (0x007fffff) >> j0;
  1072. if ((i0 & i) == 0)
  1073. return x; /* x is integral */
  1074. if (huge_f + x > (float)0.0) { /* raise inexact flag */
  1075. if (i0 < 0)
  1076. i0 += (0x00800000) >> j0;
  1077. i0 &= (~i);
  1078. }
  1079. }
  1080. }
  1081. else {
  1082. if (j0 == 0x80)
  1083. return x + x; /* inf or NaN */
  1084. else
  1085. return x; /* x is integral */
  1086. }
  1087. SET_FLOAT_WORD(x, i0);
  1088. return x;
  1089. }
  1090. static float
  1091. freebsd_fminf(float x, float y)
  1092. {
  1093. if (is_little_endian()) {
  1094. IEEEf2bits_L u[2] = { 0 };
  1095. u[0].f = x;
  1096. u[1].f = y;
  1097. /* Check for NaNs to avoid raising spurious exceptions. */
  1098. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  1099. return (y);
  1100. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  1101. return (x);
  1102. /* Handle comparisons of signed zeroes. */
  1103. if (u[0].bits.sign != u[1].bits.sign)
  1104. return (u[u[1].bits.sign].f);
  1105. }
  1106. else {
  1107. IEEEf2bits_B u[2] = { 0 };
  1108. u[0].f = x;
  1109. u[1].f = y;
  1110. /* Check for NaNs to avoid raising spurious exceptions. */
  1111. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  1112. return (y);
  1113. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  1114. return (x);
  1115. /* Handle comparisons of signed zeroes. */
  1116. if (u[0].bits.sign != u[1].bits.sign)
  1117. return (u[u[1].bits.sign].f);
  1118. }
  1119. return (x < y ? x : y);
  1120. }
  1121. static float
  1122. freebsd_fmaxf(float x, float y)
  1123. {
  1124. if (is_little_endian()) {
  1125. IEEEf2bits_L u[2] = { 0 };
  1126. u[0].f = x;
  1127. u[1].f = y;
  1128. /* Check for NaNs to avoid raising spurious exceptions. */
  1129. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  1130. return (y);
  1131. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  1132. return (x);
  1133. /* Handle comparisons of signed zeroes. */
  1134. if (u[0].bits.sign != u[1].bits.sign)
  1135. return (u[u[0].bits.sign].f);
  1136. }
  1137. else {
  1138. IEEEf2bits_B u[2] = { 0 };
  1139. u[0].f = x;
  1140. u[1].f = y;
  1141. /* Check for NaNs to avoid raising spurious exceptions. */
  1142. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  1143. return (y);
  1144. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  1145. return (x);
  1146. /* Handle comparisons of signed zeroes. */
  1147. if (u[0].bits.sign != u[1].bits.sign)
  1148. return (u[u[0].bits.sign].f);
  1149. }
  1150. return (x > y ? x : y);
  1151. }
  1152. static double
  1153. freebsd_copysign(double x, double y)
  1154. {
  1155. u_int32_t hx, hy;
  1156. GET_HIGH_WORD(hx, x);
  1157. GET_HIGH_WORD(hy, y);
  1158. SET_HIGH_WORD(x, (hx & 0x7fffffff) | (hy & 0x80000000));
  1159. return x;
  1160. }
  1161. static double
  1162. freebsd_scalbn(double x, int n)
  1163. {
  1164. int32_t k, hx, lx;
  1165. EXTRACT_WORDS(hx, lx, x);
  1166. k = (hx & 0x7ff00000) >> 20; /* extract exponent */
  1167. if (k == 0) { /* 0 or subnormal x */
  1168. if ((lx | (hx & 0x7fffffff)) == 0)
  1169. return x; /* +-0 */
  1170. x *= two54;
  1171. GET_HIGH_WORD(hx, x);
  1172. k = ((hx & 0x7ff00000) >> 20) - 54;
  1173. if (n < -50000)
  1174. return tiny * x; /*underflow*/
  1175. }
  1176. if (k == 0x7ff)
  1177. return x + x; /* NaN or Inf */
  1178. k = k + n;
  1179. if (k > 0x7fe)
  1180. return huge * freebsd_copysign(huge, x); /* overflow */
  1181. if (k > 0) /* normal result */
  1182. {
  1183. SET_HIGH_WORD(x, (hx & 0x800fffff) | (k << 20));
  1184. return x;
  1185. }
  1186. if (k <= -54) {
  1187. if (n > 50000) /* in case integer overflow in n+k */
  1188. return huge * freebsd_copysign(huge, x); /*overflow*/
  1189. else
  1190. return tiny * freebsd_copysign(tiny, x); /*underflow*/
  1191. }
  1192. k += 54; /* subnormal result */
  1193. SET_HIGH_WORD(x, (hx & 0x800fffff) | (k << 20));
  1194. return x * twom54;
  1195. }
  1196. static double
  1197. freebsd_pow(double x, double y)
  1198. {
  1199. double z, ax, z_h, z_l, p_h, p_l;
  1200. double y1, t1, t2, r, s, t, u, v, w;
  1201. int32_t i, j, k, yisint, n;
  1202. int32_t hx, hy, ix, iy;
  1203. u_int32_t lx, ly;
  1204. EXTRACT_WORDS(hx, lx, x);
  1205. EXTRACT_WORDS(hy, ly, y);
  1206. ix = hx & 0x7fffffff;
  1207. iy = hy & 0x7fffffff;
  1208. /* y==zero: x**0 = 1 */
  1209. if ((iy | ly) == 0)
  1210. return one;
  1211. /* x==1: 1**y = 1, even if y is NaN */
  1212. if (hx == 0x3ff00000 && lx == 0)
  1213. return one;
  1214. /* y!=zero: result is NaN if either arg is NaN */
  1215. if (ix > 0x7ff00000 || ((ix == 0x7ff00000) && (lx != 0)) || iy > 0x7ff00000
  1216. || ((iy == 0x7ff00000) && (ly != 0)))
  1217. return (x + 0.0) + (y + 0.0);
  1218. /* determine if y is an odd int when x < 0
  1219. * yisint = 0 ... y is not an integer
  1220. * yisint = 1 ... y is an odd int
  1221. * yisint = 2 ... y is an even int
  1222. */
  1223. yisint = 0;
  1224. if (hx < 0) {
  1225. if (iy >= 0x43400000)
  1226. yisint = 2; /* even integer y */
  1227. else if (iy >= 0x3ff00000) {
  1228. k = (iy >> 20) - 0x3ff; /* exponent */
  1229. if (k > 20) {
  1230. j = ly >> (52 - k);
  1231. if ((j << (52 - k)) == ly)
  1232. yisint = 2 - (j & 1);
  1233. }
  1234. else if (ly == 0) {
  1235. j = iy >> (20 - k);
  1236. if ((j << (20 - k)) == iy)
  1237. yisint = 2 - (j & 1);
  1238. }
  1239. }
  1240. }
  1241. /* special value of y */
  1242. if (ly == 0) {
  1243. if (iy == 0x7ff00000) { /* y is +-inf */
  1244. if (((ix - 0x3ff00000) | lx) == 0)
  1245. return one; /* (-1)**+-inf is NaN */
  1246. else if (ix >= 0x3ff00000) /* (|x|>1)**+-inf = inf,0 */
  1247. return (hy >= 0) ? y : zero;
  1248. else /* (|x|<1)**-,+inf = inf,0 */
  1249. return (hy < 0) ? -y : zero;
  1250. }
  1251. if (iy == 0x3ff00000) { /* y is +-1 */
  1252. if (hy < 0)
  1253. return one / x;
  1254. else
  1255. return x;
  1256. }
  1257. if (hy == 0x40000000)
  1258. return x * x; /* y is 2 */
  1259. if (hy == 0x40080000)
  1260. return x * x * x; /* y is 3 */
  1261. if (hy == 0x40100000) { /* y is 4 */
  1262. u = x * x;
  1263. return u * u;
  1264. }
  1265. if (hy == 0x3fe00000) { /* y is 0.5 */
  1266. if (hx >= 0) /* x >= +0 */
  1267. return sqrt(x);
  1268. }
  1269. }
  1270. ax = fabs(x);
  1271. /* special value of x */
  1272. if (lx == 0) {
  1273. if (ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000) {
  1274. z = ax; /*x is +-0,+-inf,+-1*/
  1275. if (hy < 0)
  1276. z = one / z; /* z = (1/|x|) */
  1277. if (hx < 0) {
  1278. if (((ix - 0x3ff00000) | yisint) == 0) {
  1279. z = (z - z) / (z - z); /* (-1)**non-int is NaN */
  1280. }
  1281. else if (yisint == 1)
  1282. z = -z; /* (x<0)**odd = -(|x|**odd) */
  1283. }
  1284. return z;
  1285. }
  1286. }
  1287. /* CYGNUS LOCAL + fdlibm-5.3 fix: This used to be
  1288. n = (hx>>31)+1;
  1289. but ANSI C says a right shift of a signed negative quantity is
  1290. implementation defined. */
  1291. n = ((u_int32_t)hx >> 31) - 1;
  1292. /* (x<0)**(non-int) is NaN */
  1293. if ((n | yisint) == 0)
  1294. return (x - x) / (x - x);
  1295. s = one; /* s (sign of result -ve**odd) = -1 else = 1 */
  1296. if ((n | (yisint - 1)) == 0)
  1297. s = -one; /* (-ve)**(odd int) */
  1298. /* |y| is huge */
  1299. if (iy > 0x41e00000) { /* if |y| > 2**31 */
  1300. if (iy > 0x43f00000) { /* if |y| > 2**64, must o/uflow */
  1301. if (ix <= 0x3fefffff)
  1302. return (hy < 0) ? huge * huge : tiny * tiny;
  1303. if (ix >= 0x3ff00000)
  1304. return (hy > 0) ? huge * huge : tiny * tiny;
  1305. }
  1306. /* over/underflow if x is not close to one */
  1307. if (ix < 0x3fefffff)
  1308. return (hy < 0) ? s * huge * huge : s * tiny * tiny;
  1309. if (ix > 0x3ff00000)
  1310. return (hy > 0) ? s * huge * huge : s * tiny * tiny;
  1311. /* now |1-x| is tiny <= 2**-20, suffice to compute
  1312. log(x) by x-x^2/2+x^3/3-x^4/4 */
  1313. t = ax - one; /* t has 20 trailing zeros */
  1314. w = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25));
  1315. u = ivln2_h * t; /* ivln2_h has 21 sig. bits */
  1316. v = t * ivln2_l - w * ivln2;
  1317. t1 = u + v;
  1318. SET_LOW_WORD(t1, 0);
  1319. t2 = v - (t1 - u);
  1320. }
  1321. else {
  1322. double ss, s2, s_h, s_l, t_h, t_l;
  1323. n = 0;
  1324. /* take care subnormal number */
  1325. if (ix < 0x00100000) {
  1326. ax *= two53;
  1327. n -= 53;
  1328. GET_HIGH_WORD(ix, ax);
  1329. }
  1330. n += ((ix) >> 20) - 0x3ff;
  1331. j = ix & 0x000fffff;
  1332. /* determine interval */
  1333. ix = j | 0x3ff00000; /* normalize ix */
  1334. if (j <= 0x3988E)
  1335. k = 0; /* |x|<sqrt(3/2) */
  1336. else if (j < 0xBB67A)
  1337. k = 1; /* |x|<sqrt(3) */
  1338. else {
  1339. k = 0;
  1340. n += 1;
  1341. ix -= 0x00100000;
  1342. }
  1343. SET_HIGH_WORD(ax, ix);
  1344. /* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */
  1345. u = ax - bp[k]; /* bp[0]=1.0, bp[1]=1.5 */
  1346. v = one / (ax + bp[k]);
  1347. ss = u * v;
  1348. s_h = ss;
  1349. SET_LOW_WORD(s_h, 0);
  1350. /* t_h=ax+bp[k] High */
  1351. t_h = zero;
  1352. SET_HIGH_WORD(t_h, ((ix >> 1) | 0x20000000) + 0x00080000 + (k << 18));
  1353. t_l = ax - (t_h - bp[k]);
  1354. s_l = v * ((u - s_h * t_h) - s_h * t_l);
  1355. /* compute log(ax) */
  1356. s2 = ss * ss;
  1357. r = s2 * s2
  1358. * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6)))));
  1359. r += s_l * (s_h + ss);
  1360. s2 = s_h * s_h;
  1361. t_h = 3.0 + s2 + r;
  1362. SET_LOW_WORD(t_h, 0);
  1363. t_l = r - ((t_h - 3.0) - s2);
  1364. /* u+v = ss*(1+...) */
  1365. u = s_h * t_h;
  1366. v = s_l * t_h + t_l * ss;
  1367. /* 2/(3log2)*(ss+...) */
  1368. p_h = u + v;
  1369. SET_LOW_WORD(p_h, 0);
  1370. p_l = v - (p_h - u);
  1371. z_h = cp_h * p_h; /* cp_h+cp_l = 2/(3*log2) */
  1372. z_l = cp_l * p_h + p_l * cp + dp_l[k];
  1373. /* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */
  1374. t = (double)n;
  1375. t1 = (((z_h + z_l) + dp_h[k]) + t);
  1376. SET_LOW_WORD(t1, 0);
  1377. t2 = z_l - (((t1 - t) - dp_h[k]) - z_h);
  1378. }
  1379. /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */
  1380. y1 = y;
  1381. SET_LOW_WORD(y1, 0);
  1382. p_l = (y - y1) * t1 + y * t2;
  1383. p_h = y1 * t1;
  1384. z = p_l + p_h;
  1385. EXTRACT_WORDS(j, i, z);
  1386. if (j >= 0x40900000) { /* z >= 1024 */
  1387. if (((j - 0x40900000) | i) != 0) /* if z > 1024 */
  1388. return s * huge * huge; /* overflow */
  1389. else {
  1390. if (p_l + ovt > z - p_h)
  1391. return s * huge * huge; /* overflow */
  1392. }
  1393. }
  1394. else if ((j & 0x7fffffff) >= 0x4090cc00) { /* z <= -1075 */
  1395. if (((j - 0xc090cc00) | i) != 0) /* z < -1075 */
  1396. return s * tiny * tiny; /* underflow */
  1397. else {
  1398. if (p_l <= z - p_h)
  1399. return s * tiny * tiny; /* underflow */
  1400. }
  1401. }
  1402. /*
  1403. * compute 2**(p_h+p_l)
  1404. */
  1405. i = j & 0x7fffffff;
  1406. k = (i >> 20) - 0x3ff;
  1407. n = 0;
  1408. if (i > 0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */
  1409. n = j + (0x00100000 >> (k + 1));
  1410. k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */
  1411. t = zero;
  1412. SET_HIGH_WORD(t, n & ~(0x000fffff >> k));
  1413. n = ((n & 0x000fffff) | 0x00100000) >> (20 - k);
  1414. if (j < 0)
  1415. n = -n;
  1416. p_h -= t;
  1417. }
  1418. t = p_l + p_h;
  1419. SET_LOW_WORD(t, 0);
  1420. u = t * lg2_h;
  1421. v = (p_l - (t - p_h)) * lg2 + t * lg2_l;
  1422. z = u + v;
  1423. w = v - (z - u);
  1424. t = z * z;
  1425. t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5))));
  1426. r = (z * t1) / (t1 - two) - (w + z * w);
  1427. z = one - (r - z);
  1428. GET_HIGH_WORD(j, z);
  1429. j += (n << 20);
  1430. if ((j >> 20) <= 0)
  1431. z = freebsd_scalbn(z, n); /* subnormal output */
  1432. else
  1433. SET_HIGH_WORD(z, j);
  1434. return s * z;
  1435. }
  1436. double
  1437. atan(double x)
  1438. {
  1439. return freebsd_atan(x);
  1440. }
  1441. double
  1442. atan2(double y, double x)
  1443. {
  1444. return freebsd_atan2(y, x);
  1445. }
  1446. #ifndef BH_HAS_SQRT
  1447. double
  1448. sqrt(double x)
  1449. {
  1450. return freebsd_sqrt(x);
  1451. }
  1452. #endif
  1453. double
  1454. floor(double x)
  1455. {
  1456. return freebsd_floor(x);
  1457. }
  1458. double
  1459. ceil(double x)
  1460. {
  1461. return freebsd_ceil(x);
  1462. }
  1463. double
  1464. fmin(double x, double y)
  1465. {
  1466. return x < y ? x : y;
  1467. }
  1468. double
  1469. fmax(double x, double y)
  1470. {
  1471. return x > y ? x : y;
  1472. }
  1473. double
  1474. rint(double x)
  1475. {
  1476. return freebsd_rint(x);
  1477. }
  1478. double
  1479. fabs(double x)
  1480. {
  1481. return freebsd_fabs(x);
  1482. }
  1483. int
  1484. isnan_float(float x)
  1485. {
  1486. return freebsd_isnanf(x);
  1487. }
  1488. int
  1489. isnan_double(double x)
  1490. {
  1491. return freebsd_isnan(x);
  1492. }
  1493. double
  1494. trunc(double x)
  1495. {
  1496. return (x > 0) ? freebsd_floor(x) : freebsd_ceil(x);
  1497. }
  1498. int
  1499. signbit_float(float x)
  1500. {
  1501. unsigned int i;
  1502. GET_FLOAT_WORD(i, x);
  1503. return (int)(i >> 31);
  1504. }
  1505. int
  1506. signbit_double(double x)
  1507. {
  1508. return ((__HI(x) & 0x80000000) >> 31);
  1509. }
  1510. float
  1511. fabsf(float x)
  1512. {
  1513. return freebsd_fabsf(x);
  1514. }
  1515. float
  1516. truncf(float x)
  1517. {
  1518. return freebsd_truncf(x);
  1519. }
  1520. float
  1521. rintf(float x)
  1522. {
  1523. return freebsd_rintf(x);
  1524. }
  1525. float
  1526. ceilf(float x)
  1527. {
  1528. return freebsd_ceilf(x);
  1529. }
  1530. float
  1531. floorf(float x)
  1532. {
  1533. return freebsd_floorf(x);
  1534. }
  1535. float
  1536. fminf(float x, float y)
  1537. {
  1538. return freebsd_fminf(x, y);
  1539. }
  1540. float
  1541. fmaxf(float x, float y)
  1542. {
  1543. return freebsd_fmaxf(x, y);
  1544. }
  1545. #ifndef BH_HAS_SQRTF
  1546. float
  1547. sqrtf(float x)
  1548. {
  1549. return freebsd_sqrtf(x);
  1550. }
  1551. #endif
  1552. double
  1553. pow(double x, double y)
  1554. {
  1555. return freebsd_pow(x, y);
  1556. }
  1557. double
  1558. scalbn(double x, int n)
  1559. {
  1560. return freebsd_scalbn(x, n);
  1561. }