1 //===-- lib/comparedf2.c - Double-precision comparisons -----------*- C -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // // This file implements the following soft-float comparison routines:
12 // __eqdf2 __gedf2 __unorddf2
17 // The semantics of the routines grouped in each column are identical, so there
18 // is a single implementation for each, and wrappers to provide the other names.
20 // The main routines behave as follows:
22 // __ledf2(a,b) returns -1 if a < b
25 // 1 if either a or b is NaN
27 // __gedf2(a,b) returns -1 if a < b
30 // -1 if either a or b is NaN
32 // __unorddf2(a,b) returns 0 if both a and b are numbers
33 // 1 if either a or b is NaN
35 // Note that __ledf2( ) and __gedf2( ) are identical except in their handling of
38 //===----------------------------------------------------------------------===//
40 #define DOUBLE_PRECISION
50 COMPILER_RT_ABI enum LE_RESULT
51 __ledf2(fp_t a, fp_t b) {
53 const srep_t aInt = toRep(a);
54 const srep_t bInt = toRep(b);
55 const rep_t aAbs = aInt & absMask;
56 const rep_t bAbs = bInt & absMask;
58 // If either a or b is NaN, they are unordered.
59 if (aAbs > infRep || bAbs > infRep) return LE_UNORDERED;
61 // If a and b are both zeros, they are equal.
62 if ((aAbs | bAbs) == 0) return LE_EQUAL;
64 // If at least one of a and b is positive, we get the same result comparing
65 // a and b as signed integers as we would with a floating-point compare.
66 if ((aInt & bInt) >= 0) {
67 if (aInt < bInt) return LE_LESS;
68 else if (aInt == bInt) return LE_EQUAL;
69 else return LE_GREATER;
72 // Otherwise, both are negative, so we need to flip the sense of the
73 // comparison to get the correct result. (This assumes a twos- or ones-
74 // complement integer representation; if integers are represented in a
75 // sign-magnitude representation, then this flip is incorrect).
77 if (aInt > bInt) return LE_LESS;
78 else if (aInt == bInt) return LE_EQUAL;
79 else return LE_GREATER;
84 // Alias for libgcc compatibility
85 FNALIAS(__cmpdf2, __ledf2);
92 GE_UNORDERED = -1 // Note: different from LE_UNORDERED
95 COMPILER_RT_ABI enum GE_RESULT
96 __gedf2(fp_t a, fp_t b) {
98 const srep_t aInt = toRep(a);
99 const srep_t bInt = toRep(b);
100 const rep_t aAbs = aInt & absMask;
101 const rep_t bAbs = bInt & absMask;
103 if (aAbs > infRep || bAbs > infRep) return GE_UNORDERED;
104 if ((aAbs | bAbs) == 0) return GE_EQUAL;
105 if ((aInt & bInt) >= 0) {
106 if (aInt < bInt) return GE_LESS;
107 else if (aInt == bInt) return GE_EQUAL;
108 else return GE_GREATER;
110 if (aInt > bInt) return GE_LESS;
111 else if (aInt == bInt) return GE_EQUAL;
112 else return GE_GREATER;
116 ARM_EABI_FNALIAS(dcmpun, unorddf2)
119 __unorddf2(fp_t a, fp_t b) {
120 const rep_t aAbs = toRep(a) & absMask;
121 const rep_t bAbs = toRep(b) & absMask;
122 return aAbs > infRep || bAbs > infRep;
125 // The following are alternative names for the preceding routines.
127 COMPILER_RT_ABI enum LE_RESULT
128 __eqdf2(fp_t a, fp_t b) {
129 return __ledf2(a, b);
132 COMPILER_RT_ABI enum LE_RESULT
133 __ltdf2(fp_t a, fp_t b) {
134 return __ledf2(a, b);
137 COMPILER_RT_ABI enum LE_RESULT
138 __nedf2(fp_t a, fp_t b) {
139 return __ledf2(a, b);
142 COMPILER_RT_ABI enum GE_RESULT
143 __gtdf2(fp_t a, fp_t b) {
144 return __gedf2(a, b);