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