fpmacros.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /***********************************************************************
  2. ** File: fpmacros.c
  3. **
  4. ** Contains: C source code for implementations of floating-point
  5. ** functions which involve float format numbers, as
  6. ** defined in header <fp.h>. In particular, this file
  7. ** contains implementations of functions
  8. ** __fpclassify(d,f), __isnormal(d,f), __isfinite(d,f),
  9. ** __isnan(d,f), and __signbit(d,f). This file targets
  10. ** PowerPC platforms.
  11. **
  12. ** Written by: Robert A. Murley, Ali Sazegari
  13. **
  14. ** Copyright: c 2001 by Apple Computer, Inc., all rights reserved
  15. **
  16. ** Change History (most recent first):
  17. **
  18. ** 07 Jul 01 ram First created from fpfloatfunc.c, fp.c,
  19. ** classify.c and sign.c in MathLib v3 Mac OS9.
  20. **
  21. ***********************************************************************/
  22. #include <features.h>
  23. #include "fp_private.h"
  24. #define SIGN_MASK 0x80000000
  25. #define NSIGN_MASK 0x7fffffff
  26. #define FEXP_MASK 0x7f800000
  27. #define FFRAC_MASK 0x007fffff
  28. /***********************************************************************
  29. long int __fpclassifyf(float x) returns the classification code of the
  30. argument x, as defined in <fp.h>.
  31. Exceptions: INVALID signaled if x is a signaling NaN; in this case,
  32. the FP_QNAN code is returned.
  33. Calls: none
  34. ***********************************************************************/
  35. long int __fpclassifyf ( float x )
  36. {
  37. unsigned long int iexp;
  38. union {
  39. unsigned long int lval;
  40. float fval;
  41. } z;
  42. z.fval = x;
  43. iexp = z.lval & FEXP_MASK; /* isolate float exponent */
  44. if (iexp == FEXP_MASK) { /* NaN or INF case */
  45. if ((z.lval & 0x007fffff) == 0)
  46. return (long int) FP_INFINITE;
  47. else if ((z.lval & 0x00400000) != 0)
  48. return (long int) FP_QNAN;
  49. else
  50. return (long int) FP_SNAN;
  51. }
  52. if (iexp != 0) /* normal float */
  53. return (long int) FP_NORMAL;
  54. if (x == 0.0)
  55. return (long int) FP_ZERO; /* zero */
  56. else
  57. return (long int) FP_SUBNORMAL; /* must be subnormal */
  58. }
  59. /***********************************************************************
  60. Function __fpclassify,
  61. Implementation of classify of a double number for the PowerPC.
  62. Exceptions: INVALID signaled if x is a signaling NaN; in this case,
  63. the FP_QNAN code is returned.
  64. Calls: none
  65. ***********************************************************************/
  66. long int __fpclassify ( double arg )
  67. {
  68. register unsigned long int exponent;
  69. union
  70. {
  71. dHexParts hex;
  72. double dbl;
  73. } x;
  74. x.dbl = arg;
  75. exponent = x.hex.high & dExpMask;
  76. if ( exponent == dExpMask )
  77. {
  78. if ( ( ( x.hex.high & dHighMan ) | x.hex.low ) == 0 )
  79. return (long int) FP_INFINITE;
  80. else
  81. return ( x.hex.high & 0x00080000 ) ? FP_QNAN : FP_SNAN;
  82. }
  83. else if ( exponent != 0)
  84. return (long int) FP_NORMAL;
  85. else {
  86. if ( arg == 0.0 )
  87. return (long int) FP_ZERO;
  88. else
  89. return (long int) FP_SUBNORMAL;
  90. }
  91. }
  92. /***********************************************************************
  93. long int __isnormalf(float x) returns nonzero if and only if x is a
  94. normalized float number and zero otherwise.
  95. Exceptions: INVALID is raised if x is a signaling NaN; in this case,
  96. zero is returned.
  97. Calls: none
  98. ***********************************************************************/
  99. long int __isnormalf ( float x )
  100. {
  101. unsigned long int iexp;
  102. union {
  103. unsigned long int lval;
  104. float fval;
  105. } z;
  106. z.fval = x;
  107. iexp = z.lval & FEXP_MASK; /* isolate float exponent */
  108. return ((iexp != FEXP_MASK) && (iexp != 0));
  109. }
  110. long int __isnorma ( double x )
  111. {
  112. return ( __fpclassify ( x ) == FP_NORMAL );
  113. }
  114. /***********************************************************************
  115. long int __isfinitef(float x) returns nonzero if and only if x is a
  116. finite (normal, subnormal, or zero) float number and zero otherwise.
  117. Exceptions: INVALID is raised if x is a signaling NaN; in this case,
  118. zero is returned.
  119. Calls: none
  120. ***********************************************************************/
  121. long int __finitef ( float x )
  122. {
  123. union {
  124. unsigned long int lval;
  125. float fval;
  126. } z;
  127. z.fval = x;
  128. return ((z.lval & FEXP_MASK) != FEXP_MASK);
  129. }
  130. long int __finite ( double x )
  131. {
  132. return ( __fpclassify ( x ) >= FP_ZERO );
  133. }
  134. /***********************************************************************
  135. long int __signbitf(float x) returns nonzero if and only if the sign
  136. bit of x is set and zero otherwise.
  137. Exceptions: INVALID is raised if x is a signaling NaN.
  138. Calls: none
  139. ***********************************************************************/
  140. long int __signbitf ( float x )
  141. {
  142. union {
  143. unsigned long int lval;
  144. float fval;
  145. } z;
  146. z.fval = x;
  147. return ((z.lval & SIGN_MASK) != 0);
  148. }
  149. /***********************************************************************
  150. Function sign of a double.
  151. Implementation of sign bit for the PowerPC.
  152. Calls: none
  153. ***********************************************************************/
  154. long int __signbit ( double arg )
  155. {
  156. union
  157. {
  158. dHexParts hex;
  159. double dbl;
  160. } x;
  161. long int sign;
  162. x.dbl = arg;
  163. sign = ( ( x.hex.high & dSgnMask ) == dSgnMask ) ? 1 : 0;
  164. return sign;
  165. }
  166. /***********************************************************************
  167. * long int __isinff(float x) returns -1 if value represents negative
  168. * infinity, 1 if value represents positive infinity,
  169. * and 0 otherwise.
  170. *
  171. * Calls: __signbit
  172. * +***********************************************************************/
  173. long int __isinff ( float x )
  174. {
  175. long int class = __fpclassifyf(x);
  176. if ( class == FP_INFINITE ) {
  177. return ( (__signbitf(x)) ? -1 : 1);
  178. }
  179. return 0;
  180. }
  181. weak_alias (__isinff, isinff)
  182. long int __isinf ( double x )
  183. {
  184. long int class = __fpclassify(x);
  185. if ( class == FP_INFINITE ) {
  186. return ( (__signbit(x)) ? -1 : 1);
  187. }
  188. return 0;
  189. }
  190. weak_alias (__isinf, isinf)
  191. #if 0
  192. long int __isinfl ( long double x )
  193. {
  194. long int class = __fpclassify(x);
  195. if ( class == FP_INFINITE ) {
  196. return ( (__signbit(x)) ? -1 : 1);
  197. }
  198. return 0;
  199. }
  200. weak_alias (__isinfl, isinfl);
  201. #else
  202. weak_alias (__isinf, isinfl)
  203. #endif
  204. /***********************************************************************
  205. long int __isnanf(float x) returns nonzero if and only if x is a
  206. NaN and zero otherwise.
  207. Exceptions: INVALID is raised if x is a signaling NaN; in this case,
  208. nonzero is returned.
  209. Calls: none
  210. ***********************************************************************/
  211. long int __isnanf ( float x )
  212. {
  213. union {
  214. unsigned long int lval;
  215. float fval;
  216. } z;
  217. z.fval = x;
  218. return (((z.lval&FEXP_MASK) == FEXP_MASK) && ((z.lval&FFRAC_MASK) != 0));
  219. }
  220. weak_alias (__isnanf, isnanf);
  221. long int __isnan ( double x )
  222. {
  223. long int class = __fpclassify(x);
  224. return ( ( class == FP_SNAN ) || ( class == FP_QNAN ) );
  225. }
  226. weak_alias (__isnan, isnan);
  227. #if 0
  228. long int __isnanl ( long double x )
  229. {
  230. long int class = __fpclassify(x);
  231. return ( ( class == FP_SNAN ) || ( class == FP_QNAN ) );
  232. }
  233. weak_alias (__isnanl, isnanl);
  234. #else
  235. weak_alias (__isnan, isnanl);
  236. #endif