1 //===-- lib/comparesf2.c - Single-precision comparisons -----------*- C -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the following soft-fp_t comparison routines:
11 // __eqsf2 __gesf2 __unordsf2
16 // The semantics of the routines grouped in each column are identical, so there
17 // is a single implementation for each, and wrappers to provide the other names.
19 // The main routines behave as follows:
21 // __lesf2(a,b) returns -1 if a < b
24 // 1 if either a or b is NaN
26 // __gesf2(a,b) returns -1 if a < b
29 // -1 if either a or b is NaN
31 // __unordsf2(a,b) returns 0 if both a and b are numbers
32 // 1 if either a or b is NaN
34 // Note that __lesf2( ) and __gesf2( ) are identical except in their handling of
37 //===----------------------------------------------------------------------===//
39 #define SINGLE_PRECISION
42 enum LE_RESULT { LE_LESS = -1, LE_EQUAL = 0, LE_GREATER = 1, LE_UNORDERED = 1 };
44 COMPILER_RT_ABI enum LE_RESULT __lesf2(fp_t a, fp_t b) {
46 const srep_t aInt = toRep(a);
47 const srep_t bInt = toRep(b);
48 const rep_t aAbs = aInt & absMask;
49 const rep_t bAbs = bInt & absMask;
51 // If either a or b is NaN, they are unordered.
52 if (aAbs > infRep || bAbs > infRep)
55 // If a and b are both zeros, they are equal.
56 if ((aAbs | bAbs) == 0)
59 // If at least one of a and b is positive, we get the same result comparing
60 // a and b as signed integers as we would with a fp_ting-point compare.
61 if ((aInt & bInt) >= 0) {
64 else if (aInt == bInt)
70 // Otherwise, both are negative, so we need to flip the sense of the
71 // comparison to get the correct result. (This assumes a twos- or ones-
72 // complement integer representation; if integers are represented in a
73 // sign-magnitude representation, then this flip is incorrect).
77 else if (aInt == bInt)
85 // Alias for libgcc compatibility
86 COMPILER_RT_ALIAS(__lesf2, __cmpsf2)
88 COMPILER_RT_ALIAS(__lesf2, __eqsf2)
89 COMPILER_RT_ALIAS(__lesf2, __ltsf2)
90 COMPILER_RT_ALIAS(__lesf2, __nesf2)
96 GE_UNORDERED = -1 // Note: different from LE_UNORDERED
99 COMPILER_RT_ABI enum GE_RESULT __gesf2(fp_t a, fp_t b) {
101 const srep_t aInt = toRep(a);
102 const srep_t bInt = toRep(b);
103 const rep_t aAbs = aInt & absMask;
104 const rep_t bAbs = bInt & absMask;
106 if (aAbs > infRep || bAbs > infRep)
108 if ((aAbs | bAbs) == 0)
110 if ((aInt & bInt) >= 0) {
113 else if (aInt == bInt)
120 else if (aInt == bInt)
127 COMPILER_RT_ALIAS(__gesf2, __gtsf2)
130 __unordsf2(fp_t a, fp_t b) {
131 const rep_t aAbs = toRep(a) & absMask;
132 const rep_t bAbs = toRep(b) & absMask;
133 return aAbs > infRep || bAbs > infRep;
136 #if defined(__ARM_EABI__)
137 #if defined(COMPILER_RT_ARMHF_TARGET)
138 AEABI_RTABI int __aeabi_fcmpun(fp_t a, fp_t b) { return __unordsf2(a, b); }
140 COMPILER_RT_ALIAS(__unordsf2, __aeabi_fcmpun)
144 #if defined(_WIN32) && !defined(__MINGW32__)
145 // The alias mechanism doesn't work on Windows except for MinGW, so emit
146 // wrapper functions.
147 int __eqsf2(fp_t a, fp_t b) { return __lesf2(a, b); }
148 int __ltsf2(fp_t a, fp_t b) { return __lesf2(a, b); }
149 int __nesf2(fp_t a, fp_t b) { return __lesf2(a, b); }
150 int __gtsf2(fp_t a, fp_t b) { return __gesf2(a, b); }