math.c 21 KB

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