fpmacros.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. int __isnormalf ( float x )
  104. {
  105. unsigned int iexp;
  106. union {
  107. u_int32_t lval;
  108. float fval;
  109. } z;
  110. z.fval = x;
  111. iexp = z.lval & FEXP_MASK; /* isolate float exponent */
  112. return ((iexp != FEXP_MASK) && (iexp != 0));
  113. }
  114. int __isnormal ( double x );
  115. int __isnormal ( double x )
  116. {
  117. return ( __fpclassify ( x ) == FP_NORMAL );
  118. }
  119. /***********************************************************************
  120. int __isfinitef(float x) returns nonzero if and only if x is a
  121. finite (normal, subnormal, or zero) float number and zero otherwise.
  122. Exceptions: INVALID is raised if x is a signaling NaN; in this case,
  123. zero is returned.
  124. Calls: none
  125. ***********************************************************************/
  126. int __finitef ( float x )
  127. {
  128. union {
  129. u_int32_t lval;
  130. float fval;
  131. } z;
  132. z.fval = x;
  133. return ((z.lval & FEXP_MASK) != FEXP_MASK);
  134. }
  135. strong_alias(__finitef,finitef)
  136. #if 0 /* use __finite in s_finite.c */
  137. int __finite ( double x )
  138. {
  139. return ( __fpclassify ( x ) >= FP_ZERO );
  140. }
  141. strong_alias(__finite,finite)
  142. #endif
  143. /***********************************************************************
  144. int __signbitf(float x) returns nonzero if and only if the sign
  145. bit of x is set and zero otherwise.
  146. Exceptions: INVALID is raised if x is a signaling NaN.
  147. Calls: none
  148. ***********************************************************************/
  149. libm_hidden_proto(__signbitf)
  150. int __signbitf ( float x )
  151. {
  152. union {
  153. u_int32_t lval;
  154. float fval;
  155. } z;
  156. z.fval = x;
  157. return ((z.lval & SIGN_MASK) != 0);
  158. }
  159. libm_hidden_def(__signbitf)
  160. /***********************************************************************
  161. Function sign of a double.
  162. Implementation of sign bit for the PowerPC.
  163. Calls: none
  164. ***********************************************************************/
  165. libm_hidden_proto(__signbit)
  166. int __signbit ( double arg )
  167. {
  168. union
  169. {
  170. dHexParts hex;
  171. double dbl;
  172. } x;
  173. int sign;
  174. x.dbl = arg;
  175. sign = ( ( x.hex.high & dSgnMask ) == dSgnMask ) ? 1 : 0;
  176. return sign;
  177. }
  178. libm_hidden_def(__signbit)
  179. /***********************************************************************
  180. * int __isinff(float x) returns -1 if value represents negative
  181. * infinity, 1 if value represents positive infinity,
  182. * and 0 otherwise.
  183. *
  184. * Calls: __signbit
  185. * +***********************************************************************/
  186. int __isinff ( float x )
  187. {
  188. int class = __fpclassifyf(x);
  189. if ( class == FP_INFINITE ) {
  190. return ( (__signbitf(x)) ? -1 : 1);
  191. }
  192. return 0;
  193. }
  194. strong_alias(__isinff,isinff)
  195. int __isinf ( 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. strong_alias(__isinf,isinf)
  204. #if 0
  205. int __isinfl ( long double x )
  206. {
  207. int class = __fpclassify(x);
  208. if ( class == FP_INFINITE ) {
  209. return ( (__signbit(x)) ? -1 : 1);
  210. }
  211. return 0;
  212. }
  213. strong_alias(__isinfl,isinfl)
  214. #endif
  215. /***********************************************************************
  216. int __isnanf(float x) returns nonzero if and only if x is a
  217. NaN and zero otherwise.
  218. Exceptions: INVALID is raised if x is a signaling NaN; in this case,
  219. nonzero is returned.
  220. Calls: none
  221. ***********************************************************************/
  222. int __isnanf ( float x )
  223. {
  224. union {
  225. u_int32_t lval;
  226. float fval;
  227. } z;
  228. z.fval = x;
  229. return (((z.lval&FEXP_MASK) == FEXP_MASK) && ((z.lval&FFRAC_MASK) != 0));
  230. }
  231. strong_alias(__isnanf,isnanf)
  232. libm_hidden_proto(__isnan)
  233. int __isnan ( double x )
  234. {
  235. int class = __fpclassify(x);
  236. return ( class == FP_NAN );
  237. }
  238. libm_hidden_def(__isnan)
  239. strong_alias(__isnan,isnan)
  240. #if 0
  241. int __isnanl ( long double x )
  242. {
  243. int class = __fpclassify(x);
  244. return ( class == FP_NAN );
  245. }
  246. strong_alias(__isnanl,isnanl)
  247. #endif