home *** CD-ROM | disk | FTP | other *** search
/ RISCWORLD 7 / RISCWORLD_VOL7.iso / Software / Issue2 / SDL.ARC / !unixlib / source / clib / unixlib / h / math < prev    next >
Encoding:
Text File  |  2004-09-05  |  5.9 KB  |  250 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /usr/local/cvsroot/gccsdk/unixlib/source/clib/unixlib/math.h,v $
  4.  * $Date: 2004/06/12 08:59:47 $
  5.  * $Revision: 1.4 $
  6.  * $State: Exp $
  7.  * $Author: peter $
  8.  *
  9.  ***************************************************************************/
  10.  
  11. /*
  12.  * ====================================================
  13.  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  14.  *
  15.  * Developed at SunPro, a Sun Microsystems, Inc. business.
  16.  * Permission to use, copy, modify, and distribute this
  17.  * software is freely granted, provided that this notice
  18.  * is preserved.
  19.  * ====================================================
  20.  */
  21.  
  22. #ifndef __UNIXLIB_MATH_H
  23. #define __UNIXLIB_MATH_H
  24.  
  25. #ifndef __UNIXLIB_TYPES_H
  26. #include <unixlib/types.h>
  27. #endif
  28.  
  29. /* The original fdlibm code used statements like:
  30.     n0 = ((*(int*)&one)>>29)^1;        * index of high word *
  31.     ix0 = *(n0+(int*)&x);            * high word of x *
  32.     ix1 = *((1-n0)+(int*)&x);        * low word of x *
  33.    to dig two 32 bit words out of the 64 bit IEEE floating point
  34.    value.  That is non-ANSI, and, moreover, the gcc instruction
  35.    scheduler gets it wrong.  We instead use the following macros.
  36.    Unlike the original code, we determine the endianness at compile
  37.    time, not at run time; I don't see much benefit to selecting
  38.    endianness at run time.  */
  39.  
  40. /* A union which permits us to convert between a double and two 32 bit
  41.    ints.  */
  42.  
  43.  
  44. typedef union
  45. {
  46.   double value;
  47.   struct
  48.   {
  49.     __uint32_t msw;
  50.     __uint32_t lsw;
  51.   } parts;
  52. } ieee_double_shape_type;
  53.  
  54. /* Get two 32 bit ints from a double.  */
  55.  
  56. #define EXTRACT_WORDS(ix0,ix1,d)                \
  57. do {                                \
  58.   ieee_double_shape_type ew_u;                    \
  59.   ew_u.value = (d);                        \
  60.   (ix0) = ew_u.parts.msw;                    \
  61.   (ix1) = ew_u.parts.lsw;                    \
  62. } while (0)
  63.  
  64. /* Get the more significant 32 bit int from a double.  */
  65.  
  66. #define GET_HIGH_WORD(i,d)                    \
  67. do {                                \
  68.   ieee_double_shape_type gh_u;                    \
  69.   gh_u.value = (d);                        \
  70.   (i) = gh_u.parts.msw;                        \
  71. } while (0)
  72.  
  73. /* Get the less significant 32 bit int from a double.  */
  74.  
  75. #define GET_LOW_WORD(i,d)                    \
  76. do {                                \
  77.   ieee_double_shape_type gl_u;                    \
  78.   gl_u.value = (d);                        \
  79.   (i) = gl_u.parts.lsw;                        \
  80. } while (0)
  81.  
  82. /* Set a double from two 32 bit ints.  */
  83.  
  84. #define INSERT_WORDS(d,ix0,ix1)                    \
  85. do {                                \
  86.   ieee_double_shape_type iw_u;                    \
  87.   iw_u.parts.msw = (ix0);                    \
  88.   iw_u.parts.lsw = (ix1);                    \
  89.   (d) = iw_u.value;                        \
  90. } while (0)
  91.  
  92. /* Set the more significant 32 bits of a double from an int.  */
  93.  
  94. #define SET_HIGH_WORD(d,v)                    \
  95. do {                                \
  96.   ieee_double_shape_type sh_u;                    \
  97.   sh_u.value = (d);                        \
  98.   sh_u.parts.msw = (v);                        \
  99.   (d) = sh_u.value;                        \
  100. } while (0)
  101.  
  102. /* Set the less significant 32 bits of a double from an int.  */
  103.  
  104. #define SET_LOW_WORD(d,v)                    \
  105. do {                                \
  106.   ieee_double_shape_type sl_u;                    \
  107.   sl_u.value = (d);                        \
  108.   sl_u.parts.lsw = (v);                        \
  109.   (d) = sl_u.value;                        \
  110. } while (0)
  111.  
  112. /* A union which permits us to convert between a float and a 32 bit
  113.    int.  */
  114.  
  115. typedef union
  116. {
  117.   float value;
  118.   __uint32_t word;
  119. } ieee_float_shape_type;
  120.  
  121. /* Get a 32 bit int from a float.  */
  122.  
  123. #define GET_FLOAT_WORD(i,d)                    \
  124. do {                                \
  125.   ieee_float_shape_type gf_u;                    \
  126.   gf_u.value = (d);                        \
  127.   (i) = gf_u.word;                        \
  128. } while (0)
  129.  
  130. /* Set a float from a 32 bit int.  */
  131.  
  132. #define SET_FLOAT_WORD(d,i)                    \
  133. do {                                \
  134.   ieee_float_shape_type sf_u;                    \
  135.   sf_u.word = (i);                        \
  136.   (d) = sf_u.value;                        \
  137. } while (0)
  138.  
  139. /* A union which permits us to convert between a long double and
  140.    three 32 bit ints.  */
  141.  
  142. typedef union
  143. {
  144.   long double value;
  145.   struct
  146.   {
  147.     unsigned int exponent:15;
  148.     unsigned int empty:16;
  149.     unsigned int sign:1;
  150.     __uint32_t msw;
  151.     __uint32_t lsw;
  152.   } parts;
  153. } ieee_long_double_shape_type;
  154.  
  155. /* Get three 32 bit ints from a double. Hacked for ARM FPA.  */
  156.  
  157. #define GET_LDOUBLE_WORDS(exp,ix0,ix1,d)            \
  158. do {                                \
  159.   ieee_long_double_shape_type ew_u;                \
  160.   ew_u.value = (d);                        \
  161.   (exp) = ew_u.parts.exponent | (ew_u.parts.sign << 16);    \
  162.   (ix0) = ew_u.parts.msw;                    \
  163.   (ix1) = ew_u.parts.lsw;                    \
  164. } while (0)
  165.  
  166. /* Set a double from two 32 bit ints. Hacked for ARM FPA.  */
  167.  
  168. #define SET_LDOUBLE_WORDS(d,exp,ix0,ix1)            \
  169. do {                                \
  170.   ieee_long_double_shape_type iw_u;                \
  171.   iw_u.parts.sign = (exp) & (1 << 16);                \
  172.   iw_u.parts.exponent = (exp) & ((1 << 15)-1)            \
  173.   iw_u.parts.msw = (ix0);                    \
  174.   iw_u.parts.lsw = (ix1);                    \
  175.   (d) = iw_u.value;                        \
  176. } while (0)
  177.  
  178.  
  179. /* Get the more significant 32 bits of a long double mantissa.  */
  180.  
  181. #define GET_LDOUBLE_MSW(v,d)                    \
  182. do {                                \
  183.   ieee_long_double_shape_type sh_u;                \
  184.   sh_u.value = (d);                        \
  185.   (v) = sh_u.parts.msw;                        \
  186. } while (0)
  187.  
  188. /* Set the more significant 32 bits of a long double mantissa from an int.  */
  189.  
  190. #define SET_LDOUBLE_MSW(d,v)                    \
  191. do {                                \
  192.   ieee_long_double_shape_type sh_u;                \
  193.   sh_u.value = (d);                        \
  194.   sh_u.parts.msw = (v);                        \
  195.   (d) = sh_u.value;                        \
  196. } while (0)
  197.  
  198. /* Get int from the exponent of a long double.  */
  199.  
  200. #define GET_LDOUBLE_EXP(exp,d)                    \
  201. do {                                \
  202.   ieee_long_double_shape_type ge_u;                \
  203.   ge_u.value = (d);                        \
  204.   (exp) = ge_u.parts.sign_exponent;                \
  205. } while (0)
  206.  
  207. /* Set exponent of a long double from an int.  */
  208.  
  209. #define SET_LDOUBLE_EXP(d,exp)                    \
  210. do {                                \
  211.   ieee_long_double_shape_type se_u;                \
  212.   se_u.value = (d);                        \
  213.   se_u.parts.sign_exponent = (exp);                \
  214.   (d) = se_u.value;                        \
  215. } while (0)
  216.  
  217. #if 0
  218. #if defined(__GNUC__) && defined(__OPTIMIZE__)
  219. extern __inline__ int signbit (double x)
  220. {
  221.   __int32_t hx;
  222.  
  223.   GET_HIGH_WORD (hx, x);
  224.   return hx & 0x80000000;
  225. }
  226.  
  227. extern __inline__ double copysign (double x, double y)
  228. {
  229.   __uint32_t hx,hy;
  230.  
  231.   GET_HIGH_WORD(hx,x);
  232.   GET_HIGH_WORD(hy,y);
  233.   SET_HIGH_WORD(x, (hx & 0x7fffffff) | (hy & 0x80000000));
  234.   return x;
  235. }
  236.  
  237. extern __inline__ int finite (double x)
  238. {
  239.   __int32_t hx;
  240.  
  241.   GET_HIGH_WORD(hx,x);
  242.  
  243.    return (int) ((__uint32_t) ((hx & 0x7fffffff) - 0x7ff00000) >> 31);
  244. }
  245.  
  246. #endif
  247. #endif
  248.  
  249. #endif
  250.