floorl.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /* ceill()
  2. * floorl()
  3. * frexpl()
  4. * ldexpl()
  5. * fabsl()
  6. * signbitl()
  7. * isnanl()
  8. * isfinitel()
  9. *
  10. * Floating point numeric utilities
  11. *
  12. *
  13. *
  14. * SYNOPSIS:
  15. *
  16. * long double ceill(), floorl(), frexpl(), ldexpl(), fabsl();
  17. * int signbitl(), isnanl(), isfinitel();
  18. * long double x, y;
  19. * int expnt, n;
  20. *
  21. * y = floorl(x);
  22. * y = ceill(x);
  23. * y = frexpl( x, &expnt );
  24. * y = ldexpl( x, n );
  25. * y = fabsl( x );
  26. * n = signbitl(x);
  27. * n = isnanl(x);
  28. * n = isfinitel(x);
  29. *
  30. *
  31. *
  32. * DESCRIPTION:
  33. *
  34. * The following routines return a long double precision floating point
  35. * result:
  36. *
  37. * floorl() returns the largest integer less than or equal to x.
  38. * It truncates toward minus infinity.
  39. *
  40. * ceill() returns the smallest integer greater than or equal
  41. * to x. It truncates toward plus infinity.
  42. *
  43. * frexpl() extracts the exponent from x. It returns an integer
  44. * power of two to expnt and the significand between 0.5 and 1
  45. * to y. Thus x = y * 2**expn.
  46. *
  47. * ldexpl() multiplies x by 2**n.
  48. *
  49. * fabsl() returns the absolute value of its argument.
  50. *
  51. * These functions are part of the standard C run time library
  52. * for some but not all C compilers. The ones supplied are
  53. * written in C for IEEE arithmetic. They should
  54. * be used only if your compiler library does not already have
  55. * them.
  56. *
  57. * The IEEE versions assume that denormal numbers are implemented
  58. * in the arithmetic. Some modifications will be required if
  59. * the arithmetic has abrupt rather than gradual underflow.
  60. */
  61. /*
  62. Cephes Math Library Release 2.7: May, 1998
  63. Copyright 1984, 1987, 1988, 1992, 1998 by Stephen L. Moshier
  64. */
  65. #include <math.h>
  66. /* This is defined in mconf.h. */
  67. /* #define DENORMAL 1 */
  68. #ifdef UNK
  69. /* Change UNK into something else. */
  70. #undef UNK
  71. #if BIGENDIAN
  72. #define MIEEE 1
  73. #else
  74. #define IBMPC 1
  75. #endif
  76. #endif
  77. #ifdef IBMPC
  78. #define EXPMSK 0x800f
  79. #define MEXP 0x7ff
  80. #define NBITS 64
  81. #endif
  82. #ifdef MIEEE
  83. #define EXPMSK 0x800f
  84. #define MEXP 0x7ff
  85. #define NBITS 64
  86. #endif
  87. extern double MAXNUML;
  88. #ifdef ANSIPROT
  89. extern long double fabsl ( long double );
  90. extern long double floorl ( long double );
  91. extern int isnanl ( long double );
  92. #else
  93. long double fabsl(), floorl();
  94. int isnanl();
  95. #endif
  96. #ifdef INFINITIES
  97. extern long double INFINITYL;
  98. #endif
  99. #ifdef NANS
  100. extern long double NANL;
  101. #endif
  102. long double fabsl(x)
  103. long double x;
  104. {
  105. union
  106. {
  107. long double d;
  108. short i[6];
  109. } u;
  110. u.d = x;
  111. #ifdef IBMPC
  112. u.i[4] &= 0x7fff;
  113. #endif
  114. #ifdef MIEEE
  115. u.i[0] &= 0x7fff;
  116. #endif
  117. return( u.d );
  118. }
  119. long double ceill(x)
  120. long double x;
  121. {
  122. long double y;
  123. #ifdef UNK
  124. mtherr( "ceill", DOMAIN );
  125. return(0.0L);
  126. #endif
  127. #ifdef INFINITIES
  128. if(x == -INFINITYL)
  129. return(x);
  130. #endif
  131. #ifdef MINUSZERO
  132. if(x == 0.0L)
  133. return(x);
  134. #endif
  135. y = floorl(x);
  136. if( y < x )
  137. y += 1.0L;
  138. return(y);
  139. }
  140. /* Bit clearing masks: */
  141. static unsigned short bmask[] = {
  142. 0xffff,
  143. 0xfffe,
  144. 0xfffc,
  145. 0xfff8,
  146. 0xfff0,
  147. 0xffe0,
  148. 0xffc0,
  149. 0xff80,
  150. 0xff00,
  151. 0xfe00,
  152. 0xfc00,
  153. 0xf800,
  154. 0xf000,
  155. 0xe000,
  156. 0xc000,
  157. 0x8000,
  158. 0x0000,
  159. };
  160. long double floorl(x)
  161. long double x;
  162. {
  163. unsigned short *p;
  164. union
  165. {
  166. long double y;
  167. unsigned short sh[6];
  168. } u;
  169. int e;
  170. #ifdef UNK
  171. mtherr( "floor", DOMAIN );
  172. return(0.0L);
  173. #endif
  174. #ifdef INFINITIES
  175. if( x == INFINITYL )
  176. return(x);
  177. #endif
  178. #ifdef MINUSZERO
  179. if(x == 0.0L)
  180. return(x);
  181. #endif
  182. u.y = x;
  183. /* find the exponent (power of 2) */
  184. #ifdef IBMPC
  185. p = (unsigned short *)&u.sh[4];
  186. e = (*p & 0x7fff) - 0x3fff;
  187. p -= 4;
  188. #endif
  189. #ifdef MIEEE
  190. p = (unsigned short *)&u.sh[0];
  191. e = (*p & 0x7fff) - 0x3fff;
  192. p += 5;
  193. #endif
  194. if( e < 0 )
  195. {
  196. if( u.y < 0.0L )
  197. return( -1.0L );
  198. else
  199. return( 0.0L );
  200. }
  201. e = (NBITS -1) - e;
  202. /* clean out 16 bits at a time */
  203. while( e >= 16 )
  204. {
  205. #ifdef IBMPC
  206. *p++ = 0;
  207. #endif
  208. #ifdef MIEEE
  209. *p-- = 0;
  210. #endif
  211. e -= 16;
  212. }
  213. /* clear the remaining bits */
  214. if( e > 0 )
  215. *p &= bmask[e];
  216. if( (x < 0) && (u.y != x) )
  217. u.y -= 1.0L;
  218. return(u.y);
  219. }
  220. long double frexpl( x, pw2 )
  221. long double x;
  222. int *pw2;
  223. {
  224. union
  225. {
  226. long double y;
  227. unsigned short sh[6];
  228. } u;
  229. int i, k;
  230. short *q;
  231. u.y = x;
  232. #ifdef NANS
  233. if(isnanl(x))
  234. {
  235. *pw2 = 0;
  236. return(x);
  237. }
  238. #endif
  239. #ifdef INFINITIES
  240. if(x == -INFINITYL)
  241. {
  242. *pw2 = 0;
  243. return(x);
  244. }
  245. #endif
  246. #ifdef MINUSZERO
  247. if(x == 0.0L)
  248. {
  249. *pw2 = 0;
  250. return(x);
  251. }
  252. #endif
  253. #ifdef UNK
  254. mtherr( "frexpl", DOMAIN );
  255. return(0.0L);
  256. #endif
  257. /* find the exponent (power of 2) */
  258. #ifdef IBMPC
  259. q = (short *)&u.sh[4];
  260. i = *q & 0x7fff;
  261. #endif
  262. #ifdef MIEEE
  263. q = (short *)&u.sh[0];
  264. i = *q & 0x7fff;
  265. #endif
  266. if( i == 0 )
  267. {
  268. if( u.y == 0.0L )
  269. {
  270. *pw2 = 0;
  271. return(0.0L);
  272. }
  273. /* Number is denormal or zero */
  274. #ifdef DENORMAL
  275. /* Handle denormal number. */
  276. do
  277. {
  278. u.y *= 2.0L;
  279. i -= 1;
  280. k = *q & 0x7fff;
  281. }
  282. while( (k == 0) && (i > -66) );
  283. i = i + k;
  284. #else
  285. *pw2 = 0;
  286. return(0.0L);
  287. #endif /* DENORMAL */
  288. }
  289. *pw2 = i - 0x3ffe;
  290. /* *q = 0x3ffe; */
  291. /* Preserve sign of argument. */
  292. *q &= 0x8000;
  293. *q |= 0x3ffe;
  294. return( u.y );
  295. }
  296. long double ldexpl( x, pw2 )
  297. long double x;
  298. int pw2;
  299. {
  300. union
  301. {
  302. long double y;
  303. unsigned short sh[6];
  304. } u;
  305. unsigned short *q;
  306. long e;
  307. #ifdef UNK
  308. mtherr( "ldexp", DOMAIN );
  309. return(0.0L);
  310. #endif
  311. u.y = x;
  312. #ifdef IBMPC
  313. q = (unsigned short *)&u.sh[4];
  314. #endif
  315. #ifdef MIEEE
  316. q = (unsigned short *)&u.sh[0];
  317. #endif
  318. while( (e = (*q & 0x7fffL)) == 0 )
  319. {
  320. #ifdef DENORMAL
  321. if( u.y == 0.0L )
  322. {
  323. return( 0.0L );
  324. }
  325. /* Input is denormal. */
  326. if( pw2 > 0 )
  327. {
  328. u.y *= 2.0L;
  329. pw2 -= 1;
  330. }
  331. if( pw2 < 0 )
  332. {
  333. if( pw2 < -64 )
  334. return(0.0L);
  335. u.y *= 0.5L;
  336. pw2 += 1;
  337. }
  338. if( pw2 == 0 )
  339. return(u.y);
  340. #else
  341. return( 0.0L );
  342. #endif
  343. }
  344. e = e + pw2;
  345. /* Handle overflow */
  346. if( e > 0x7fffL )
  347. {
  348. return( MAXNUML );
  349. }
  350. *q &= 0x8000;
  351. /* Handle denormalized results */
  352. if( e < 1 )
  353. {
  354. #ifdef DENORMAL
  355. if( e < -64 )
  356. return(0.0L);
  357. #ifdef IBMPC
  358. *(q-1) |= 0x8000;
  359. #endif
  360. #ifdef MIEEE
  361. *(q+2) |= 0x8000;
  362. #endif
  363. while( e < 1 )
  364. {
  365. u.y *= 0.5L;
  366. e += 1;
  367. }
  368. e = 0;
  369. #else
  370. return(0.0L);
  371. #endif
  372. }
  373. *q |= (unsigned short) e & 0x7fff;
  374. return(u.y);
  375. }