blob: 442289c1004ef697533f7dd40da247fb8f19dfe0 [file] [log] [blame]
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +00001//===-- lib/comparesf2.c - Single-precision comparisons -----------*- C -*-===//
Stephen Canonb1fdde12010-07-01 15:52:42 +00002//
3// 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 Canonb1fdde12010-07-01 15:52:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the following soft-fp_t comparison routines:
11//
Stephen Canon74eaf1f2010-07-01 17:58:24 +000012// __eqsf2 __gesf2 __unordsf2
Stephen Canonb1fdde12010-07-01 15:52:42 +000013// __lesf2 __gtsf2
14// __ltsf2
15// __nesf2
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// __lesf2(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// __gesf2(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// __unordsf2(a,b) returns 0 if both a and b are numbers
33// 1 if either a or b is NaN
34//
35// Note that __lesf2( ) and __gesf2( ) are identical except in their handling of
36// NaN values.
37//
38//===----------------------------------------------------------------------===//
39
40#define SINGLE_PRECISION
41#include "fp_lib.h"
42
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__lesf2(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 fp_ting-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
83enum GE_RESULT {
84 GE_LESS = -1,
85 GE_EQUAL = 0,
86 GE_GREATER = 1,
87 GE_UNORDERED = -1 // Note: different from LE_UNORDERED
88};
89
Joerg Sonnenberger6e99daa2014-03-01 15:30:50 +000090COMPILER_RT_ABI enum GE_RESULT
91__gesf2(fp_t a, fp_t b) {
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +000092
93 const srep_t aInt = toRep(a);
94 const srep_t bInt = toRep(b);
95 const rep_t aAbs = aInt & absMask;
96 const rep_t bAbs = bInt & absMask;
97
98 if (aAbs > infRep || bAbs > infRep) return GE_UNORDERED;
99 if ((aAbs | bAbs) == 0) return GE_EQUAL;
100 if ((aInt & bInt) >= 0) {
101 if (aInt < bInt) return GE_LESS;
102 else if (aInt == bInt) return GE_EQUAL;
103 else return GE_GREATER;
104 } else {
105 if (aInt > bInt) return GE_LESS;
106 else if (aInt == bInt) return GE_EQUAL;
107 else return GE_GREATER;
108 }
109}
110
Zonr Chang2d5d8162013-02-07 08:29:47 +0000111ARM_EABI_FNALIAS(fcmpun, unordsf2)
112
Joerg Sonnenberger6e99daa2014-03-01 15:30:50 +0000113COMPILER_RT_ABI int
114__unordsf2(fp_t a, fp_t b) {
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +0000115 const rep_t aAbs = toRep(a) & absMask;
116 const rep_t bAbs = toRep(b) & absMask;
117 return aAbs > infRep || bAbs > infRep;
118}
119
Alp Toker1ee7fc72014-05-15 02:22:34 +0000120// The following are alternative names for the preceding routines.
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +0000121
Joerg Sonnenberger6e99daa2014-03-01 15:30:50 +0000122COMPILER_RT_ABI enum LE_RESULT
123__eqsf2(fp_t a, fp_t b) {
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +0000124 return __lesf2(a, b);
125}
126
Joerg Sonnenberger6e99daa2014-03-01 15:30:50 +0000127COMPILER_RT_ABI enum LE_RESULT
128__ltsf2(fp_t a, fp_t b) {
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +0000129 return __lesf2(a, b);
130}
131
Joerg Sonnenberger6e99daa2014-03-01 15:30:50 +0000132COMPILER_RT_ABI enum LE_RESULT
133__nesf2(fp_t a, fp_t b) {
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +0000134 return __lesf2(a, b);
135}
136
Joerg Sonnenberger6e99daa2014-03-01 15:30:50 +0000137COMPILER_RT_ABI enum GE_RESULT
138__gtsf2(fp_t a, fp_t b) {
Daniel Dunbar4c70f3e2011-03-25 18:45:39 +0000139 return __gesf2(a, b);
140}