1 //===-- comparesf2.S - Implement single-precision soft-float comparisons --===//
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-fp_t comparison routines:
12 // __eqsf2 __gesf2 __unordsf2
17 // The semantics of the routines grouped in each column are identical, so there
18 // is a single implementation for each, with multiple names.
20 // The routines behave as follows:
22 // __lesf2(a,b) returns -1 if a < b
25 // 1 if either a or b is NaN
27 // __gesf2(a,b) returns -1 if a < b
30 // -1 if either a or b is NaN
32 // __unordsf2(a,b) returns 0 if both a and b are numbers
33 // 1 if either a or b is NaN
35 // Note that __lesf2( ) and __gesf2( ) are identical except in their handling of
38 //===----------------------------------------------------------------------===//
40 #include "../assembly.h"
43 #if defined(USE_THUMB_PROLOGUE)
47 @ int __eqsf2(float a, float b)
50 #if defined(USE_THUMB_PROLOGUE)
51 DEFINE_COMPILERRT_THUMB_FUNCTION(__eqsf2)
53 DEFINE_COMPILERRT_FUNCTION(__eqsf2)
55 #if defined(COMPILER_RT_ARMHF_TARGET)
59 // Make copies of a and b with the sign bit shifted off the top. These will
60 // be used to detect zeros and NaNs.
61 #if __ARM_ARCH_ISA_THUMB == 1
70 // We do the comparison in three stages (ignoring NaN values for the time
71 // being). First, we orr the absolute values of a and b; this sets the Z
72 // flag if both a and b are zero (of either sign). The shift of r3 doesn't
73 // effect this at all, but it *does* make sure that the C flag is clear for
74 // the subsequent operations.
75 #if defined(USE_THUMB_1)
79 orrs r12, r2, r3, lsr #1
81 // Next, we check if a and b have the same or different signs. If they have
82 // opposite signs, this eor will set the N flag.
83 #if defined(USE_THUMB_1)
93 // If a and b are equal (either both zeros or bit identical; again, we're
94 // ignoring NaNs for now), this subtract will zero out r0. If they have the
95 // same sign, the flags are updated as they would be for a comparison of the
96 // absolute values of a and b.
97 #if defined(USE_THUMB_1)
106 // If a is smaller in magnitude than b and both have the same sign, place
107 // the negation of the sign of b in r0. Thus, if both are negative and
108 // a > b, this sets r0 to 0; if both are positive and a < b, this sets
111 // This is also done if a and b have opposite signs and are not both zero,
112 // because in that case the subtract was not performed and the C flag is
113 // still clear from the shift argument in orrs; if a is positive and b
114 // negative, this places 0 in r0; if a is negative and b positive, -1 is
116 #if defined(USE_THUMB_1)
118 // Here if a and b have the same sign and absA < absB, the result is thus
119 // b < 0 ? 1 : -1. Same if a and b have the opposite sign (ignoring Nan).
122 bne LOCAL_LABEL(CHECK_NAN)
124 b LOCAL_LABEL(CHECK_NAN)
128 mvnlo r0, r1, asr #31
131 // If a is greater in magnitude than b and both have the same sign, place
132 // the sign of b in r0. Thus, if both are negative and a < b, -1 is placed
133 // in r0, which is the desired result. Conversely, if both are positive
134 // and a > b, zero is placed in r0.
135 #if defined(USE_THUMB_1)
137 // Here both have the same sign and absA > absB.
140 beq LOCAL_LABEL(CHECK_NAN)
145 movhi r0, r1, asr #31
148 // If you've been keeping track, at this point r0 contains -1 if a < b and
149 // 0 if a >= b. All that remains to be done is to set it to 1 if a > b.
150 // If a == b, then the Z flag is set, so we can get the correct final value
151 // into r0 by simply or'ing with 1 if Z is clear.
152 // For Thumb-1, r0 contains -1 if a < b, 0 if a > b and 0 if a == b.
153 #if defined(USE_THUMB_1)
158 // Finally, we need to deal with NaNs. If either argument is NaN, replace
159 // the value in r0 with 1.
160 #if defined(USE_THUMB_1)
161 LOCAL_LABEL(CHECK_NAN):
175 cmpls r3, #0xff000000
179 END_COMPILERRT_FUNCTION(__eqsf2)
181 DEFINE_COMPILERRT_FUNCTION_ALIAS(__lesf2, __eqsf2)
182 DEFINE_COMPILERRT_FUNCTION_ALIAS(__ltsf2, __eqsf2)
183 DEFINE_COMPILERRT_FUNCTION_ALIAS(__nesf2, __eqsf2)
185 @ int __gtsf2(float a, float b)
188 #if defined(USE_THUMB)
189 DEFINE_COMPILERRT_THUMB_FUNCTION(__gtsf2)
191 DEFINE_COMPILERRT_FUNCTION(__gtsf2)
193 // Identical to the preceding except in that we return -1 for NaN values.
194 // Given that the two paths share so much code, one might be tempted to
195 // unify them; however, the extra code needed to do so makes the code size
196 // to performance tradeoff very hard to justify for such small functions.
197 #if defined(COMPILER_RT_ARMHF_TARGET)
201 #if defined(USE_THUMB_1)
217 bne LOCAL_LABEL(CHECK_NAN_2)
219 b LOCAL_LABEL(CHECK_NAN_2)
224 beq LOCAL_LABEL(CHECK_NAN_2)
227 LOCAL_LABEL(CHECK_NAN_2):
242 orrs r12, r2, r3, lsr #1
248 mvnlo r0, r1, asr #31
250 movhi r0, r1, asr #31
255 cmpls r3, #0xff000000
259 END_COMPILERRT_FUNCTION(__gtsf2)
261 DEFINE_COMPILERRT_FUNCTION_ALIAS(__gesf2, __gtsf2)
263 @ int __unordsf2(float a, float b)
266 #if defined(USE_THUMB)
267 DEFINE_COMPILERRT_THUMB_FUNCTION(__unordsf2)
269 DEFINE_COMPILERRT_FUNCTION(__unordsf2)
272 #if defined(COMPILER_RT_ARMHF_TARGET)
276 // Return 1 for NaN values, 0 otherwise.
280 #if defined(USE_THUMB_1)
293 cmpls r3, #0xff000000
297 END_COMPILERRT_FUNCTION(__unordsf2)
299 #if defined(COMPILER_RT_ARMHF_TARGET)
300 DEFINE_COMPILERRT_FUNCTION(__aeabi_fcmpum)
303 b SYMBOL_NAME(__unordsf2)
304 END_COMPILERRT_FUNCTION(__aeabi_fcmpum)
306 DEFINE_AEABI_FUNCTION_ALIAS(__aeabi_fcmpun, __unordsf2)
309 NO_EXEC_STACK_DIRECTIVE