fpmacros.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 <sys/types.h>
  24. #include <math.h>
  25. #include "fp_private.h"
  26. #define SIGN_MASK 0x80000000
  27. #define NSIGN_MASK 0x7fffffff
  28. #define FEXP_MASK 0x7f800000
  29. #define FFRAC_MASK 0x007fffff
  30. /***********************************************************************
  31. int __fpclassifyf(float x) returns the classification code of the
  32. argument x, as defined in <fp.h>.
  33. Exceptions: INVALID signaled if x is a signaling NaN; in this case,
  34. the FP_QNAN code is returned.
  35. Calls: none
  36. ***********************************************************************/
  37. int __fpclassifyf ( float x )
  38. {
  39. unsigned int iexp;
  40. union {
  41. u_int32_t lval;
  42. float fval;
  43. } z;
  44. z.fval = x;
  45. iexp = z.lval & FEXP_MASK; /* isolate float exponent */
  46. if (iexp == FEXP_MASK) { /* NaN or INF case */
  47. if ((z.lval & 0x007fffff) == 0)
  48. return FP_INFINITE;
  49. return FP_NAN;
  50. }
  51. if (iexp != 0) /* normal float */
  52. return FP_NORMAL;
  53. if (x == 0.0)
  54. return FP_ZERO; /* zero */
  55. else
  56. return FP_SUBNORMAL; /* must be subnormal */
  57. }
  58. /***********************************************************************
  59. Function __fpclassify,
  60. Implementation of classify of a double number for the PowerPC.
  61. Exceptions: INVALID signaled if x is a signaling NaN; in this case,
  62. the FP_QNAN code is returned.
  63. Calls: none
  64. ***********************************************************************/
  65. int __fpclassify ( double arg )
  66. {
  67. register unsigned int exponent;
  68. union
  69. {
  70. dHexParts hex;
  71. double dbl;
  72. } x;
  73. x.dbl = arg;
  74. exponent = x.hex.high & dExpMask;
  75. if ( exponent == dExpMask )
  76. {
  77. if ( ( ( x.hex.high & dHighMan ) | x.hex.low ) == 0 )
  78. return FP_INFINITE;
  79. else
  80. return FP_NAN;
  81. }
  82. else if ( exponent != 0)
  83. return FP_NORMAL;
  84. else {
  85. if ( arg == 0.0 )
  86. return FP_ZERO;
  87. else
  88. return FP_SUBNORMAL;
  89. }
  90. }
  91. /***********************************************************************
  92. int __isnormalf(float x) returns nonzero if and only if x is a
  93. normalized float number and zero otherwise.
  94. Exceptions: INVALID is raised if x is a signaling NaN; in this case,
  95. zero is returned.
  96. Calls: none
  97. ***********************************************************************/
  98. int __isnormalf ( float x )
  99. {
  100. unsigned int iexp;
  101. union {
  102. u_int32_t lval;
  103. float fval;
  104. } z;
  105. z.fval = x;
  106. iexp = z.lval & FEXP_MASK; /* isolate float exponent */
  107. return ((iexp != FEXP_MASK) && (iexp != 0));
  108. }
  109. int __isnormal ( double x )
  110. {
  111. return ( __fpclassify ( x ) == FP_NORMAL );
  112. }
  113. /***********************************************************************
  114. int __isfinitef(float x) returns nonzero if and only if x is a
  115. finite (normal, subnormal, or zero) float number and zero otherwise.
  116. Exceptions: INVALID is raised if x is a signaling NaN; in this case,
  117. zero is returned.
  118. Calls: none
  119. ***********************************************************************/
  120. int __finitef ( float x )
  121. {
  122. union {
  123. u_int32_t lval;
  124. float fval;
  125. } z;
  126. z.fval = x;
  127. return ((z.lval & FEXP_MASK) != FEXP_MASK);
  128. }
  129. weak_alias (__finitef, finitef)
  130. #if 0 /* use __finite in s_finite.c */
  131. int __finite ( double x )
  132. {
  133. return ( __fpclassify ( x ) >= FP_ZERO );
  134. }
  135. weak_alias (__finite, finite)
  136. #endif
  137. /***********************************************************************
  138. int __signbitf(float x) returns nonzero if and only if the sign
  139. bit of x is set and zero otherwise.
  140. Exceptions: INVALID is raised if x is a signaling NaN.
  141. Calls: none
  142. ***********************************************************************/
  143. int __signbitf ( float x )
  144. {
  145. union {
  146. u_int32_t lval;
  147. float fval;
  148. } z;
  149. z.fval = x;
  150. return ((z.lval & SIGN_MASK) != 0);
  151. }
  152. /***********************************************************************
  153. Function sign of a double.
  154. Implementation of sign bit for the PowerPC.
  155. Calls: none
  156. ***********************************************************************/
  157. int __signbit ( double arg )
  158. {
  159. union
  160. {
  161. dHexParts hex;
  162. double dbl;
  163. } x;
  164. int sign;
  165. x.dbl = arg;
  166. sign = ( ( x.hex.high & dSgnMask ) == dSgnMask ) ? 1 : 0;
  167. return sign;
  168. }
  169. /***********************************************************************
  170. * int __isinff(float x) returns -1 if value represents negative
  171. * infinity, 1 if value represents positive infinity,
  172. * and 0 otherwise.
  173. *
  174. * Calls: __signbit
  175. * +***********************************************************************/
  176. int __isinff ( float x )
  177. {
  178. int class = __fpclassifyf(x);
  179. if ( class == FP_INFINITE ) {
  180. return ( (__signbitf(x)) ? -1 : 1);
  181. }
  182. return 0;
  183. }
  184. weak_alias (__isinff, isinff)
  185. int __isinf ( double x )
  186. {
  187. int class = __fpclassify(x);
  188. if ( class == FP_INFINITE ) {
  189. return ( (__signbit(x)) ? -1 : 1);
  190. }
  191. return 0;
  192. }
  193. weak_alias (__isinf, isinf)
  194. #if 0
  195. int __isinfl ( long double x )
  196. {
  197. int class = __fpclassify(x);
  198. if ( class == FP_INFINITE ) {
  199. return ( (__signbit(x)) ? -1 : 1);
  200. }
  201. return 0;
  202. }
  203. weak_alias (__isinfl, isinfl);
  204. #endif
  205. /***********************************************************************
  206. int __isnanf(float x) returns nonzero if and only if x is a
  207. NaN and zero otherwise.
  208. Exceptions: INVALID is raised if x is a signaling NaN; in this case,
  209. nonzero is returned.
  210. Calls: none
  211. ***********************************************************************/
  212. int __isnanf ( float x )
  213. {
  214. union {
  215. u_int32_t lval;
  216. float fval;
  217. } z;
  218. z.fval = x;
  219. return (((z.lval&FEXP_MASK) == FEXP_MASK) && ((z.lval&FFRAC_MASK) != 0));
  220. }
  221. weak_alias (__isnanf, isnanf);
  222. int __isnan ( double x )
  223. {
  224. int class = __fpclassify(x);
  225. return ( class == FP_NAN );
  226. }
  227. weak_alias (__isnan, isnan);
  228. #if 0
  229. int __isnanl ( long double x )
  230. {
  231. int class = __fpclassify(x);
  232. return ( class == FP_NAN );
  233. }
  234. weak_alias (__isnanl, isnanl);
  235. #endif