1 //===-- comparesf2.S - Implement single-precision soft-float comparisons --===//
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, with multiple names.
19 // The 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 #include "../assembly.h"
45 .macro COMPARESF2_FUNCTION_BODY handle_nan:req
46 #if defined(COMPILER_RT_ARMHF_TARGET)
50 // Make copies of a and b with the sign bit shifted off the top. These will
51 // be used to detect zeros and NaNs.
52 #if defined(USE_THUMB_1)
61 // We do the comparison in three stages (ignoring NaN values for the time
62 // being). First, we orr the absolute values of a and b; this sets the Z
63 // flag if both a and b are zero (of either sign). The shift of r3 doesn't
64 // effect this at all, but it *does* make sure that the C flag is clear for
65 // the subsequent operations.
66 #if defined(USE_THUMB_1)
70 orrs r12, r2, r3, lsr #1
72 // Next, we check if a and b have the same or different signs. If they have
73 // opposite signs, this eor will set the N flag.
74 #if defined(USE_THUMB_1)
84 // If a and b are equal (either both zeros or bit identical; again, we're
85 // ignoring NaNs for now), this subtract will zero out r0. If they have the
86 // same sign, the flags are updated as they would be for a comparison of the
87 // absolute values of a and b.
88 #if defined(USE_THUMB_1)
97 // If a is smaller in magnitude than b and both have the same sign, place
98 // the negation of the sign of b in r0. Thus, if both are negative and
99 // a > b, this sets r0 to 0; if both are positive and a < b, this sets
102 // This is also done if a and b have opposite signs and are not both zero,
103 // because in that case the subtract was not performed and the C flag is
104 // still clear from the shift argument in orrs; if a is positive and b
105 // negative, this places 0 in r0; if a is negative and b positive, -1 is
107 #if defined(USE_THUMB_1)
109 // Here if a and b have the same sign and absA < absB, the result is thus
110 // b < 0 ? 1 : -1. Same if a and b have the opposite sign (ignoring Nan).
113 bne LOCAL_LABEL(CHECK_NAN\@)
115 b LOCAL_LABEL(CHECK_NAN\@)
119 mvnlo r0, r1, asr #31
122 // If a is greater in magnitude than b and both have the same sign, place
123 // the sign of b in r0. Thus, if both are negative and a < b, -1 is placed
124 // in r0, which is the desired result. Conversely, if both are positive
125 // and a > b, zero is placed in r0.
126 #if defined(USE_THUMB_1)
128 // Here both have the same sign and absA > absB.
131 beq LOCAL_LABEL(CHECK_NAN\@)
136 movhi r0, r1, asr #31
139 // If you've been keeping track, at this point r0 contains -1 if a < b and
140 // 0 if a >= b. All that remains to be done is to set it to 1 if a > b.
141 // If a == b, then the Z flag is set, so we can get the correct final value
142 // into r0 by simply or'ing with 1 if Z is clear.
143 // For Thumb-1, r0 contains -1 if a < b, 0 if a > b and 0 if a == b.
144 #if !defined(USE_THUMB_1)
149 // Finally, we need to deal with NaNs. If either argument is NaN, replace
150 // the value in r0 with 1.
151 #if defined(USE_THUMB_1)
152 LOCAL_LABEL(CHECK_NAN\@):
166 cmpls r3, #0xff000000
172 @ int __eqsf2(float a, float b)
175 DEFINE_COMPILERRT_FUNCTION(__eqsf2)
177 .macro __eqsf2_handle_nan
178 #if defined(USE_THUMB_1)
185 COMPARESF2_FUNCTION_BODY __eqsf2_handle_nan
187 END_COMPILERRT_FUNCTION(__eqsf2)
189 DEFINE_COMPILERRT_FUNCTION_ALIAS(__lesf2, __eqsf2)
190 DEFINE_COMPILERRT_FUNCTION_ALIAS(__ltsf2, __eqsf2)
191 DEFINE_COMPILERRT_FUNCTION_ALIAS(__nesf2, __eqsf2)
194 // Alias for libgcc compatibility
195 DEFINE_COMPILERRT_FUNCTION_ALIAS(__cmpsf2, __lesf2)
198 @ int __gtsf2(float a, float b)
201 DEFINE_COMPILERRT_FUNCTION(__gtsf2)
203 .macro __gtsf2_handle_nan
204 #if defined(USE_THUMB_1)
212 COMPARESF2_FUNCTION_BODY __gtsf2_handle_nan
214 END_COMPILERRT_FUNCTION(__gtsf2)
216 DEFINE_COMPILERRT_FUNCTION_ALIAS(__gesf2, __gtsf2)
218 @ int __unordsf2(float a, float b)
221 DEFINE_COMPILERRT_FUNCTION(__unordsf2)
223 #if defined(COMPILER_RT_ARMHF_TARGET)
227 // Return 1 for NaN values, 0 otherwise.
231 #if defined(USE_THUMB_1)
244 cmpls r3, #0xff000000
248 END_COMPILERRT_FUNCTION(__unordsf2)
250 #if defined(COMPILER_RT_ARMHF_TARGET)
251 DEFINE_COMPILERRT_FUNCTION(__aeabi_fcmpun)
254 b SYMBOL_NAME(__unordsf2)
255 END_COMPILERRT_FUNCTION(__aeabi_fcmpun)
257 DEFINE_AEABI_FUNCTION_ALIAS(__aeabi_fcmpun, __unordsf2)
260 NO_EXEC_STACK_DIRECTIVE