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 00035 #include<sftypes.h> 00036 #include<comparison.h> 00037 00038 inline int isFloat32NaN(float32 f) 00039 { /* NaN : exp = 0xff and nonzero fraction */ 00040 return ((f.parts.exp==0xFF)&&(f.parts.fraction)); 00041 } 00042 00043 inline int isFloat64NaN(float64 d) 00044 { /* NaN : exp = 0x7ff and nonzero fraction */ 00045 return ((d.parts.exp==0x7FF)&&(d.parts.fraction)); 00046 } 00047 00048 inline int isFloat32SigNaN(float32 f) 00049 { /* SigNaN : exp = 0xff fraction = 0xxxxx..x (binary), where at least one x is nonzero */ 00050 return ((f.parts.exp==0xFF)&&(f.parts.fraction<0x400000)&&(f.parts.fraction)); 00051 } 00052 00053 inline int isFloat64SigNaN(float64 d) 00054 { /* SigNaN : exp = 0x7ff fraction = 0xxxxx..x (binary), where at least one x is nonzero */ 00055 return ((d.parts.exp==0x7FF)&&(d.parts.fraction)&&(d.parts.fraction<0x8000000000000ll)); 00056 } 00057 00058 inline int isFloat32Infinity(float32 f) 00059 { 00060 return ((f.parts.exp==0xFF)&&(f.parts.fraction==0x0)); 00061 } 00062 00063 inline int isFloat64Infinity(float64 d) 00064 { 00065 return ((d.parts.exp==0x7FF)&&(d.parts.fraction==0x0)); 00066 } 00067 00068 inline int isFloat32Zero(float32 f) 00069 { 00070 return (((f.binary) & 0x7FFFFFFF) == 0); 00071 } 00072 00073 inline int isFloat64Zero(float64 d) 00074 { 00075 return (((d.binary) & 0x7FFFFFFFFFFFFFFFll) == 0); 00076 } 00077 00081 inline int isFloat32eq(float32 a, float32 b) 00082 { 00083 return ((a.binary==b.binary)||(((a.binary| b.binary)&0x7FFFFFFF)==0)); /* a equals to b or both are zeros (with any sign) */ 00084 } 00085 00089 inline int isFloat32lt(float32 a, float32 b) 00090 { 00091 if (((a.binary| b.binary)&0x7FFFFFFF)==0) { 00092 return 0; /* +- zeroes */ 00093 }; 00094 00095 if ((a.parts.sign)&&(b.parts.sign)) { 00096 /*if both are negative, smaller is that with greater binary value*/ 00097 return (a.binary>b.binary); 00098 }; 00099 00100 /* lets negate signs - now will be positive numbers allways bigger than negative (first bit will be set for unsigned integer comparison)*/ 00101 a.parts.sign=!a.parts.sign; 00102 b.parts.sign=!b.parts.sign; 00103 return (a.binary<b.binary); 00104 00105 } 00106 00110 inline int isFloat32gt(float32 a, float32 b) 00111 { 00112 if (((a.binary| b.binary)&0x7FFFFFFF)==0) { 00113 return 0; /* zeroes are equal with any sign */ 00114 }; 00115 00116 if ((a.parts.sign)&&(b.parts.sign)) { 00117 /*if both are negative, greater is that with smaller binary value*/ 00118 return (a.binary<b.binary); 00119 }; 00120 00121 /* lets negate signs - now will be positive numbers allways bigger than negative (first bit will be set for unsigned integer comparison)*/ 00122 a.parts.sign=!a.parts.sign; 00123 b.parts.sign=!b.parts.sign; 00124 return (a.binary>b.binary); 00125 00126 } 00127