home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / sys / sgi / 12365 < prev    next >
Encoding:
Text File  |  1992-08-13  |  9.3 KB  |  337 lines

  1. Newsgroups: comp.sys.sgi
  2. Path: sparky!uunet!stanford.edu!ames!nsisrv!climate.gsfc.nasa.gov!merritt
  3. From: merritt@climate.gsfc.nasa.gov (John H. Merritt)
  4. Subject: Re: Binary File: SGI <-> IBM
  5. Message-ID: <1992Aug13.202709.28230@nsisrv.gsfc.nasa.gov>
  6. Sender: usenet@nsisrv.gsfc.nasa.gov (Usenet)
  7. Nntp-Posting-Host: climate.gsfc.nasa.gov
  8. Organization: Climate and Radiation Branch
  9. References:  <1157@deere.com>
  10. Date: Thu, 13 Aug 1992 20:27:09 GMT
  11. Lines: 324
  12.  
  13. In article <1157@deere.com>, jrh@de.deere.com (John R. Howell) writes:
  14. |> I have a binary file created with C fwrites and read with C freads.
  15. |> 
  16. |> If I create this on an SGI (IRIX) and read it with an IBM RS6000, I seem to
  17. |> read everything ok, but my floating point numbers don't make sense. If
  18. |> I go the other way (IBM -> SGI) a similar thing happens.
  19. |> 
  20. |> Apparently the integers are read correctly ...
  21.  
  22. On the IBM, you will find that floating point numbers are not stored base 2,
  23. rather they are stored base 16.  Awh who cares, all you want are the routines
  24. that can convert each 4 or 8 byte word.
  25.  
  26. Here are two routines to get you started.  The first converts a buffer
  27. load of ibm r*4 variables to iris r*4 (bifloat) and the other (bireal8) handles
  28. r*8.  It is up to you to call these routines at the appropriate time.
  29.  
  30. I hope you don't mind Leslie.
  31.  
  32. /*.............................................................*/
  33. typedef STUFF char;
  34. int bifloat(inn,out,num)
  35.    STUFF *inn, *out;
  36.    int num;
  37. {
  38.    /* translates real*4 */
  39.  
  40.    int i;
  41.    long expont;
  42.  
  43.    for (i=0; i<num; i++)
  44.    {
  45.  
  46.      out[3] = inn[3];   /* extract and copy mantissa */
  47.      out[2] = inn[2];
  48.      out[1] = inn[1];
  49.  
  50.      out[0] = inn[0] & 0x80;  /* put the sign in */
  51.  
  52.      expont = inn[0] & 127;  /* extract exponent */
  53.      if (expont != 0)
  54.      {
  55.  
  56.         expont -= 64; /* remove the offset of 64 */
  57.         expont *= 4;  /* convert from 16-exp to 2-exp */
  58.  
  59.         /* find first non-zero byte in mantissa */
  60.         if (out[1]) 
  61.         {    /* first byte contains a bit--do nothing */
  62.         }
  63.         else if (out[2])
  64.         {    /* second byte contains a bit--move up to first byte */
  65.              out[1] = out[2];
  66.              out[2] = out[3];
  67.              out[3] = 0;
  68.              expont -= 8;
  69.         }
  70.         else if (out[1])
  71.         {    /* third byte contains a bit--move up to first byte */
  72.              out[1] = out[3];
  73.              out[2] = 0;
  74.              out[3] = 0;
  75.              expont -= 16;
  76.         }
  77.         else
  78.         {    /* none of the bytes contains a bit--we have zero */
  79.         }
  80.  
  81.         /* make sure that the highest bit in the leading byte of the 
  82.            mantissa is set */
  83.         if (out[1]) 
  84.         {
  85.            while (!(out[1] & 0x80))
  86.            {
  87.                out[1] = (out[1]<<1) | ((out[2]>>7)& 1);
  88.                out[2] = (out[2]<<1) | ((out[3]>>7)& 1);
  89.                out[3] = (out[3]<<1) ;
  90.                expont -= 1;
  91.            }
  92.  
  93.            /* Now hide the highest bit */
  94.            out[1] = out[1] & 0x7f ;
  95.            expont -= 1;
  96.         }
  97.  
  98.         /* now convert the exponent to iris format */
  99.         expont += 127;
  100.  
  101.         if ((expont == 0) && ((out[0] | out[1] | out[2] | out[3]) != 0))
  102.         {
  103.            fprintf(stderr," Illegal Floating point value in real conversion\n");
  104.            return i+1;
  105.         }
  106.         else if (expont < 0)
  107.         {
  108.            fprintf(stderr," Floating point underflow in real conversion\n");
  109.             out[3] = 0;
  110.             out[2] = 0;
  111.             out[1] = 0;
  112.             out[0] = 0;
  113.         }
  114.         else if (expont > 255) 
  115.         {
  116.             fprintf(stderr," Floating point overflow in real conversion\n");
  117.             return i+1;
  118.         }
  119.         else
  120.         {  /* ok to convert */
  121.             out[0] = out[0] | ((expont >> 1) & 0x7f);
  122.             out[1] = out[1] | (expont << 7);
  123.         }
  124.  
  125.      }
  126.      else
  127.      {
  128.             out[3] = 0;
  129.             out[2] = 0;
  130.             out[1] = 0;
  131.             out[0] = 0;
  132.      } 
  133.  
  134.      inn += 4;
  135.      out += 4;
  136.    }
  137.  
  138.    return i;
  139.  
  140. }
  141.  
  142. /*.............................................................*/
  143. int bireal8(inn,out,num)
  144.    STUFF *inn, *out;
  145.    int num;
  146. {
  147.    /* translates real*8 */
  148.    STUFF syn1, syn2;
  149.    long expont;
  150.    int i;
  151.  
  152.    for (i=0; i<num; i++)
  153.    {
  154.  
  155.      out[3] = inn[3];   /* extract and copy mantissa */
  156.      out[2] = inn[2];
  157.      out[1] = inn[1];
  158.      out[5] = inn[5];
  159.      out[4] = inn[4];
  160.      out[7] = inn[7];
  161.      out[6] = inn[6];
  162.  
  163.      out[0] = inn[0] & 0x80;  /* put the sign in */
  164.  
  165.      expont = inn[0] & 127;  /* extract exponent */
  166.      if (expont != 0)
  167.      {
  168.  
  169.         expont -= 64; /* remove the offset of 64 */
  170.         expont *= 4;  /* convert from 16-exp to 2-exp */
  171.  
  172.         /* find first non-zero byte in mantissa */
  173.         if (out[1]) 
  174.         {    /* first byte contains a bit--do nothing */
  175.         }
  176.         else if (out[2])
  177.         {    /* second byte contains a bit--move up to first byte */
  178.              out[1] = out[2];
  179.              out[2] = out[3];
  180.              out[3] = out[4];
  181.              out[4] = out[5];
  182.              out[5] = out[6];
  183.              out[6] = out[7];
  184.              out[7] = 0;
  185.              expont -= 8;
  186.         }
  187.         else if (out[3])
  188.         {    /* third byte contains a bit--move up to first byte */
  189.              out[1] = out[3];
  190.              out[2] = out[4];
  191.              out[3] = out[5];
  192.              out[4] = out[6];
  193.              out[5] = out[7];
  194.              out[6] = 0;
  195.              out[7] = 0;
  196.              expont -= 16;
  197.         }
  198.         else if (out[4])
  199.         {    /* fourth byte contains a bit--move up to first byte */
  200.              out[1] = out[4];
  201.              out[2] = out[5];
  202.              out[3] = out[6];
  203.              out[4] = out[7];
  204.              out[5] = 0;
  205.              out[6] = 0;
  206.              out[7] = 0;
  207.              expont -= 24;
  208.         }
  209.         else if (out[5])
  210.         {    /* fifth byte contains a bit--move up to first byte */
  211.              out[1] = out[5];
  212.              out[2] = out[6];
  213.              out[3] = out[7];
  214.              out[4] = 0;
  215.              out[5] = 0;
  216.              out[6] = 0;
  217.              out[7] = 0;
  218.              expont -= 32;
  219.         }
  220.         else if (out[6])
  221.         {    /* sixth byte contains a bit--move up to first byte */
  222.              out[1] = out[6];
  223.              out[2] = out[7];
  224.              out[3] = 0;
  225.              out[4] = 0;
  226.              out[5] = 0;
  227.              out[6] = 0;
  228.              out[7] = 0;
  229.              expont -= 40;
  230.         }
  231.         else if (out[7])
  232.         {    /* seventh byte contains a bit--move up to first byte */
  233.              out[1] = out[7];
  234.              out[2] = 0;
  235.              out[3] = 0;
  236.              out[4] = 0;
  237.              out[5] = 0;
  238.              out[6] = 0;
  239.              out[7] = 0;
  240.              expont -= 48;
  241.         }
  242.         else
  243.         {    /* none of the bytes contains a bit--we have zero */
  244.         }
  245.  
  246.         /* make sure that the bit 4 in the leading byte of the 
  247.            mantissa is set */
  248.         if (out[1]) 
  249.         {
  250.            while (out[1] & 0xf0)  /* slide leading bits back */
  251.            {
  252.                out[7] = ((out[7]>>1) & 0x7f) | (out[6]<<7);
  253.                out[6] = ((out[6]>>1) & 0x7f) | (out[5]<<7);
  254.                out[5] = ((out[5]>>1) & 0x7f) | (out[4]<<7);
  255.                out[4] = ((out[4]>>1) & 0x7f) | (out[3]<<7);
  256.                out[3] = ((out[3]>>1) & 0x7f) | (out[2]<<7);
  257.                out[2] = ((out[2]>>1) & 0x7f) | (out[1]<<7);
  258.                out[1] = (out[1]>>1) & 0x7f;
  259.                expont += 1;
  260.            }
  261.            while (!(out[1] & 0x10))   /* slide lower bits up */
  262.            {
  263.                out[1] = (out[1]<<1) | ((out[2]>>7)& 1);
  264.                out[2] = (out[2]<<1) | ((out[3]>>7)& 1);
  265.                out[3] = (out[3]<<1) | ((out[4]>>7)& 1);
  266.                out[4] = (out[4]<<1) | ((out[5]>>7)& 1);
  267.                out[5] = (out[5]<<1) | ((out[6]>>7)& 1);
  268.                out[6] = (out[6]<<1) | ((out[7]>>7)& 1);
  269.                out[7] = (out[7]<<1);
  270.                expont -= 1;
  271.            }
  272.  
  273.            /* Now hide the highest bit */
  274.            out[1] = out[1] & 0x0f ;
  275.            expont -= 4;
  276.         }
  277.  
  278.         /* now convert the exponent to iris format */
  279.         expont += 1023;
  280.  
  281.         if ((expont == 0) && ((out[0] | out[1] | out[2] | out[3] | out[4]
  282.         | out[5] | out[6] | out[7]) != 0))
  283.         {
  284.           fprintf(stderr," Illegal Floating point value in double conversion\n");
  285.           return i+1; 
  286.         }
  287.         else if (expont < 0)
  288.         {
  289.           fprintf(stderr," Floating point underflow in double conversion\n");
  290.           out[7] = 0;
  291.           out[6] = 0;
  292.           out[5] = 0;
  293.           out[4] = 0;
  294.           out[3] = 0;
  295.           out[2] = 0;
  296.           out[1] = 0;
  297.           out[0] = 0;
  298.         }
  299.         else if (expont > 4095) 
  300.         {
  301.           fprintf(stderr," Floating point overflow in double conversion\n");
  302.           return i+1; 
  303.         }
  304.         else
  305.         {  /* ok to convert */
  306.            out[0] = out[0] | ((expont >> 4) & 0x7f);
  307.            out[1] = out[1] | (expont << 4);
  308.         }
  309.      }
  310.      else
  311.      {
  312.           out[7] = 0;
  313.           out[6] = 0;
  314.           out[5] = 0;
  315.           out[4] = 0;
  316.           out[3] = 0;
  317.           out[2] = 0;
  318.           out[1] = 0;
  319.           out[0] = 0;
  320.      } 
  321.  
  322.       inn += 8;
  323.       out += 8;
  324.  
  325.    }
  326.  
  327.    return i;
  328.  
  329. }
  330.  
  331.  
  332. -- 
  333. \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
  334. John H. Merritt --> merritt@climate.gsfc.nasa.gov
  335. "I am generally intolerant of ignorance,
  336. but I have made an exception in your case."
  337.