home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-base.tgz / libg++-2.7.1-src.tar / fsf / libg++ / libiberty / floatformat.c < prev    next >
C/C++ Source or Header  |  1995-07-07  |  12KB  |  386 lines

  1. /* IEEE floating point support routines, for GDB, the GNU Debugger.
  2.    Copyright (C) 1991, 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
  19.  
  20. #include "floatformat.h"
  21. #include <math.h>        /* ldexp */
  22. #ifdef __STDC__
  23. #include <stddef.h>
  24. extern void *memcpy (void *s1, const void *s2, size_t n);
  25. extern void *memset (void *s, int c, size_t n);
  26. #else
  27. extern char *memcpy ();
  28. extern char *memset ();
  29. #endif
  30.  
  31. /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
  32.    going to bother with trying to muck around with whether it is defined in
  33.    a system header, what we do if not, etc.  */
  34. #define FLOATFORMAT_CHAR_BIT 8
  35.  
  36. /* floatformats for IEEE single and double, big and little endian.  */
  37. const struct floatformat floatformat_ieee_single_big =
  38. {
  39.   floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23, floatformat_intbit_no
  40. };
  41. const struct floatformat floatformat_ieee_single_little =
  42. {
  43.   floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23, floatformat_intbit_no
  44. };
  45. const struct floatformat floatformat_ieee_double_big =
  46. {
  47.   floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52, floatformat_intbit_no
  48. };
  49. const struct floatformat floatformat_ieee_double_little =
  50. {
  51.   floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52, floatformat_intbit_no
  52. };
  53.  
  54. const struct floatformat floatformat_i387_ext =
  55. {
  56.   floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
  57.   floatformat_intbit_yes
  58. };
  59. const struct floatformat floatformat_m68881_ext =
  60. {
  61.   /* Note that the bits from 16 to 31 are unused.  */
  62.   floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64, floatformat_intbit_yes
  63. };
  64. const struct floatformat floatformat_i960_ext =
  65. {
  66.   /* Note that the bits from 0 to 15 are unused.  */
  67.   floatformat_little, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
  68.   floatformat_intbit_yes
  69. };
  70. const struct floatformat floatformat_m88110_ext =
  71. {
  72. #ifdef HARRIS_FLOAT_FORMAT
  73.   /* Harris uses raw format 128 bytes long, but the number is just an ieee
  74.      double, and the last 64 bits are wasted. */
  75.   floatformat_big,128, 0, 1, 11,  0x3ff,  0x7ff, 12, 52,
  76.   floatformat_intbit_no
  77. #else
  78.   floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
  79.   floatformat_intbit_yes
  80. #endif /* HARRIS_FLOAT_FORMAT */
  81. };
  82. const struct floatformat floatformat_arm_ext =
  83. {
  84.   /* Bits 1 to 16 are unused.  */
  85.   floatformat_big, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
  86.   floatformat_intbit_yes
  87. };
  88.  
  89. static unsigned long get_field PARAMS ((unsigned char *,
  90.                     enum floatformat_byteorders,
  91.                     unsigned int,
  92.                     unsigned int,
  93.                     unsigned int));
  94.  
  95. /* Extract a field which starts at START and is LEN bytes long.  DATA and
  96.    TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
  97. static unsigned long
  98. get_field (data, order, total_len, start, len)
  99.      unsigned char *data;
  100.      enum floatformat_byteorders order;
  101.      unsigned int total_len;
  102.      unsigned int start;
  103.      unsigned int len;
  104. {
  105.   unsigned long result;
  106.   unsigned int cur_byte;
  107.   int cur_bitshift;
  108.  
  109.   /* Start at the least significant part of the field.  */
  110.   cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
  111.   if (order == floatformat_little)
  112.     cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
  113.   cur_bitshift =
  114.     ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
  115.   result = *(data + cur_byte) >> (-cur_bitshift);
  116.   cur_bitshift += FLOATFORMAT_CHAR_BIT;
  117.   if (order == floatformat_little)
  118.     ++cur_byte;
  119.   else
  120.     --cur_byte;
  121.  
  122.   /* Move towards the most significant part of the field.  */
  123.   while (cur_bitshift < len)
  124.     {
  125.       if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
  126.     /* This is the last byte; zero out the bits which are not part of
  127.        this field.  */
  128.     result |=
  129.       (*(data + cur_byte) & ((1 << (len - cur_bitshift)) - 1))
  130.         << cur_bitshift;
  131.       else
  132.     result |= *(data + cur_byte) << cur_bitshift;
  133.       cur_bitshift += FLOATFORMAT_CHAR_BIT;
  134.       if (order == floatformat_little)
  135.     ++cur_byte;
  136.       else
  137.     --cur_byte;
  138.     }
  139.   return result;
  140. }
  141.   
  142. /* Convert from FMT to a double.
  143.    FROM is the address of the extended float.
  144.    Store the double in *TO.  */
  145.  
  146. void
  147. floatformat_to_double (fmt, from, to)
  148.      const struct floatformat *fmt;
  149.      char *from;
  150.      double *to;
  151. {
  152.   unsigned char *ufrom = (unsigned char *)from;
  153.   double dto;
  154.   long exponent;
  155.   unsigned long mant;
  156.   unsigned int mant_bits, mant_off;
  157.   int mant_bits_left;
  158.  
  159.   exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
  160.             fmt->exp_start, fmt->exp_len);
  161.   /* Note that if exponent indicates a NaN, we can't really do anything useful
  162.      (not knowing if the host has NaN's, or how to build one).  So it will
  163.      end up as an infinity or something close; that is OK.  */
  164.  
  165.   mant_bits_left = fmt->man_len;
  166.   mant_off = fmt->man_start;
  167.   dto = 0.0;
  168.   exponent -= fmt->exp_bias;
  169.  
  170.   /* Build the result algebraically.  Might go infinite, underflow, etc;
  171.      who cares. */
  172.   while (mant_bits_left > 0)
  173.     {
  174.       int exp_bits;
  175.       exp_bits = mant_bits_left < 32 ? mant_bits_left : 32;
  176.       if (mant_bits_left == fmt->man_len
  177.       && exp_bits == 32
  178.       && fmt->intbit == floatformat_intbit_no)
  179.     {
  180.       /* If there is no integer bit, we need to get only 31 bits
  181.          so we have room for an integer bit that we create.  */
  182.       mant_bits = 31;
  183.     }
  184.       else
  185.     mant_bits = exp_bits;
  186.  
  187.       mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
  188.             mant_off, mant_bits);
  189.       if (mant_bits_left == fmt->man_len)
  190.     mant |= 0x80000000;
  191.       dto += ldexp ((double)mant, exponent - (exp_bits - 1));
  192.       exponent -= exp_bits;
  193.       mant_off += mant_bits;
  194.       mant_bits_left -= mant_bits;
  195.     }
  196.  
  197.   /* Negate it if negative.  */
  198.   if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
  199.     dto = -dto;
  200.   memcpy (to, &dto, sizeof (dto));
  201. }
  202.  
  203. static void put_field PARAMS ((unsigned char *, enum floatformat_byteorders,
  204.                    unsigned int,
  205.                    unsigned int,
  206.                    unsigned int,
  207.                    unsigned long));
  208.  
  209. /* Set a field which starts at START and is LEN bytes long.  DATA and
  210.    TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER.  */
  211. static void
  212. put_field (data, order, total_len, start, len, stuff_to_put)
  213.      unsigned char *data;
  214.      enum floatformat_byteorders order;
  215.      unsigned int total_len;
  216.      unsigned int start;
  217.      unsigned int len;
  218.      unsigned long stuff_to_put;
  219. {
  220.   unsigned int cur_byte;
  221.   int cur_bitshift;
  222.  
  223.   /* Start at the least significant part of the field.  */
  224.   cur_byte = (start + len) / FLOATFORMAT_CHAR_BIT;
  225.   if (order == floatformat_little)
  226.     cur_byte = (total_len / FLOATFORMAT_CHAR_BIT) - cur_byte - 1;
  227.   cur_bitshift =
  228.     ((start + len) % FLOATFORMAT_CHAR_BIT) - FLOATFORMAT_CHAR_BIT;
  229.   *(data + cur_byte) &=
  230.     ~(((1 << ((start + len) % FLOATFORMAT_CHAR_BIT)) - 1) << (-cur_bitshift));
  231.   *(data + cur_byte) |=
  232.     (stuff_to_put & ((1 << FLOATFORMAT_CHAR_BIT) - 1)) << (-cur_bitshift);
  233.   cur_bitshift += FLOATFORMAT_CHAR_BIT;
  234.   if (order == floatformat_little)
  235.     ++cur_byte;
  236.   else
  237.     --cur_byte;
  238.  
  239.   /* Move towards the most significant part of the field.  */
  240.   while (cur_bitshift < len)
  241.     {
  242.       if (len - cur_bitshift < FLOATFORMAT_CHAR_BIT)
  243.     {
  244.       /* This is the last byte.  */
  245.       *(data + cur_byte) &=
  246.         ~((1 << (len - cur_bitshift)) - 1);
  247.       *(data + cur_byte) |= (stuff_to_put >> cur_bitshift);
  248.     }
  249.       else
  250.     *(data + cur_byte) = ((stuff_to_put >> cur_bitshift)
  251.                   & ((1 << FLOATFORMAT_CHAR_BIT) - 1));
  252.       cur_bitshift += FLOATFORMAT_CHAR_BIT;
  253.       if (order == floatformat_little)
  254.     ++cur_byte;
  255.       else
  256.     --cur_byte;
  257.     }
  258. }
  259.  
  260. /* The converse: convert the double *FROM to an extended float
  261.    and store where TO points.  Neither FROM nor TO have any alignment
  262.    restrictions.  */
  263.  
  264. void
  265. floatformat_from_double (fmt, from, to)
  266.      CONST struct floatformat *fmt;
  267.      double *from;
  268.      char *to;
  269. {
  270.   double dfrom;
  271.   int exponent;
  272.   double mant;
  273.   unsigned int mant_bits, mant_off;
  274.   int mant_bits_left;
  275.   unsigned char *uto = (unsigned char *)to;
  276.  
  277.   memcpy (&dfrom, from, sizeof (dfrom));
  278.   memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
  279.   if (dfrom == 0)
  280.     return;            /* Result is zero */
  281.   if (dfrom != dfrom)
  282.     {
  283.       /* From is NaN */
  284.       put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
  285.          fmt->exp_len, fmt->exp_nan);
  286.       /* Be sure it's not infinity, but NaN value is irrel */
  287.       put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
  288.          32, 1);
  289.       return;
  290.     }
  291.  
  292.   /* If negative, set the sign bit.  */
  293.   if (dfrom < 0)
  294.     {
  295.       put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
  296.       dfrom = -dfrom;
  297.     }
  298.  
  299.   /* How to tell an infinity from an ordinary number?  FIXME-someday */
  300.  
  301.   mant = frexp (dfrom, &exponent);
  302.   put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start, fmt->exp_len,
  303.          exponent + fmt->exp_bias - 1);
  304.  
  305.   mant_bits_left = fmt->man_len;
  306.   mant_off = fmt->man_start;
  307.   while (mant_bits_left > 0)
  308.     {
  309.       unsigned long mant_long;
  310.       mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
  311.  
  312.       mant *= 4294967296.0;
  313.       mant_long = (unsigned long)mant;
  314.       mant -= mant_long;
  315.  
  316.       /* If the integer bit is implicit, then we need to discard it.
  317.      If we are discarding a zero, we should be (but are not) creating
  318.      a denormalized    number which means adjusting the exponent
  319.      (I think).  */
  320.       if (mant_bits_left == fmt->man_len
  321.       && fmt->intbit == floatformat_intbit_no)
  322.     {
  323.       mant_long &= 0x7fffffff;
  324.       mant_bits -= 1;
  325.     }
  326.       else if (mant_bits < 32)
  327.     {
  328.       /* The bits we want are in the most significant MANT_BITS bits of
  329.          mant_long.  Move them to the least significant.  */
  330.       mant_long >>= 32 - mant_bits;
  331.     }
  332.  
  333.       put_field (uto, fmt->byteorder, fmt->totalsize,
  334.          mant_off, mant_bits, mant_long);
  335.       mant_off += mant_bits;
  336.       mant_bits_left -= mant_bits;
  337.     }
  338. }
  339.  
  340.  
  341. #ifdef IEEE_DEBUG
  342.  
  343. /* This is to be run on a host which uses IEEE floating point.  */
  344.  
  345. void
  346. ieee_test (n)
  347.      double n;
  348. {
  349.   double result;
  350.   char exten[16];
  351.  
  352.   floatformat_to_double (&floatformat_ieee_double_big, &n, &result);
  353.   if (n != result)
  354.     printf ("Differ(to): %.20g -> %.20g\n", n, result);
  355.   floatformat_from_double (&floatformat_ieee_double_big, &n, &result);
  356.   if (n != result)
  357.     printf ("Differ(from): %.20g -> %.20g\n", n, result);
  358.  
  359.   floatformat_from_double (&floatformat_m68881_ext, &n, exten);
  360.   floatformat_to_double (&floatformat_m68881_ext, exten, &result);
  361.   if (n != result)
  362.     printf ("Differ(to+from): %.20g -> %.20g\n", n, result);
  363.  
  364. #if IEEE_DEBUG > 1
  365.   /* This is to be run on a host which uses 68881 format.  */
  366.   {
  367.     long double ex = *(long double *)exten;
  368.     if (ex != n)
  369.       printf ("Differ(from vs. extended): %.20g\n", n);
  370.   }
  371. #endif
  372. }
  373.  
  374. int
  375. main ()
  376. {
  377.   ieee_test (0.5);
  378.   ieee_test (256.0);
  379.   ieee_test (0.12345);
  380.   ieee_test (234235.78907234);
  381.   ieee_test (-512.0);
  382.   ieee_test (-0.004321);
  383.   return 0;
  384. }
  385. #endif
  386.