Subversion Repositories HelenOS-historic

Rev

Rev 542 | Rev 647 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. /*
  3.  * Copyright (C) 2005 Josef Cejka
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  * - Redistributions in binary form must reproduce the above copyright
  13.  *   notice, this list of conditions and the following disclaimer in the
  14.  *   documentation and/or other materials provided with the distribution.
  15.  * - The name of the author may not be used to endorse or promote products
  16.  *   derived from this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28.  */
  29.  
  30. #include<softfloat.h>
  31.  
  32. float32 addFloat32(float32 a, float32 b);
  33. float32 subFloat32(float32 a, float32 b);
  34. inline int isFloat32NaN(float32 f);
  35. inline int isFloat32SigNaN(float32 f);
  36.  
  37. float __addsf3(float a, float b)
  38. {
  39.     float32 fa, fb;
  40.     fa.f=a;
  41.     fb.f=b;
  42.     if (fa.parts.sign!=fb.parts.sign) return subFloat32(fa,fb).f;
  43.     return addFloat32(fa,fb).f;
  44. };
  45.  
  46. float __subsf3(float a, float b)
  47. {
  48.     float32 fa, fb;
  49.     fa.f=a;
  50.     fb.f=b;
  51.     if (fa.parts.sign!=fb.parts.sign) return addFloat32(fa,fb).f;
  52.     return subFloat32(fa,fb).f;
  53. };
  54.  
  55. float __negsf2(float a)
  56. {
  57.     float32 fa;
  58.     fa.f=a;
  59.     fa.parts.sign=!fa.parts.sign;
  60.     return fa.f;
  61. };
  62.  
  63. double __negdf2(double a)
  64. {
  65.     float64 fa;
  66.     fa.d=a;
  67.     fa.parts.sign=!fa.parts.sign;
  68.     return fa.d;
  69. };
  70.  
  71. /** Add two Float32 numbers with same signs
  72.  */
  73. float32 addFloat32(float32 a, float32 b)
  74. {
  75.     int expdiff;
  76.     __u32 exp1,exp2,mant1,mant2;
  77.    
  78.     expdiff=a.parts.exp - b.parts.exp;
  79.     if (expdiff<0) {
  80.         if (isFloat32NaN(b)) {
  81.             //TODO: fix SigNaN
  82.             if (isFloat32SigNaN(b)) {
  83.             };
  84.             return b;
  85.         };
  86.        
  87.         if (b.parts.exp==0xFF) {
  88.             return b;
  89.         }
  90.        
  91.         mant1=b.parts.mantisa;
  92.         exp1=b.parts.exp;
  93.         mant2=a.parts.mantisa;
  94.         exp2=a.parts.exp;
  95.         expdiff*=-1;
  96.     } else {
  97.         if (isFloat32NaN(a)) {
  98.             //TODO: fix SigNaN
  99.             if ((isFloat32SigNaN(a))||(isFloat32SigNaN(b))) {
  100.             };
  101.             return a;
  102.         };
  103.        
  104.         if (a.parts.exp==0xFF) {
  105.             return a;
  106.         }
  107.        
  108.         mant1=a.parts.mantisa;
  109.         exp1=a.parts.exp;
  110.         mant2=b.parts.mantisa;
  111.         exp2=b.parts.exp;
  112.     };
  113.    
  114.     if (exp1==0) {
  115.         //both are denormalized
  116.         mant1+=mant2;
  117.         if (mant1&0xF00000) {
  118.             a.parts.exp=1;
  119.         };
  120.         a.parts.mantisa=mant1;
  121.         return a;
  122.     };
  123.    
  124.     // create some space for rounding
  125.     mant1<<=6;
  126.     mant2<<=6;
  127.    
  128.     mant1|=0x20000000; //add hidden bit
  129.    
  130.    
  131.     if (exp2==0) {
  132.         --expdiff; 
  133.     } else {
  134.         mant2|=0x20000000; //hidden bit
  135.     };
  136.    
  137.     mant2>>=expdiff;
  138.     mant1+=mant2;
  139. done:
  140.     if (mant1&0x40000000) {
  141.         ++exp1;
  142.         mant1>>=1;
  143.     };
  144.    
  145.     //rounding - if first bit after mantisa is set then round up
  146.     mant1+=0x20;
  147.    
  148.     a.parts.exp=exp1;
  149.     a.parts.mantisa=mant1>>6;
  150.     return a;
  151. };
  152.  
  153. /** Substract two float32 numbers with same signs
  154.  */
  155. float32 subFloat32(float32 a, float32 b)
  156. {
  157.    
  158.    
  159. };
  160.  
  161. inline int isFloat32NaN(float32 f)
  162. {   /* NaN : exp = 0xff and nonzero mantisa */
  163.     return ((f.parts.exp==0xFF)&&(f.parts.mantisa));
  164. };
  165.  
  166. inline int isFloat32SigNaN(float32 f)
  167. {   /* SigNaN : exp = 0xff mantisa = 1xxxxx..x (binary), where at least one x is nonzero */
  168.     return ((f.parts.exp==0xFF)&&(f.parts.mantisa>0x400000));
  169. };
  170.  
  171.