fpmacros.c 7.2 KB

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