softfloat.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2005 Josef Cejka
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00037 #include<softfloat.h>
00038 #include<sftypes.h>
00039 
00040 #include<add.h>
00041 #include<sub.h>
00042 #include<mul.h>
00043 #include<div.h>
00044 
00045 #include<conversion.h>
00046 #include<comparison.h>
00047 #include<other.h>
00048 
00049 #include<functions.h>
00050 
00051 /* Arithmetic functions */
00052 
00053 float __addsf3(float a, float b)
00054 {
00055         float32 fa, fb;
00056         fa.f = a;
00057         fb.f = b;
00058         if (fa.parts.sign != fb.parts.sign) {
00059                 if (fa.parts.sign) {
00060                         fa.parts.sign = 0;
00061                         return subFloat32(fb, fa).f;
00062                 };
00063                 fb.parts.sign = 0;
00064                 return subFloat32(fa, fb).f;
00065         }
00066         return addFloat32(fa, fb).f;
00067 }
00068 
00069 double __adddf3(double a, double b)
00070 {
00071         float64 da, db;
00072         da.d = a;
00073         db.d = b;
00074         if (da.parts.sign != db.parts.sign) {
00075                 if (da.parts.sign) {
00076                         da.parts.sign = 0;
00077                         return subFloat64(db, da).d;
00078                 };
00079                 db.parts.sign = 0;
00080                 return subFloat64(da, db).d;
00081         }
00082         return addFloat64(da, db).d;
00083 }
00084 
00085 float __subsf3(float a, float b)
00086 {
00087         float32 fa, fb;
00088         fa.f = a;
00089         fb.f = b;
00090         if (fa.parts.sign != fb.parts.sign) {
00091                 fb.parts.sign = !fb.parts.sign;
00092                 return addFloat32(fa, fb).f;
00093         }
00094         return subFloat32(fa, fb).f;
00095 }
00096 
00097 double __subdf3(double a, double b)
00098 {
00099         float64 da, db;
00100         da.d = a;
00101         db.d = b;
00102         if (da.parts.sign != db.parts.sign) {
00103                 db.parts.sign = !db.parts.sign;
00104                 return addFloat64(da, db).d;
00105         }
00106         return subFloat64(da, db).d;
00107 }
00108 
00109 float __mulsf3(float a, float b) 
00110 {
00111         float32 fa, fb;
00112         fa.f = a;
00113         fb.f = b;
00114         return  mulFloat32(fa, fb).f;
00115 }
00116 
00117 double __muldf3(double a, double b) 
00118 {
00119         float64 da, db;
00120         da.d = a;
00121         db.d = b;
00122         return  mulFloat64(da, db).d;
00123 }
00124 
00125 float __divsf3(float a, float b) 
00126 {
00127         float32 fa, fb;
00128         fa.f = a;
00129         fb.f = b;
00130         return  divFloat32(fa, fb).f;
00131 }
00132 
00133 double __divdf3(double a, double b) 
00134 {
00135         float64 da, db;
00136         da.d = a;
00137         db.d = b;
00138         return  divFloat64(da, db).d;
00139 }
00140 
00141 float __negsf2(float a)
00142 {
00143         float32 fa;
00144         fa.f = a;
00145         fa.parts.sign = !fa.parts.sign;
00146         return fa.f;
00147 }
00148 
00149 double __negdf2(double a)
00150 {
00151         float64 fa;
00152         fa.d = a;
00153         fa.parts.sign = !fa.parts.sign;
00154         return fa.d;
00155 }
00156 
00157 /* Conversion functions */
00158 
00159 double __extendsfdf2(float a) 
00160 {
00161         float32 fa;
00162         fa.f = a;
00163         return convertFloat32ToFloat64(fa).d;
00164 }
00165 
00166 float __truncdfsf2(double a) 
00167 {
00168         float64 da;
00169         da.d = a;
00170         return convertFloat64ToFloat32(da).f;
00171 }
00172 
00173 int __fixsfsi(float a)
00174 {
00175         float32 fa;
00176         fa.f = a;
00177         
00178         return float32_to_int(fa);
00179 }
00180 int __fixdfsi(double a)
00181 {
00182         float64 da;
00183         da.d = a;
00184         
00185         return float64_to_int(da);
00186 }
00187  
00188 long __fixsfdi(float a)
00189 {
00190         float32 fa;
00191         fa.f = a;
00192         
00193         return float32_to_long(fa);
00194 }
00195 long __fixdfdi(double a)
00196 {
00197         float64 da;
00198         da.d = a;
00199         
00200         return float64_to_long(da);
00201 }
00202  
00203 long long __fixsfti(float a)
00204 {
00205         float32 fa;
00206         fa.f = a;
00207         
00208         return float32_to_longlong(fa);
00209 }
00210 long long __fixdfti(double a)
00211 {
00212         float64 da;
00213         da.d = a;
00214         
00215         return float64_to_longlong(da);
00216 }
00217 
00218 unsigned int __fixunssfsi(float a)
00219 {
00220         float32 fa;
00221         fa.f = a;
00222         
00223         return float32_to_uint(fa);
00224 }
00225 unsigned int __fixunsdfsi(double a)
00226 {
00227         float64 da;
00228         da.d = a;
00229         
00230         return float64_to_uint(da);
00231 }
00232  
00233 unsigned long __fixunssfdi(float a)
00234 {
00235         float32 fa;
00236         fa.f = a;
00237         
00238         return float32_to_ulong(fa);
00239 }
00240 unsigned long __fixunsdfdi(double a)
00241 {
00242         float64 da;
00243         da.d = a;
00244         
00245         return float64_to_ulong(da);
00246 }
00247  
00248 unsigned long long __fixunssfti(float a)
00249 {
00250         float32 fa;
00251         fa.f = a;
00252         
00253         return float32_to_ulonglong(fa);
00254 }
00255 unsigned long long __fixunsdfti(double a)
00256 {
00257         float64 da;
00258         da.d = a;
00259         
00260         return float64_to_ulonglong(da);
00261 }
00262  
00263 float __floatsisf(int i)
00264 {
00265         float32 fa;
00266         
00267         fa = int_to_float32(i);
00268         return fa.f;
00269 }
00270 double __floatsidf(int i)
00271 {
00272         float64 da;
00273         
00274         da = int_to_float64(i);
00275         return da.d;
00276 }
00277  
00278 float __floatdisf(long i)
00279 {
00280         float32 fa;
00281         
00282         fa = long_to_float32(i);
00283         return fa.f;
00284 }
00285 double __floatdidf(long i)
00286 {
00287         float64 da;
00288         
00289         da = long_to_float64(i);
00290         return da.d;
00291 }
00292  
00293 float __floattisf(long long i)
00294 {
00295         float32 fa;
00296         
00297         fa = longlong_to_float32(i);
00298         return fa.f;
00299 }
00300 double __floattidf(long long i)
00301 {
00302         float64 da;
00303         
00304         da = longlong_to_float64(i);
00305         return da.d;
00306 }
00307 
00308 float __floatunsisf(unsigned int i)
00309 {
00310         float32 fa;
00311         
00312         fa = uint_to_float32(i);
00313         return fa.f;
00314 }
00315 double __floatunsidf(unsigned int i)
00316 {
00317         float64 da;
00318         
00319         da = uint_to_float64(i);
00320         return da.d;
00321 }
00322  
00323 float __floatundisf(unsigned long i)
00324 {
00325         float32 fa;
00326         
00327         fa = ulong_to_float32(i);
00328         return fa.f;
00329 }
00330 double __floatundidf(unsigned long i)
00331 {
00332         float64 da;
00333         
00334         da = ulong_to_float64(i);
00335         return da.d;
00336 }
00337  
00338 float __floatuntisf(unsigned long long i)
00339 {
00340         float32 fa;
00341         
00342         fa = ulonglong_to_float32(i);
00343         return fa.f;
00344 }
00345 double __floatuntidf(unsigned long long i)
00346 {
00347         float64 da;
00348         
00349         da = ulonglong_to_float64(i);
00350         return da.d;
00351 }
00352 
00353 /* Comparison functions */
00354 /* Comparison functions */
00355 
00356 /* a<b .. -1
00357  * a=b ..  0
00358  * a>b ..  1
00359  * */
00360 
00361 int __cmpsf2(float a, float b) 
00362 {
00363         float32 fa, fb;
00364         fa.f = a;
00365         fb.f = b;
00366         if ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ) {
00367                 return 1; /* no special constant for unordered - maybe signaled? */
00368         };
00369 
00370         
00371         if (isFloat32eq(fa, fb)) {
00372                 return 0;
00373         };
00374         
00375         if (isFloat32lt(fa, fb)) {
00376                 return -1;
00377                 };
00378         return 1;
00379 }
00380 
00381 int __unordsf2(float a, float b) 
00382 {
00383         float32 fa, fb;
00384         fa.f = a;
00385         fb.f = b;
00386         return ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) );
00387 }
00388 
00392 int __eqsf2(float a, float b) 
00393 {
00394         float32 fa, fb;
00395         fa.f = a;
00396         fb.f = b;
00397         if ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ) {
00398                 /* TODO: sigNaNs*/
00399                 return 1;
00400                 };
00401         return isFloat32eq(fa, fb) - 1;
00402 }
00403 
00404 /* strange behavior, but it was in gcc documentation */
00405 int __nesf2(float a, float b) 
00406 {
00407         return __eqsf2(a, b);
00408 }
00409 
00410 /* return value >= 0 if a>=b and neither is NaN */
00411 int __gesf2(float a, float b)
00412 {
00413         float32 fa, fb;
00414         fa.f = a;
00415         fb.f = b;
00416         if ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ) {
00417                 /* TODO: sigNaNs*/
00418                 return -1;
00419                 };
00420         
00421         if (isFloat32eq(fa, fb)) {
00422                 return 0;
00423         };
00424         
00425         if (isFloat32gt(fa, fb)) {
00426                 return 1;
00427                 };
00428         
00429         return -1;
00430 }
00431 
00433 int __ltsf2(float a, float b)
00434 {
00435         float32 fa, fb;
00436         fa.f = a;
00437         fb.f = b;
00438         if ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ) {
00439                 /* TODO: sigNaNs*/
00440                 return 1;
00441                 };
00442         if (isFloat32lt(fa, fb)) {
00443                 return -1;
00444                 };
00445         return 0;
00446 }
00447 
00448 /* return value <= 0 if a<=b and neither is NaN */
00449 int __lesf2(float a, float b)
00450 {
00451         float32 fa, fb;
00452         fa.f = a;
00453         fb.f = b;
00454         if ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ) {
00455                 /* TODO: sigNaNs*/
00456                 return 1;
00457                 };
00458         
00459         if (isFloat32eq(fa, fb)) {
00460                 return 0;
00461         };
00462         
00463         if (isFloat32lt(fa, fb)) {
00464                 return -1;
00465                 };
00466         
00467         return 1;
00468 }
00469 
00471 int __gtsf2(float a, float b)
00472 {
00473         float32 fa, fb;
00474         fa.f = a;
00475         fb.f = b;
00476         if ( (isFloat32NaN(fa)) || (isFloat32NaN(fb)) ) {
00477                 /* TODO: sigNaNs*/
00478                 return -1;
00479                 };
00480         if (isFloat32gt(fa, fb)) {
00481                 return 1;
00482                 };
00483         return 0;
00484 }
00485 
00486 /* Other functions */
00487 
00488 float __powisf2(float a, int b)
00489 {
00490 /* TODO: */
00491         float32 fa;
00492         fa.binary = FLOAT32_NAN;
00493         return fa.f;
00494 }
00495 
00496 

Generated on Sun Jun 18 18:00:18 2006 for HelenOS Userspace (ia64) by  doxygen 1.4.6