Subversion Repositories HelenOS

Rev

Rev 1965 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (C) 2005 Josef Cejka
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @defgroup sfl Softfloat
  30.  * @brief Software FPU emulation.
  31.  * @{
  32.  * @}
  33.  */
  34.  /** @addtogroup softfloat generic
  35.   * @ingroup sfl
  36.  * @brief Architecture independent parts of FPU software emulation library.
  37.  * @{
  38.  */
  39. /** @file
  40.  */
  41.  
  42. #include<softfloat.h>
  43. #include<sftypes.h>
  44.  
  45. #include<add.h>
  46. #include<sub.h>
  47. #include<mul.h>
  48. #include<div.h>
  49.  
  50. #include<conversion.h>
  51. #include<comparison.h>
  52. #include<other.h>
  53.  
  54. #include<functions.h>
  55.  
  56. /* Arithmetic functions */
  57.  
  58. float __addsf3(float a, float b)
  59. {
  60.     float32 fa, fb;
  61.     fa.f = a;
  62.     fb.f = b;
  63.     if (fa.parts.sign != fb.parts.sign) {
  64.         if (fa.parts.sign) {
  65.             fa.parts.sign = 0;
  66.             return subFloat32(fb, fa).f;
  67.         };
  68.         fb.parts.sign = 0;
  69.         return subFloat32(fa, fb).f;
  70.     }
  71.     return addFloat32(fa, fb).f;
  72. }
  73.  
  74. double __adddf3(double a, double b)
  75. {
  76.     float64 da, db;
  77.     da.d = a;
  78.     db.d = b;
  79.     if (da.parts.sign != db.parts.sign) {
  80.         if (da.parts.sign) {
  81.             da.parts.sign = 0;
  82.             return subFloat64(db, da).d;
  83.         };
  84.         db.parts.sign = 0;
  85.         return subFloat64(da, db).d;
  86.     }
  87.     return addFloat64(da, db).d;
  88. }
  89.  
  90. float __subsf3(float a, float b)
  91. {
  92.     float32 fa, fb;
  93.     fa.f = a;
  94.     fb.f = b;
  95.     if (fa.parts.sign != fb.parts.sign) {
  96.         fb.parts.sign = !fb.parts.sign;
  97.         return addFloat32(fa, fb).f;
  98.     }
  99.     return subFloat32(fa, fb).f;
  100. }
  101.  
  102. double __subdf3(double a, double b)
  103. {
  104.     float64 da, db;
  105.     da.d = a;
  106.     db.d = b;
  107.     if (da.parts.sign != db.parts.sign) {
  108.         db.parts.sign = !db.parts.sign;
  109.         return addFloat64(da, db).d;
  110.     }
  111.     return subFloat64(da, db).d;
  112. }
  113.  
  114. float __mulsf3(float a, float b)
  115. {
  116.     float32 fa, fb;
  117.     fa.f = a;
  118.     fb.f = b;
  119.     return  mulFloat32(fa, fb).f;
  120. }
  121.  
  122. double __muldf3(double a, double b)
  123. {
  124.     float64 da, db;
  125.     da.d = a;
  126.     db.d = b;
  127.     return  mulFloat64(da, db).d;
  128. }
  129.  
  130. float __divsf3(float a, float b)
  131. {
  132.     float32 fa, fb;
  133.     fa.f = a;
  134.     fb.f = b;
  135.     return  divFloat32(fa, fb).f;
  136. }
  137.  
  138. double __divdf3(double a, double b)
  139. {
  140.     float64 da, db;
  141.     da.d = a;
  142.     db.d = b;
  143.     return  divFloat64(da, db).d;
  144. }
  145.  
  146. float __negsf2(float a)
  147. {
  148.     float32 fa;
  149.     fa.f = a;
  150.     fa.parts.sign = !fa.parts.sign;
  151.     return fa.f;
  152. }
  153.  
  154. double __negdf2(double a)
  155. {
  156.     float64 fa;
  157.     fa.d = a;
  158.     fa.parts.sign = !fa.parts.sign;
  159.     return fa.d;
  160. }
  161.  
  162. /* Conversion functions */
  163.  
  164. double __extendsfdf2(float a)
  165. {
  166.     float32 fa;
  167.     fa.f = a;
  168.     return convertFloat32ToFloat64(fa).d;
  169. }
  170.  
  171. float __truncdfsf2(double a)
  172. {
  173.     float64 da;
  174.     da.d = a;
  175.     return convertFloat64ToFloat32(da).f;
  176. }
  177.  
  178. int __fixsfsi(float a)
  179. {
  180.     float32 fa;
  181.     fa.f = a;
  182.    
  183.     return float32_to_int(fa);
  184. }
  185. int __fixdfsi(double a)
  186. {
  187.     float64 da;
  188.     da.d = a;
  189.    
  190.     return float64_to_int(da);
  191. }
  192.  
  193. long __fixsfdi(float a)
  194. {
  195.     float32 fa;
  196.     fa.f = a;
  197.    
  198.     return float32_to_long(fa);
  199. }
  200. long __fixdfdi(double a)
  201. {
  202.     float64 da;
  203.     da.d = a;
  204.    
  205.     return float64_to_long(da);
  206. }
  207.  
  208. long long __fixsfti(float a)
  209. {
  210.     float32 fa;
  211.     fa.f = a;
  212.    
  213.     return float32_to_longlong(fa);
  214. }
  215. long long __fixdfti(double a)
  216. {
  217.     float64 da;
  218.     da.d = a;
  219.    
  220.     return float64_to_longlong(da);
  221. }
  222.  
  223. unsigned int __fixunssfsi(float a)
  224. {
  225.     float32 fa;
  226.     fa.f = a;
  227.    
  228.     return float32_to_uint(fa);
  229. }
  230. unsigned int __fixunsdfsi(double a)
  231. {
  232.     float64 da;
  233.     da.d = a;
  234.    
  235.     return float64_to_uint(da);
  236. }
  237.  
  238. unsigned long __fixunssfdi(float a)
  239. {
  240.     float32 fa;
  241.     fa.f = a;
  242.    
  243.     return float32_to_ulong(fa);
  244. }
  245. unsigned long __fixunsdfdi(double a)
  246. {
  247.     float64 da;
  248.     da.d = a;
  249.    
  250.     return float64_to_ulong(da);
  251. }
  252.  
  253. unsigned long long __fixunssfti(float a)
  254. {
  255.     float32 fa;
  256.     fa.f = a;
  257.    
  258.     return float32_to_ulonglong(fa);
  259. }
  260. unsigned long long __fixunsdfti(double a)
  261. {
  262.     float64 da;
  263.     da.d = a;
  264.    
  265.     return float64_to_ulonglong(da);
  266. }
  267.  
  268. float __floatsisf(int i)
  269. {
  270.     float32 fa;
  271.    
  272.     fa = int_to_float32(i);
  273.     return fa.f;
  274. }
  275. double __floatsidf(int i)
  276. {
  277.     float64 da;
  278.    
  279.     da = int_to_float64(i);
  280.     return da.d;
  281. }
  282.  
  283. float __floatdisf(long i)
  284. {
  285.     float32 fa;
  286.    
  287.     fa = long_to_float32(i);
  288.     return fa.f;
  289. }
  290. double __floatdidf(long i)
  291. {
  292.     float64 da;
  293.    
  294.     da = long_to_float64(i);
  295.     return da.d;
  296. }
  297.  
  298. float __floattisf(long long i)
  299. {
  300.     float32 fa;
  301.    
  302.     fa = longlong_to_float32(i);
  303.     return fa.f;
  304. }
  305. double __floattidf(long long i)
  306. {
  307.     float64 da;
  308.    
  309.     da = longlong_to_float64(i);
  310.     return da.d;
  311. }
  312.  
  313. float __floatunsisf(unsigned int i)
  314. {
  315.     float32 fa;
  316.    
  317.     fa = uint_to_float32(i);
  318.     return fa.f;
  319. }
  320. double __floatunsidf(unsigned int i)
  321. {
  322.     float64 da;
  323.    
  324.     da = uint_to_float64(i);
  325.     return da.d;
  326. }
  327.  
  328. float __floatundisf(unsigned long i)
  329. {
  330.     float32 fa;
  331.    
  332.     fa = ulong_to_float32(i);
  333.     return fa.f;
  334. }
  335. double __floatundidf(unsigned long i)
  336. {
  337.     float64 da;
  338.    
  339.     da = ulong_to_float64(i);
  340.     return da.d;
  341. }
  342.  
  343. float __floatuntisf(unsigned long long i)
  344. {
  345.     float32 fa;
  346.    
  347.     fa = ulonglong_to_float32(i);
  348.     return fa.f;
  349. }
  350. double __floatuntidf(unsigned long long i)
  351. {
  352.     float64 da;
  353.    
  354.     da = ulonglong_to_float64(i);
  355.     return da.d;
  356. }
  357.  
  358. /* Comparison functions */
  359. /* Comparison functions */
  360.  
  361. /* a<b .. -1
  362.  * a=b ..  0
  363.  * a>b ..  1
  364.  * */
  365.  
  366. int __cmpsf2(float a, float b)
  367. {
  368.     float32 fa, fb;
  369.     fa.f = a;
  370.     fb.f = b;
  371.     if ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ) {
  372.         return 1; /* no special constant for unordered - maybe signaled? */
  373.     };
  374.  
  375.    
  376.     if (isFloat32eq(fa, fb)) {
  377.         return 0;
  378.     };
  379.    
  380.     if (isFloat32lt(fa, fb)) {
  381.         return -1;
  382.         };
  383.     return 1;
  384. }
  385.  
  386. int __unordsf2(float a, float b)
  387. {
  388.     float32 fa, fb;
  389.     fa.f = a;
  390.     fb.f = b;
  391.     return ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) );
  392. }
  393.  
  394. /**
  395.  * @return zero, if neither argument is a NaN and are equal
  396.  * */
  397. int __eqsf2(float a, float b)
  398. {
  399.     float32 fa, fb;
  400.     fa.f = a;
  401.     fb.f = b;
  402.     if ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ) {
  403.         /* TODO: sigNaNs*/
  404.         return 1;
  405.         };
  406.     return isFloat32eq(fa, fb) - 1;
  407. }
  408.  
  409. /* strange behavior, but it was in gcc documentation */
  410. int __nesf2(float a, float b)
  411. {
  412.     return __eqsf2(a, b);
  413. }
  414.  
  415. /* return value >= 0 if a>=b and neither is NaN */
  416. int __gesf2(float a, float b)
  417. {
  418.     float32 fa, fb;
  419.     fa.f = a;
  420.     fb.f = b;
  421.     if ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ) {
  422.         /* TODO: sigNaNs*/
  423.         return -1;
  424.         };
  425.    
  426.     if (isFloat32eq(fa, fb)) {
  427.         return 0;
  428.     };
  429.    
  430.     if (isFloat32gt(fa, fb)) {
  431.         return 1;
  432.         };
  433.    
  434.     return -1;
  435. }
  436.  
  437. /** Return negative value, if a<b and neither is NaN*/
  438. int __ltsf2(float a, float b)
  439. {
  440.     float32 fa, fb;
  441.     fa.f = a;
  442.     fb.f = b;
  443.     if ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ) {
  444.         /* TODO: sigNaNs*/
  445.         return 1;
  446.         };
  447.     if (isFloat32lt(fa, fb)) {
  448.         return -1;
  449.         };
  450.     return 0;
  451. }
  452.  
  453. /* return value <= 0 if a<=b and neither is NaN */
  454. int __lesf2(float a, float b)
  455. {
  456.     float32 fa, fb;
  457.     fa.f = a;
  458.     fb.f = b;
  459.     if ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ) {
  460.         /* TODO: sigNaNs*/
  461.         return 1;
  462.         };
  463.    
  464.     if (isFloat32eq(fa, fb)) {
  465.         return 0;
  466.     };
  467.    
  468.     if (isFloat32lt(fa, fb)) {
  469.         return -1;
  470.         };
  471.    
  472.     return 1;
  473. }
  474.  
  475. /** Return positive value, if a>b and neither is NaN*/
  476. int __gtsf2(float a, float b)
  477. {
  478.     float32 fa, fb;
  479.     fa.f = a;
  480.     fb.f = b;
  481.     if ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ) {
  482.         /* TODO: sigNaNs*/
  483.         return -1;
  484.         };
  485.     if (isFloat32gt(fa, fb)) {
  486.         return 1;
  487.         };
  488.     return 0;
  489. }
  490.  
  491. /* Other functions */
  492.  
  493. float __powisf2(float a, int b)
  494. {
  495. /* TODO: */
  496.     float32 fa;
  497.     fa.binary = FLOAT32_NAN;
  498.     return fa.f;
  499. }
  500.  
  501.  
  502.  /** @}
  503.  */
  504.  
  505.