math.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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. typedef uint32_t u_int32_t;
  33. typedef uint64_t u_int64_t;
  34. typedef union u32double_tag {
  35. int *pint;
  36. double *pdouble;
  37. } U32DOUBLE;
  38. static inline int *
  39. pdouble2pint(double *pdouble)
  40. {
  41. U32DOUBLE u;
  42. u.pdouble = pdouble;
  43. return u.pint;
  44. }
  45. typedef union
  46. {
  47. double value;
  48. struct
  49. {
  50. u_int32_t lsw;
  51. u_int32_t msw;
  52. } parts;
  53. struct
  54. {
  55. u_int64_t w;
  56. } xparts;
  57. } ieee_double_shape_type_little;
  58. typedef union
  59. {
  60. double value;
  61. struct
  62. {
  63. u_int32_t msw;
  64. u_int32_t lsw;
  65. } parts;
  66. struct
  67. {
  68. u_int64_t w;
  69. } xparts;
  70. } ieee_double_shape_type_big;
  71. typedef union {
  72. double d;
  73. struct {
  74. unsigned int manl :32;
  75. unsigned int manh :20;
  76. unsigned int exp :11;
  77. unsigned int sign :1;
  78. } bits;
  79. } IEEEd2bits_L;
  80. typedef union {
  81. double d;
  82. struct {
  83. unsigned int sign :1;
  84. unsigned int exp :11;
  85. unsigned int manh :20;
  86. unsigned int manl :32;
  87. } bits;
  88. } IEEEd2bits_B;
  89. typedef union {
  90. float f;
  91. struct {
  92. unsigned int man :23;
  93. unsigned int exp :8;
  94. unsigned int sign :1;
  95. } bits;
  96. } IEEEf2bits_L;
  97. typedef union {
  98. float f;
  99. struct {
  100. unsigned int sign :1;
  101. unsigned int exp :8;
  102. unsigned int man :23;
  103. } bits;
  104. } IEEEf2bits_B;
  105. static union {
  106. int a;
  107. char b;
  108. } __ue = { .a = 1 };
  109. #define is_little_endian() (__ue.b == 1)
  110. #define __HIL(x) *(1+pdouble2pint(&x))
  111. #define __LOL(x) *(pdouble2pint(&x))
  112. #define __HIB(x) *(pdouble2pint(&x))
  113. #define __LOB(x) *(1+pdouble2pint(&x))
  114. /* Get two 32 bit ints from a double. */
  115. #define EXTRACT_WORDS_L(ix0,ix1,d) \
  116. do { \
  117. ieee_double_shape_type_little ew_u; \
  118. ew_u.value = (d); \
  119. (ix0) = ew_u.parts.msw; \
  120. (ix1) = ew_u.parts.lsw; \
  121. } while (0)
  122. /* Set a double from two 32 bit ints. */
  123. #define INSERT_WORDS_L(d,ix0,ix1) \
  124. do { \
  125. ieee_double_shape_type_little iw_u; \
  126. iw_u.parts.msw = (ix0); \
  127. iw_u.parts.lsw = (ix1); \
  128. (d) = iw_u.value; \
  129. } while (0)
  130. /* Get two 32 bit ints from a double. */
  131. #define EXTRACT_WORDS_B(ix0,ix1,d) \
  132. do { \
  133. ieee_double_shape_type_big ew_u; \
  134. ew_u.value = (d); \
  135. (ix0) = ew_u.parts.msw; \
  136. (ix1) = ew_u.parts.lsw; \
  137. } while (0)
  138. /* Set a double from two 32 bit ints. */
  139. #define INSERT_WORDS_B(d,ix0,ix1) \
  140. do { \
  141. ieee_double_shape_type_big iw_u; \
  142. iw_u.parts.msw = (ix0); \
  143. iw_u.parts.lsw = (ix1); \
  144. (d) = iw_u.value; \
  145. } while (0)
  146. /* Get the more significant 32 bit int from a double. */
  147. #define GET_HIGH_WORD_L(i,d) \
  148. do { \
  149. ieee_double_shape_type_little gh_u; \
  150. gh_u.value = (d); \
  151. (i) = gh_u.parts.msw; \
  152. } while (0)
  153. /* Get the more significant 32 bit int from a double. */
  154. #define GET_HIGH_WORD_B(i,d) \
  155. do { \
  156. ieee_double_shape_type_big gh_u; \
  157. gh_u.value = (d); \
  158. (i) = gh_u.parts.msw; \
  159. } while (0)
  160. /* Set the more significant 32 bits of a double from an int. */
  161. #define SET_HIGH_WORD_L(d,v) \
  162. do { \
  163. ieee_double_shape_type_little sh_u; \
  164. sh_u.value = (d); \
  165. sh_u.parts.msw = (v); \
  166. (d) = sh_u.value; \
  167. } while (0)
  168. /* Set the more significant 32 bits of a double from an int. */
  169. #define SET_HIGH_WORD_B(d,v) \
  170. do { \
  171. ieee_double_shape_type_big sh_u; \
  172. sh_u.value = (d); \
  173. sh_u.parts.msw = (v); \
  174. (d) = sh_u.value; \
  175. } while (0)
  176. /*
  177. * A union which permits us to convert between a float and a 32 bit
  178. * int.
  179. */
  180. typedef union
  181. {
  182. float value;
  183. /* FIXME: Assumes 32 bit int. */
  184. unsigned int word;
  185. } ieee_float_shape_type;
  186. /* Get a 32 bit int from a float. */
  187. #define GET_FLOAT_WORD(i,d) \
  188. do { \
  189. ieee_float_shape_type gf_u; \
  190. gf_u.value = (d); \
  191. (i) = gf_u.word; \
  192. } while (0)
  193. /* Set a float from a 32 bit int. */
  194. #define SET_FLOAT_WORD(d,i) \
  195. do { \
  196. ieee_float_shape_type sf_u; \
  197. sf_u.word = (i); \
  198. (d) = sf_u.value; \
  199. } while (0)
  200. /* Macro wrappers. */
  201. #define EXTRACT_WORDS(ix0,ix1,d) do { \
  202. if (is_little_endian()) \
  203. EXTRACT_WORDS_L(ix0,ix1,d); \
  204. else \
  205. EXTRACT_WORDS_B(ix0,ix1,d); \
  206. } while (0)
  207. #define INSERT_WORDS(d,ix0,ix1) do { \
  208. if (is_little_endian()) \
  209. INSERT_WORDS_L(d,ix0,ix1); \
  210. else \
  211. INSERT_WORDS_B(d,ix0,ix1); \
  212. } while (0)
  213. #define GET_HIGH_WORD(i,d) \
  214. do { \
  215. if (is_little_endian()) \
  216. GET_HIGH_WORD_L(i,d); \
  217. else \
  218. GET_HIGH_WORD_B(i,d); \
  219. } while (0)
  220. #define SET_HIGH_WORD(d,v) \
  221. do { \
  222. if (is_little_endian()) \
  223. SET_HIGH_WORD_L(d,v); \
  224. else \
  225. SET_HIGH_WORD_B(d,v); \
  226. } while (0)
  227. #define __HI(x) (is_little_endian() ? __HIL(x) : __HIB(x))
  228. #define __LO(x) (is_little_endian() ? __LOL(x) : __LOB(x))
  229. /*
  230. * Attempt to get strict C99 semantics for assignment with non-C99 compilers.
  231. */
  232. #if FLT_EVAL_METHOD == 0 || __GNUC__ == 0
  233. #define STRICT_ASSIGN(type, lval, rval) ((lval) = (rval))
  234. #else
  235. #define STRICT_ASSIGN(type, lval, rval) do { \
  236. volatile type __lval; \
  237. \
  238. if (sizeof(type) >= sizeof(long double)) \
  239. (lval) = (rval); \
  240. else { \
  241. __lval = (rval); \
  242. (lval) = __lval; \
  243. } \
  244. } while (0)
  245. #endif
  246. #ifdef __FDLIBM_STDC__
  247. static const double huge = 1.0e300;
  248. #else
  249. static double huge = 1.0e300;
  250. #endif
  251. #ifdef __STDC__
  252. static const double
  253. #else
  254. static double
  255. #endif
  256. tiny = 1.0e-300;
  257. #ifdef __STDC__
  258. static const double
  259. #else
  260. static double
  261. #endif
  262. one= 1.00000000000000000000e+00; /* 0x3FF00000, 0x00000000 */
  263. #ifdef __STDC__
  264. static const double
  265. #else
  266. static double
  267. #endif
  268. TWO52[2]={
  269. 4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */
  270. -4.50359962737049600000e+15, /* 0xC3300000, 0x00000000 */
  271. };
  272. static double freebsd_sqrt(double x);
  273. static double freebsd_floor(double x);
  274. static double freebsd_ceil(double x);
  275. static double freebsd_fabs(double x);
  276. static double freebsd_rint(double x);
  277. static int freebsd_isnan(double x);
  278. static double freebsd_sqrt(double x) /* wrapper sqrt */
  279. {
  280. double z;
  281. int32_t sign = (int)0x80000000;
  282. int32_t ix0,s0,q,m,t,i;
  283. u_int32_t r,t1,s1,ix1,q1;
  284. EXTRACT_WORDS(ix0,ix1,x);
  285. /* take care of Inf and NaN */
  286. if((ix0&0x7ff00000)==0x7ff00000) {
  287. return x*x+x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
  288. sqrt(-inf)=sNaN */
  289. }
  290. /* take care of zero */
  291. if(ix0<=0) {
  292. if(((ix0&(~sign))|ix1)==0) return x;/* sqrt(+-0) = +-0 */
  293. else if(ix0<0)
  294. return (x-x)/(x-x); /* sqrt(-ve) = sNaN */
  295. }
  296. /* normalize x */
  297. m = (ix0>>20);
  298. if(m==0) { /* subnormal x */
  299. while(ix0==0) {
  300. m -= 21;
  301. ix0 |= (ix1>>11); ix1 <<= 21;
  302. }
  303. for(i=0;(ix0&0x00100000)==0;i++) ix0<<=1;
  304. m -= i-1;
  305. ix0 |= (ix1>>(32-i));
  306. ix1 <<= i;
  307. }
  308. m -= 1023; /* unbias exponent */
  309. ix0 = (ix0&0x000fffff)|0x00100000;
  310. if(m&1){ /* odd m, double x to make it even */
  311. ix0 += ix0 + ((ix1&sign)>>31);
  312. ix1 += ix1;
  313. }
  314. m >>= 1; /* m = [m/2] */
  315. /* generate sqrt(x) bit by bit */
  316. ix0 += ix0 + ((ix1&sign)>>31);
  317. ix1 += ix1;
  318. q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */
  319. r = 0x00200000; /* r = moving bit from right to left */
  320. while(r!=0) {
  321. t = s0+r;
  322. if(t<=ix0) {
  323. s0 = t+r;
  324. ix0 -= t;
  325. q += r;
  326. }
  327. ix0 += ix0 + ((ix1&sign)>>31);
  328. ix1 += ix1;
  329. r>>=1;
  330. }
  331. r = sign;
  332. while(r!=0) {
  333. t1 = s1+r;
  334. t = s0;
  335. if((t<ix0)||((t==ix0)&&(t1<=ix1))) {
  336. s1 = t1+r;
  337. if(((t1&sign)==sign)&&(s1&sign)==0) s0 += 1;
  338. ix0 -= t;
  339. if (ix1 < t1) ix0 -= 1;
  340. ix1 -= t1;
  341. q1 += r;
  342. }
  343. ix0 += ix0 + ((ix1&sign)>>31);
  344. ix1 += ix1;
  345. r>>=1;
  346. }
  347. /* use floating add to find out rounding direction */
  348. if((ix0|ix1)!=0) {
  349. z = one-tiny; /* trigger inexact flag */
  350. if (z>=one) {
  351. z = one+tiny;
  352. if (q1==(u_int32_t)0xffffffff) { q1=0; q += 1;}
  353. else if (z>one) {
  354. if (q1==(u_int32_t)0xfffffffe) q+=1;
  355. q1+=2;
  356. } else
  357. q1 += (q1&1);
  358. }
  359. }
  360. ix0 = (q>>1)+0x3fe00000;
  361. ix1 = q1>>1;
  362. if ((q&1)==1) ix1 |= sign;
  363. ix0 += (m <<20);
  364. INSERT_WORDS(z,ix0,ix1);
  365. return z;
  366. }
  367. static double freebsd_floor(double x)
  368. {
  369. int32_t i0,i1,j0;
  370. u_int32_t i,j;
  371. EXTRACT_WORDS(i0,i1,x);
  372. j0 = ((i0>>20)&0x7ff)-0x3ff;
  373. if(j0<20) {
  374. if(j0<0) { /* raise inexact if x != 0 */
  375. if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
  376. if(i0>=0) {i0=i1=0;}
  377. else if(((i0&0x7fffffff)|i1)!=0)
  378. { i0=0xbff00000;i1=0;}
  379. }
  380. } else {
  381. i = (0x000fffff)>>j0;
  382. if(((i0&i)|i1)==0) return x; /* x is integral */
  383. if(huge+x>0.0) { /* raise inexact flag */
  384. if(i0<0) i0 += (0x00100000)>>j0;
  385. i0 &= (~i); i1=0;
  386. }
  387. }
  388. } else if (j0>51) {
  389. if(j0==0x400) return x+x; /* inf or NaN */
  390. else return x; /* x is integral */
  391. } else {
  392. i = ((u_int32_t)(0xffffffff))>>(j0-20);
  393. if((i1&i)==0) return x; /* x is integral */
  394. if(huge+x>0.0) { /* raise inexact flag */
  395. if(i0<0) {
  396. if(j0==20) i0+=1;
  397. else {
  398. j = i1+(1<<(52-j0));
  399. if(j<i1) i0 +=1 ; /* got a carry */
  400. i1=j;
  401. }
  402. }
  403. i1 &= (~i);
  404. }
  405. }
  406. INSERT_WORDS(x,i0,i1);
  407. return x;
  408. }
  409. static double freebsd_ceil(double x)
  410. {
  411. int32_t i0,i1,j0;
  412. u_int32_t i,j;
  413. EXTRACT_WORDS(i0,i1,x);
  414. j0 = ((i0>>20)&0x7ff)-0x3ff;
  415. if(j0<20) {
  416. if(j0<0) { /* raise inexact if x != 0 */
  417. if(huge+x>0.0) {/* return 0*sign(x) if |x|<1 */
  418. if(i0<0) {i0=0x80000000;i1=0;}
  419. else if((i0|i1)!=0) { i0=0x3ff00000;i1=0;}
  420. }
  421. } else {
  422. i = (0x000fffff)>>j0;
  423. if(((i0&i)|i1)==0) return x; /* x is integral */
  424. if(huge+x>0.0) { /* raise inexact flag */
  425. if(i0>0) i0 += (0x00100000)>>j0;
  426. i0 &= (~i); i1=0;
  427. }
  428. }
  429. } else if (j0>51) {
  430. if(j0==0x400) return x+x; /* inf or NaN */
  431. else return x; /* x is integral */
  432. } else {
  433. i = ((u_int32_t)(0xffffffff))>>(j0-20);
  434. if((i1&i)==0) return x; /* x is integral */
  435. if(huge+x>0.0) { /* raise inexact flag */
  436. if(i0>0) {
  437. if(j0==20) i0+=1;
  438. else {
  439. j = i1 + (1<<(52-j0));
  440. if(j<i1) i0+=1; /* got a carry */
  441. i1 = j;
  442. }
  443. }
  444. i1 &= (~i);
  445. }
  446. }
  447. INSERT_WORDS(x,i0,i1);
  448. return x;
  449. }
  450. static double freebsd_rint(double x)
  451. {
  452. int32_t i0,j0,sx;
  453. u_int32_t i,i1;
  454. double w,t;
  455. EXTRACT_WORDS(i0,i1,x);
  456. sx = (i0>>31)&1;
  457. j0 = ((i0>>20)&0x7ff)-0x3ff;
  458. if(j0<20) {
  459. if(j0<0) {
  460. if(((i0&0x7fffffff)|i1)==0) return x;
  461. i1 |= (i0&0x0fffff);
  462. i0 &= 0xfffe0000;
  463. i0 |= ((i1|-i1)>>12)&0x80000;
  464. SET_HIGH_WORD(x,i0);
  465. STRICT_ASSIGN(double,w,TWO52[sx]+x);
  466. t = w-TWO52[sx];
  467. GET_HIGH_WORD(i0,t);
  468. SET_HIGH_WORD(t,(i0&0x7fffffff)|(sx<<31));
  469. return t;
  470. } else {
  471. i = (0x000fffff)>>j0;
  472. if(((i0&i)|i1)==0) return x; /* x is integral */
  473. i>>=1;
  474. if(((i0&i)|i1)!=0) {
  475. /*
  476. * Some bit is set after the 0.5 bit. To avoid the
  477. * possibility of errors from double rounding in
  478. * w = TWO52[sx]+x, adjust the 0.25 bit to a lower
  479. * guard bit. We do this for all j0<=51. The
  480. * adjustment is trickiest for j0==18 and j0==19
  481. * since then it spans the word boundary.
  482. */
  483. if(j0==19) i1 = 0x40000000; else
  484. if(j0==18) i1 = 0x80000000; else
  485. i0 = (i0&(~i))|((0x20000)>>j0);
  486. }
  487. }
  488. } else if (j0>51) {
  489. if(j0==0x400) return x+x; /* inf or NaN */
  490. else return x; /* x is integral */
  491. } else {
  492. i = ((u_int32_t)(0xffffffff))>>(j0-20);
  493. if((i1&i)==0) return x; /* x is integral */
  494. i>>=1;
  495. if((i1&i)!=0) i1 = (i1&(~i))|((0x40000000)>>(j0-20));
  496. }
  497. INSERT_WORDS(x,i0,i1);
  498. STRICT_ASSIGN(double,w,TWO52[sx]+x);
  499. return w-TWO52[sx];
  500. }
  501. static int freebsd_isnan(double d)
  502. {
  503. if (is_little_endian()) {
  504. IEEEd2bits_L u;
  505. u.d = d;
  506. return (u.bits.exp == 2047 && (u.bits.manl != 0 || u.bits.manh != 0));
  507. }
  508. else {
  509. IEEEd2bits_B u;
  510. u.d = d;
  511. return (u.bits.exp == 2047 && (u.bits.manl != 0 || u.bits.manh != 0));
  512. }
  513. }
  514. static double freebsd_fabs(double x)
  515. {
  516. u_int32_t high;
  517. GET_HIGH_WORD(high,x);
  518. SET_HIGH_WORD(x,high&0x7fffffff);
  519. return x;
  520. }
  521. static const float huge_f = 1.0e30F;
  522. static const float
  523. TWO23[2]={
  524. 8.3886080000e+06, /* 0x4b000000 */
  525. -8.3886080000e+06, /* 0xcb000000 */
  526. };
  527. static float
  528. freebsd_truncf(float x)
  529. {
  530. int32_t i0,j0;
  531. u_int32_t i;
  532. GET_FLOAT_WORD(i0,x);
  533. j0 = ((i0>>23)&0xff)-0x7f;
  534. if(j0<23) {
  535. if(j0<0) { /* raise inexact if x != 0 */
  536. if(huge_f+x>0.0F) /* |x|<1, so return 0*sign(x) */
  537. i0 &= 0x80000000;
  538. } else {
  539. i = (0x007fffff)>>j0;
  540. if((i0&i)==0) return x; /* x is integral */
  541. if(huge_f+x>0.0F) /* raise inexact flag */
  542. i0 &= (~i);
  543. }
  544. } else {
  545. if(j0==0x80) return x+x; /* inf or NaN */
  546. else return x; /* x is integral */
  547. }
  548. SET_FLOAT_WORD(x,i0);
  549. return x;
  550. }
  551. static float
  552. freebsd_rintf(float x)
  553. {
  554. int32_t i0,j0,sx;
  555. float w,t;
  556. GET_FLOAT_WORD(i0,x);
  557. sx = (i0>>31)&1;
  558. j0 = ((i0>>23)&0xff)-0x7f;
  559. if(j0<23) {
  560. if(j0<0) {
  561. if((i0&0x7fffffff)==0) return x;
  562. STRICT_ASSIGN(float,w,TWO23[sx]+x);
  563. t = w-TWO23[sx];
  564. GET_FLOAT_WORD(i0,t);
  565. SET_FLOAT_WORD(t,(i0&0x7fffffff)|(sx<<31));
  566. return t;
  567. }
  568. STRICT_ASSIGN(float,w,TWO23[sx]+x);
  569. return w-TWO23[sx];
  570. }
  571. if(j0==0x80) return x+x; /* inf or NaN */
  572. else return x; /* x is integral */
  573. }
  574. static float
  575. freebsd_ceilf(float x)
  576. {
  577. int32_t i0,j0;
  578. u_int32_t i;
  579. GET_FLOAT_WORD(i0,x);
  580. j0 = ((i0>>23)&0xff)-0x7f;
  581. if(j0<23) {
  582. if(j0<0) { /* raise inexact if x != 0 */
  583. if(huge_f+x>(float)0.0) {/* return 0*sign(x) if |x|<1 */
  584. if(i0<0) {i0=0x80000000;}
  585. else if(i0!=0) { i0=0x3f800000;}
  586. }
  587. } else {
  588. i = (0x007fffff)>>j0;
  589. if((i0&i)==0) return x; /* x is integral */
  590. if(huge_f+x>(float)0.0) { /* raise inexact flag */
  591. if(i0>0) i0 += (0x00800000)>>j0;
  592. i0 &= (~i);
  593. }
  594. }
  595. } else {
  596. if(j0==0x80) return x+x; /* inf or NaN */
  597. else return x; /* x is integral */
  598. }
  599. SET_FLOAT_WORD(x,i0);
  600. return x;
  601. }
  602. static float
  603. freebsd_floorf(float x)
  604. {
  605. int32_t i0,j0;
  606. u_int32_t i;
  607. GET_FLOAT_WORD(i0,x);
  608. j0 = ((i0>>23)&0xff)-0x7f;
  609. if(j0<23) {
  610. if(j0<0) { /* raise inexact if x != 0 */
  611. if(huge_f+x>(float)0.0) {/* return 0*sign(x) if |x|<1 */
  612. if(i0>=0) {i0=0;}
  613. else if((i0&0x7fffffff)!=0)
  614. { i0=0xbf800000;}
  615. }
  616. } else {
  617. i = (0x007fffff)>>j0;
  618. if((i0&i)==0) return x; /* x is integral */
  619. if(huge_f+x>(float)0.0) { /* raise inexact flag */
  620. if(i0<0) i0 += (0x00800000)>>j0;
  621. i0 &= (~i);
  622. }
  623. }
  624. } else {
  625. if(j0==0x80) return x+x; /* inf or NaN */
  626. else return x; /* x is integral */
  627. }
  628. SET_FLOAT_WORD(x,i0);
  629. return x;
  630. }
  631. static float
  632. freebsd_fminf(float x, float y)
  633. {
  634. if (is_little_endian()) {
  635. IEEEf2bits_L u[2];
  636. u[0].f = x;
  637. u[1].f = y;
  638. /* Check for NaNs to avoid raising spurious exceptions. */
  639. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  640. return (y);
  641. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  642. return (x);
  643. /* Handle comparisons of signed zeroes. */
  644. if (u[0].bits.sign != u[1].bits.sign)
  645. return (u[u[1].bits.sign].f);
  646. }
  647. else {
  648. IEEEf2bits_B u[2];
  649. u[0].f = x;
  650. u[1].f = y;
  651. /* Check for NaNs to avoid raising spurious exceptions. */
  652. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  653. return (y);
  654. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  655. return (x);
  656. /* Handle comparisons of signed zeroes. */
  657. if (u[0].bits.sign != u[1].bits.sign)
  658. return (u[u[1].bits.sign].f);
  659. }
  660. return (x < y ? x : y);
  661. }
  662. static float
  663. freebsd_fmaxf(float x, float y)
  664. {
  665. if (is_little_endian()) {
  666. IEEEf2bits_L u[2];
  667. u[0].f = x;
  668. u[1].f = y;
  669. /* Check for NaNs to avoid raising spurious exceptions. */
  670. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  671. return (y);
  672. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  673. return (x);
  674. /* Handle comparisons of signed zeroes. */
  675. if (u[0].bits.sign != u[1].bits.sign)
  676. return (u[u[0].bits.sign].f);
  677. }
  678. else {
  679. IEEEf2bits_B u[2];
  680. u[0].f = x;
  681. u[1].f = y;
  682. /* Check for NaNs to avoid raising spurious exceptions. */
  683. if (u[0].bits.exp == 255 && u[0].bits.man != 0)
  684. return (y);
  685. if (u[1].bits.exp == 255 && u[1].bits.man != 0)
  686. return (x);
  687. /* Handle comparisons of signed zeroes. */
  688. if (u[0].bits.sign != u[1].bits.sign)
  689. return (u[u[0].bits.sign].f);
  690. }
  691. return (x > y ? x : y);
  692. }
  693. double sqrt(double x)
  694. {
  695. return freebsd_sqrt(x);
  696. }
  697. double floor(double x)
  698. {
  699. return freebsd_floor(x);
  700. }
  701. double ceil(double x)
  702. {
  703. return freebsd_ceil(x);
  704. }
  705. double fmin(double x, double y)
  706. {
  707. return x < y ? x : y;
  708. }
  709. double fmax(double x, double y)
  710. {
  711. return x > y ? x : y;
  712. }
  713. double rint(double x)
  714. {
  715. return freebsd_rint(x);
  716. }
  717. double fabs(double x)
  718. {
  719. return freebsd_fabs(x);
  720. }
  721. int isnan(double x)
  722. {
  723. return freebsd_isnan(x);
  724. }
  725. double trunc(double x)
  726. {
  727. return (x > 0) ? freebsd_floor(x) : freebsd_ceil(x);
  728. }
  729. int signbit(double x)
  730. {
  731. return ((__HI(x) & 0x80000000) >> 31);
  732. }
  733. float
  734. truncf(float x)
  735. {
  736. return freebsd_truncf(x);
  737. }
  738. float
  739. rintf(float x)
  740. {
  741. return freebsd_rintf(x);
  742. }
  743. float
  744. ceilf(float x)
  745. {
  746. return freebsd_ceilf(x);
  747. }
  748. float
  749. floorf(float x)
  750. {
  751. return freebsd_floorf(x);
  752. }
  753. float
  754. fminf(float x, float y)
  755. {
  756. return freebsd_fminf(x, y);
  757. }
  758. float
  759. fmaxf(float x, float y)
  760. {
  761. return freebsd_fmaxf(x, y);
  762. }