1 //===-- lib/comparetf2.c - Quad-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 // __eqtf2 __getf2 __unordtf2
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 // __letf2(a,b) returns -1 if a < b
25 // 1 if either a or b is NaN
27 // __getf2(a,b) returns -1 if a < b
30 // -1 if either a or b is NaN
32 // __unordtf2(a,b) returns 0 if both a and b are numbers
33 // 1 if either a or b is NaN
35 // Note that __letf2( ) and __getf2( ) are identical except in their handling of
38 //===----------------------------------------------------------------------===//
40 #define QUAD_PRECISION
43 #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)
51 COMPILER_RT_ABI
enum LE_RESULT
__letf2(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).
76 if (aInt
> bInt
) return LE_LESS
;
77 else if (aInt
== bInt
) return LE_EQUAL
;
78 else return LE_GREATER
;
86 GE_UNORDERED
= -1 // Note: different from LE_UNORDERED
89 COMPILER_RT_ABI
enum GE_RESULT
__getf2(fp_t a
, fp_t b
) {
91 const srep_t aInt
= toRep(a
);
92 const srep_t bInt
= toRep(b
);
93 const rep_t aAbs
= aInt
& absMask
;
94 const rep_t bAbs
= bInt
& absMask
;
96 if (aAbs
> infRep
|| bAbs
> infRep
) return GE_UNORDERED
;
97 if ((aAbs
| bAbs
) == 0) return GE_EQUAL
;
98 if ((aInt
& bInt
) >= 0) {
99 if (aInt
< bInt
) return GE_LESS
;
100 else if (aInt
== bInt
) return GE_EQUAL
;
101 else return GE_GREATER
;
103 if (aInt
> bInt
) return GE_LESS
;
104 else if (aInt
== bInt
) return GE_EQUAL
;
105 else return GE_GREATER
;
109 COMPILER_RT_ABI
int __unordtf2(fp_t a
, fp_t b
) {
110 const rep_t aAbs
= toRep(a
) & absMask
;
111 const rep_t bAbs
= toRep(b
) & absMask
;
112 return aAbs
> infRep
|| bAbs
> infRep
;
115 // The following are alternative names for the preceding routines.
117 COMPILER_RT_ABI
enum LE_RESULT
__eqtf2(fp_t a
, fp_t b
) {
118 return __letf2(a
, b
);
121 COMPILER_RT_ABI
enum LE_RESULT
__lttf2(fp_t a
, fp_t b
) {
122 return __letf2(a
, b
);
125 COMPILER_RT_ABI
enum LE_RESULT
__netf2(fp_t a
, fp_t b
) {
126 return __letf2(a
, b
);
129 COMPILER_RT_ABI
enum GE_RESULT
__gttf2(fp_t a
, fp_t b
) {
130 return __getf2(a
, b
);