floor.c 6.4 KB

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