blob: c5bb169d0021307758c02f48b37bb3614f3dc7f9 [file] [log] [blame]
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +00001//===-- lib/comparedf2.c - Double-precision comparisons -----------*- C -*-===//
Stephen Canonb1fdde12010-07-01 15:52:42 +00002//
Stephen Canon74eaf1f2010-07-01 17:58:24 +00003// The LLVM Compiler Infrastructure
4//
Howard Hinnant5b791f62010-11-16 22:13:33 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Stephen Canon74eaf1f2010-07-01 17:58:24 +00007//
8//===----------------------------------------------------------------------===//
9//
10// // This file implements the following soft-float comparison routines:
11//
12// __eqdf2 __gedf2 __unorddf2
Stephen Canonb1fdde12010-07-01 15:52:42 +000013// __ledf2 __gtdf2
14// __ltdf2
15// __nedf2
16//
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.
19//
20// The main routines behave as follows:
21//
22// __ledf2(a,b) returns -1 if a < b
23// 0 if a == b
24// 1 if a > b
25// 1 if either a or b is NaN
26//
27// __gedf2(a,b) returns -1 if a < b
28// 0 if a == b
29// 1 if a > b
30// -1 if either a or b is NaN
31//
32// __unorddf2(a,b) returns 0 if both a and b are numbers
33// 1 if either a or b is NaN
34//
35// Note that __ledf2( ) and __gedf2( ) are identical except in their handling of
36// NaN values.
Stephen Canon74eaf1f2010-07-01 17:58:24 +000037//
38//===----------------------------------------------------------------------===//
39
40#define DOUBLE_PRECISION
41#include "fp_lib.h"
Stephen Canonb1fdde12010-07-01 15:52:42 +000042
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +000043enum LE_RESULT {
44 LE_LESS = -1,
45 LE_EQUAL = 0,
46 LE_GREATER = 1,
47 LE_UNORDERED = 1
48};
49
Joerg Sonnenberger6e99daa2014-03-01 15:30:50 +000050COMPILER_RT_ABI enum LE_RESULT
51__ledf2(fp_t a, fp_t b) {
Stephen Canonb1fdde12010-07-01 15:52:42 +000052
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;
57
58 // If either a or b is NaN, they are unordered.
59 if (aAbs > infRep || bAbs > infRep) return LE_UNORDERED;
60
61 // If a and b are both zeros, they are equal.
62 if ((aAbs | bAbs) == 0) return LE_EQUAL;
63
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;
70 }
71
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 else {
77 if (aInt > bInt) return LE_LESS;
78 else if (aInt == bInt) return LE_EQUAL;
79 else return LE_GREATER;
80 }
81}
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +000082
Saleem Abdulrasool0d6094b2015-08-21 04:39:52 +000083#if defined(__ELF__)
Josh Gao772527c2015-08-21 02:51:17 +000084// Alias for libgcc compatibility
85FNALIAS(__cmpdf2, __ledf2);
Saleem Abdulrasool0d6094b2015-08-21 04:39:52 +000086#endif
Josh Gao772527c2015-08-21 02:51:17 +000087
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +000088enum GE_RESULT {
89 GE_LESS = -1,
90 GE_EQUAL = 0,
91 GE_GREATER = 1,
92 GE_UNORDERED = -1 // Note: different from LE_UNORDERED
93};
94
Joerg Sonnenberger6e99daa2014-03-01 15:30:50 +000095COMPILER_RT_ABI enum GE_RESULT
96__gedf2(fp_t a, fp_t b) {
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +000097
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;
102
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;
109 } else {
110 if (aInt > bInt) return GE_LESS;
111 else if (aInt == bInt) return GE_EQUAL;
112 else return GE_GREATER;
113 }
114}
115
Joerg Sonnenberger6e99daa2014-03-01 15:30:50 +0000116COMPILER_RT_ABI int
117__unorddf2(fp_t a, fp_t b) {
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +0000118 const rep_t aAbs = toRep(a) & absMask;
119 const rep_t bAbs = toRep(b) & absMask;
120 return aAbs > infRep || bAbs > infRep;
121}
122
Alp Toker1ee7fc72014-05-15 02:22:34 +0000123// The following are alternative names for the preceding routines.
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +0000124
Joerg Sonnenberger6e99daa2014-03-01 15:30:50 +0000125COMPILER_RT_ABI enum LE_RESULT
126__eqdf2(fp_t a, fp_t b) {
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +0000127 return __ledf2(a, b);
128}
129
Joerg Sonnenberger6e99daa2014-03-01 15:30:50 +0000130COMPILER_RT_ABI enum LE_RESULT
131__ltdf2(fp_t a, fp_t b) {
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +0000132 return __ledf2(a, b);
133}
134
Joerg Sonnenberger6e99daa2014-03-01 15:30:50 +0000135COMPILER_RT_ABI enum LE_RESULT
136__nedf2(fp_t a, fp_t b) {
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +0000137 return __ledf2(a, b);
138}
139
Joerg Sonnenberger6e99daa2014-03-01 15:30:50 +0000140COMPILER_RT_ABI enum GE_RESULT
141__gtdf2(fp_t a, fp_t b) {
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +0000142 return __gedf2(a, b);
143}
144
Saleem Abdulrasool36ac5dd2017-05-16 16:41:37 +0000145#if defined(__ARM_EABI__)
146AEABI_RTABI int __aeabi_dcmpun(fp_t a, fp_t b) {
147 return __unorddf2(a, b);
148}
149#endif
150