isnanl.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* isnanl()
  2. * isfinitel()
  3. * signbitl()
  4. *
  5. * Floating point IEEE special number tests
  6. *
  7. *
  8. *
  9. * SYNOPSIS:
  10. *
  11. * int signbitl(), isnanl(), isfinitel();
  12. * long double x, y;
  13. *
  14. * n = signbitl(x);
  15. * n = isnanl(x);
  16. * n = isfinitel(x);
  17. *
  18. *
  19. *
  20. * DESCRIPTION:
  21. *
  22. * These functions are part of the standard C run time library
  23. * for some but not all C compilers. The ones supplied are
  24. * written in C for IEEE arithmetic. They should
  25. * be used only if your compiler library does not already have
  26. * them.
  27. *
  28. */
  29. /*
  30. Cephes Math Library Release 2.7: June, 1998
  31. Copyright 1992, 1998 by Stephen L. Moshier
  32. */
  33. #include <math.h>
  34. /* This is defined in mconf.h. */
  35. /* #define DENORMAL 1 */
  36. #ifdef UNK
  37. /* Change UNK into something else. */
  38. #undef UNK
  39. #if BIGENDIAN
  40. #define MIEEE 1
  41. #else
  42. #define IBMPC 1
  43. #endif
  44. #endif
  45. /* Return 1 if the sign bit of x is 1, else 0. */
  46. int signbitl(x)
  47. long double x;
  48. {
  49. union
  50. {
  51. long double d;
  52. short s[6];
  53. int i[3];
  54. } u;
  55. u.d = x;
  56. if( sizeof(int) == 4 )
  57. {
  58. #ifdef IBMPC
  59. return( u.s[4] < 0 );
  60. #endif
  61. #ifdef MIEEE
  62. return( u.i[0] < 0 );
  63. #endif
  64. }
  65. else
  66. {
  67. #ifdef IBMPC
  68. return( u.s[4] < 0 );
  69. #endif
  70. #ifdef MIEEE
  71. return( u.s[0] < 0 );
  72. #endif
  73. }
  74. }
  75. /* Return 1 if x is a number that is Not a Number, else return 0. */
  76. int isnanl(x)
  77. long double x;
  78. {
  79. #ifdef NANS
  80. union
  81. {
  82. long double d;
  83. unsigned short s[6];
  84. unsigned int i[3];
  85. } u;
  86. u.d = x;
  87. if( sizeof(int) == 4 )
  88. {
  89. #ifdef IBMPC
  90. if( ((u.s[4] & 0x7fff) == 0x7fff)
  91. && (((u.i[1] & 0x7fffffff)!= 0) || (u.i[0] != 0)))
  92. return 1;
  93. #endif
  94. #ifdef MIEEE
  95. if( ((u.i[0] & 0x7fff0000) == 0x7fff0000)
  96. && (((u.i[1] & 0x7fffffff) != 0) || (u.i[2] != 0)))
  97. return 1;
  98. #endif
  99. return(0);
  100. }
  101. else
  102. { /* size int not 4 */
  103. #ifdef IBMPC
  104. if( (u.s[4] & 0x7fff) == 0x7fff)
  105. {
  106. if((u.s[3] & 0x7fff) | u.s[2] | u.s[1] | u.s[0])
  107. return(1);
  108. }
  109. #endif
  110. #ifdef MIEEE
  111. if( (u.s[0] & 0x7fff) == 0x7fff)
  112. {
  113. if((u.s[2] & 0x7fff) | u.s[3] | u.s[4] | u.s[5])
  114. return(1);
  115. }
  116. #endif
  117. return(0);
  118. } /* size int not 4 */
  119. #else
  120. /* No NANS. */
  121. return(0);
  122. #endif
  123. }
  124. /* Return 1 if x is not infinite and is not a NaN. */
  125. int isfinitel(x)
  126. long double x;
  127. {
  128. #ifdef INFINITIES
  129. union
  130. {
  131. long double d;
  132. unsigned short s[6];
  133. unsigned int i[3];
  134. } u;
  135. u.d = x;
  136. if( sizeof(int) == 4 )
  137. {
  138. #ifdef IBMPC
  139. if( (u.s[4] & 0x7fff) != 0x7fff)
  140. return 1;
  141. #endif
  142. #ifdef MIEEE
  143. if( (u.i[0] & 0x7fff0000) != 0x7fff0000)
  144. return 1;
  145. #endif
  146. return(0);
  147. }
  148. else
  149. {
  150. #ifdef IBMPC
  151. if( (u.s[4] & 0x7fff) != 0x7fff)
  152. return 1;
  153. #endif
  154. #ifdef MIEEE
  155. if( (u.s[0] & 0x7fff) != 0x7fff)
  156. return 1;
  157. #endif
  158. return(0);
  159. }
  160. #else
  161. /* No INFINITY. */
  162. return(1);
  163. #endif
  164. }