floorf.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /* ceilf()
  2. * floorf()
  3. * frexpf()
  4. * ldexpf()
  5. * signbitf()
  6. * isnanf()
  7. * isfinitef()
  8. *
  9. * Single precision floating point numeric utilities
  10. *
  11. *
  12. *
  13. * SYNOPSIS:
  14. *
  15. * float x, y;
  16. * float ceilf(), floorf(), frexpf(), ldexpf();
  17. * int signbit(), isnan(), isfinite();
  18. * int expnt, n;
  19. *
  20. * y = floorf(x);
  21. * y = ceilf(x);
  22. * y = frexpf( x, &expnt );
  23. * y = ldexpf( 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 single precision floating point
  33. * result.
  34. *
  35. * sfloor() returns the largest integer less than or equal to x.
  36. * It truncates toward minus infinity.
  37. *
  38. * sceil() returns the smallest integer greater than or equal
  39. * to x. It truncates toward plus infinity.
  40. *
  41. * sfrexp() 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. * ldexpf() 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.1: December, 1988
  61. Copyright 1984, 1987, 1988 by Stephen L. Moshier
  62. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  63. */
  64. #include <math.h>
  65. #ifdef DEC
  66. #undef DENORMAL
  67. #define DENORMAL 0
  68. #endif
  69. #ifdef UNK
  70. #undef UNK
  71. #if BIGENDIAN
  72. #define MIEEE 1
  73. #else
  74. #define IBMPC 1
  75. #endif
  76. /*
  77. char *unkmsg = "ceil(), floor(), frexp(), ldexp() must be rewritten!\n";
  78. */
  79. #endif
  80. #define EXPMSK 0x807f
  81. #define MEXP 255
  82. #define NBITS 24
  83. extern float MAXNUMF; /* (2^24 - 1) * 2^103 */
  84. #ifdef ANSIC
  85. float floorf(float);
  86. #else
  87. float floorf();
  88. #endif
  89. float ceilf( float x )
  90. {
  91. float y;
  92. #ifdef UNK
  93. printf( "%s\n", unkmsg );
  94. return(0.0);
  95. #endif
  96. y = floorf( (float )x );
  97. if( y < x )
  98. y += 1.0;
  99. return(y);
  100. }
  101. /* Bit clearing masks: */
  102. static unsigned short bmask[] = {
  103. 0xffff,
  104. 0xfffe,
  105. 0xfffc,
  106. 0xfff8,
  107. 0xfff0,
  108. 0xffe0,
  109. 0xffc0,
  110. 0xff80,
  111. 0xff00,
  112. 0xfe00,
  113. 0xfc00,
  114. 0xf800,
  115. 0xf000,
  116. 0xe000,
  117. 0xc000,
  118. 0x8000,
  119. 0x0000,
  120. };
  121. float floorf( float x )
  122. {
  123. unsigned short *p;
  124. union
  125. {
  126. float y;
  127. unsigned short i[2];
  128. } u;
  129. int e;
  130. #ifdef UNK
  131. printf( "%s\n", unkmsg );
  132. return(0.0);
  133. #endif
  134. u.y = x;
  135. /* find the exponent (power of 2) */
  136. #ifdef DEC
  137. p = &u.i[0];
  138. e = (( *p >> 7) & 0377) - 0201;
  139. p += 3;
  140. #endif
  141. #ifdef IBMPC
  142. p = &u.i[1];
  143. e = (( *p >> 7) & 0xff) - 0x7f;
  144. p -= 1;
  145. #endif
  146. #ifdef MIEEE
  147. p = &u.i[0];
  148. e = (( *p >> 7) & 0xff) - 0x7f;
  149. p += 1;
  150. #endif
  151. if( e < 0 )
  152. {
  153. if( u.y < 0 )
  154. return( -1.0 );
  155. else
  156. return( 0.0 );
  157. }
  158. e = (NBITS -1) - e;
  159. /* clean out 16 bits at a time */
  160. while( e >= 16 )
  161. {
  162. #ifdef IBMPC
  163. *p++ = 0;
  164. #endif
  165. #ifdef DEC
  166. *p-- = 0;
  167. #endif
  168. #ifdef MIEEE
  169. *p-- = 0;
  170. #endif
  171. e -= 16;
  172. }
  173. /* clear the remaining bits */
  174. if( e > 0 )
  175. *p &= bmask[e];
  176. if( (x < 0) && (u.y != x) )
  177. u.y -= 1.0;
  178. return(u.y);
  179. }
  180. float frexpf( float x, int *pw2 )
  181. {
  182. union
  183. {
  184. float y;
  185. unsigned short i[2];
  186. } u;
  187. int i, k;
  188. short *q;
  189. u.y = x;
  190. #ifdef UNK
  191. printf( "%s\n", unkmsg );
  192. return(0.0);
  193. #endif
  194. #ifdef IBMPC
  195. q = &u.i[1];
  196. #endif
  197. #ifdef DEC
  198. q = &u.i[0];
  199. #endif
  200. #ifdef MIEEE
  201. q = &u.i[0];
  202. #endif
  203. /* find the exponent (power of 2) */
  204. i = ( *q >> 7) & 0xff;
  205. if( i == 0 )
  206. {
  207. if( u.y == 0.0 )
  208. {
  209. *pw2 = 0;
  210. return(0.0);
  211. }
  212. /* Number is denormal or zero */
  213. #if DENORMAL
  214. /* Handle denormal number. */
  215. do
  216. {
  217. u.y *= 2.0;
  218. i -= 1;
  219. k = ( *q >> 7) & 0xff;
  220. }
  221. while( k == 0 );
  222. i = i + k;
  223. #else
  224. *pw2 = 0;
  225. return( 0.0 );
  226. #endif /* DENORMAL */
  227. }
  228. i -= 0x7e;
  229. *pw2 = i;
  230. *q &= 0x807f; /* strip all exponent bits */
  231. *q |= 0x3f00; /* mantissa between 0.5 and 1 */
  232. return( u.y );
  233. }
  234. float ldexpf( float x, int pw2 )
  235. {
  236. union
  237. {
  238. float y;
  239. unsigned short i[2];
  240. } u;
  241. short *q;
  242. int e;
  243. #ifdef UNK
  244. printf( "%s\n", unkmsg );
  245. return(0.0);
  246. #endif
  247. u.y = x;
  248. #ifdef DEC
  249. q = &u.i[0];
  250. #endif
  251. #ifdef IBMPC
  252. q = &u.i[1];
  253. #endif
  254. #ifdef MIEEE
  255. q = &u.i[0];
  256. #endif
  257. while( (e = ( *q >> 7) & 0xff) == 0 )
  258. {
  259. if( u.y == (float )0.0 )
  260. {
  261. return( 0.0 );
  262. }
  263. /* Input is denormal. */
  264. if( pw2 > 0 )
  265. {
  266. u.y *= 2.0;
  267. pw2 -= 1;
  268. }
  269. if( pw2 < 0 )
  270. {
  271. if( pw2 < -24 )
  272. return( 0.0 );
  273. u.y *= 0.5;
  274. pw2 += 1;
  275. }
  276. if( pw2 == 0 )
  277. return(u.y);
  278. }
  279. e += pw2;
  280. /* Handle overflow */
  281. if( e > MEXP )
  282. {
  283. return( MAXNUMF );
  284. }
  285. *q &= 0x807f;
  286. /* Handle denormalized results */
  287. if( e < 1 )
  288. {
  289. #if DENORMAL
  290. if( e < -24 )
  291. return( 0.0 );
  292. *q |= 0x80; /* Set LSB of exponent. */
  293. /* For denormals, significant bits may be lost even
  294. when dividing by 2. Construct 2^-(1-e) so the result
  295. is obtained with only one multiplication. */
  296. u.y *= ldexpf(1.0f, e - 1);
  297. return(u.y);
  298. #else
  299. return( 0.0 );
  300. #endif
  301. }
  302. *q |= (e & 0xff) << 7;
  303. return(u.y);
  304. }
  305. /* Return 1 if the sign bit of x is 1, else 0. */
  306. int signbitf(x)
  307. float x;
  308. {
  309. union
  310. {
  311. float f;
  312. short s[4];
  313. int i;
  314. } u;
  315. u.f = x;
  316. if( sizeof(int) == 4 )
  317. {
  318. #ifdef IBMPC
  319. return( u.i < 0 );
  320. #endif
  321. #ifdef DEC
  322. return( u.s[1] < 0 );
  323. #endif
  324. #ifdef MIEEE
  325. return( u.i < 0 );
  326. #endif
  327. }
  328. else
  329. {
  330. #ifdef IBMPC
  331. return( u.s[1] < 0 );
  332. #endif
  333. #ifdef DEC
  334. return( u.s[1] < 0 );
  335. #endif
  336. #ifdef MIEEE
  337. return( u.s[0] < 0 );
  338. #endif
  339. }
  340. }
  341. /* Return 1 if x is a number that is Not a Number, else return 0. */
  342. int isnanf(x)
  343. float x;
  344. {
  345. #ifdef NANS
  346. union
  347. {
  348. float f;
  349. unsigned short s[2];
  350. unsigned int i;
  351. } u;
  352. u.f = x;
  353. if( sizeof(int) == 4 )
  354. {
  355. #ifdef IBMPC
  356. if( ((u.i & 0x7f800000) == 0x7f800000)
  357. && ((u.i & 0x007fffff) != 0) )
  358. return 1;
  359. #endif
  360. #ifdef DEC
  361. if( (u.s[1] & 0x7f80) == 0)
  362. {
  363. if( (u.s[1] | u.s[0]) != 0 )
  364. return(1);
  365. }
  366. #endif
  367. #ifdef MIEEE
  368. if( ((u.i & 0x7f800000) == 0x7f800000)
  369. && ((u.i & 0x007fffff) != 0) )
  370. return 1;
  371. #endif
  372. return(0);
  373. }
  374. else
  375. { /* size int not 4 */
  376. #ifdef IBMPC
  377. if( (u.s[1] & 0x7f80) == 0x7f80)
  378. {
  379. if( ((u.s[1] & 0x007f) | u.s[0]) != 0 )
  380. return(1);
  381. }
  382. #endif
  383. #ifdef DEC
  384. if( (u.s[1] & 0x7f80) == 0)
  385. {
  386. if( (u.s[1] | u.s[0]) != 0 )
  387. return(1);
  388. }
  389. #endif
  390. #ifdef MIEEE
  391. if( (u.s[0] & 0x7f80) == 0x7f80)
  392. {
  393. if( ((u.s[0] & 0x000f) | u.s[1]) != 0 )
  394. return(1);
  395. }
  396. #endif
  397. return(0);
  398. } /* size int not 4 */
  399. #else
  400. /* No NANS. */
  401. return(0);
  402. #endif
  403. }
  404. /* Return 1 if x is not infinite and is not a NaN. */
  405. int isfinitef(x)
  406. float x;
  407. {
  408. #ifdef INFINITIES
  409. union
  410. {
  411. float f;
  412. unsigned short s[2];
  413. unsigned int i;
  414. } u;
  415. u.f = x;
  416. if( sizeof(int) == 4 )
  417. {
  418. #ifdef IBMPC
  419. if( (u.i & 0x7f800000) != 0x7f800000)
  420. return 1;
  421. #endif
  422. #ifdef DEC
  423. if( (u.s[1] & 0x7f80) == 0)
  424. {
  425. if( (u.s[1] | u.s[0]) != 0 )
  426. return(1);
  427. }
  428. #endif
  429. #ifdef MIEEE
  430. if( (u.i & 0x7f800000) != 0x7f800000)
  431. return 1;
  432. #endif
  433. return(0);
  434. }
  435. else
  436. {
  437. #ifdef IBMPC
  438. if( (u.s[1] & 0x7f80) != 0x7f80)
  439. return 1;
  440. #endif
  441. #ifdef DEC
  442. if( (u.s[1] & 0x7f80) == 0)
  443. {
  444. if( (u.s[1] | u.s[0]) != 0 )
  445. return(1);
  446. }
  447. #endif
  448. #ifdef MIEEE
  449. if( (u.s[0] & 0x7f80) != 0x7f80)
  450. return 1;
  451. #endif
  452. return(0);
  453. }
  454. #else
  455. /* No INFINITY. */
  456. return(1);
  457. #endif
  458. }