comparesf2.S revision 2d1fdb26e458c4ddc04155c1d421bced3ba90cd0
1//===-- comparesf2.S - Implement single-precision soft-float comparisons --===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the following soft-fp_t comparison routines:
11//
12//   __eqsf2   __gesf2   __unordsf2
13//   __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, with multiple names.
19//
20// The 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#include "../assembly.h"
41.syntax unified
42
43.p2align 2
44DEFINE_COMPILERRT_FUNCTION(__eqsf2)
45    // Make copies of a and b with the sign bit shifted off the top.  These will
46    // be used to detect zeros and NaNs.
47    mov     r2,         r0, lsl #1
48    mov     r3,         r1, lsl #1
49
50    // We do the comparison in three stages (ignoring NaN values for the time
51    // being).  First, we orr the absolute values of a and b; this sets the Z
52    // flag if both a and b are zero (of either sign).  The shift of r3 doesn't
53    // effect this at all, but it *does* make sure that the C flag is clear for
54    // the subsequent operations.
55    orrs    r12,    r2, r3, lsr #1
56
57    // Next, we check if a and b have the same or different signs.  If they have
58    // opposite signs, this eor will set the N flag.
59    it ne
60    eorsne  r12,    r0, r1
61
62    // If a and b are equal (either both zeros or bit identical; again, we're
63    // ignoring NaNs for now), this subtract will zero out r0.  If they have the
64    // same sign, the flags are updated as they would be for a comparison of the
65    // absolute values of a and b.
66    it pl
67    subspl  r0,     r2, r3
68
69    // If a is smaller in magnitude than b and both have the same sign, place
70    // the negation of the sign of b in r0.  Thus, if both are negative and
71    // a > b, this sets r0 to 0; if both are positive and a < b, this sets
72    // r0 to -1.
73    //
74    // This is also done if a and b have opposite signs and are not both zero,
75    // because in that case the subtract was not performed and the C flag is
76    // still clear from the shift argument in orrs; if a is positive and b
77    // negative, this places 0 in r0; if a is negative and b positive, -1 is
78    // placed in r0.
79    it lo
80    mvnlo   r0,         r1, asr #31
81
82    // If a is greater in magnitude than b and both have the same sign, place
83    // the sign of b in r0.  Thus, if both are negative and a < b, -1 is placed
84    // in r0, which is the desired result.  Conversely, if both are positive
85    // and a > b, zero is placed in r0.
86    it hi
87    movhi   r0,         r1, asr #31
88
89    // If you've been keeping track, at this point r0 contains -1 if a < b and
90    // 0 if a >= b.  All that remains to be done is to set it to 1 if a > b.
91    // If a == b, then the Z flag is set, so we can get the correct final value
92    // into r0 by simply or'ing with 1 if Z is clear.
93    it ne
94    orrne   r0,     r0, #1
95
96    // Finally, we need to deal with NaNs.  If either argument is NaN, replace
97    // the value in r0 with 1.
98    cmp     r2,         #0xff000000
99    ite ls
100    cmpls   r3,         #0xff000000
101    movhi   r0,         #1
102    JMP(lr)
103END_COMPILERRT_FUNCTION(__eqsf2)
104DEFINE_COMPILERRT_FUNCTION_ALIAS(__lesf2, __eqsf2)
105DEFINE_COMPILERRT_FUNCTION_ALIAS(__ltsf2, __eqsf2)
106DEFINE_COMPILERRT_FUNCTION_ALIAS(__nesf2, __eqsf2)
107
108.p2align 2
109DEFINE_COMPILERRT_FUNCTION(__gtsf2)
110    // Identical to the preceding except in that we return -1 for NaN values.
111    // Given that the two paths share so much code, one might be tempted to
112    // unify them; however, the extra code needed to do so makes the code size
113    // to performance tradeoff very hard to justify for such small functions.
114    mov     r2,         r0, lsl #1
115    mov     r3,         r1, lsl #1
116    orrs    r12,    r2, r3, lsr #1
117    it ne
118    eorsne  r12,    r0, r1
119    it pl
120    subspl  r0,     r2, r3
121    it lo
122    mvnlo   r0,         r1, asr #31
123    it hi
124    movhi   r0,         r1, asr #31
125    it ne
126    orrne   r0,     r0, #1
127    cmp     r2,         #0xff000000
128    ite ls
129    cmpls   r3,         #0xff000000
130    movhi   r0,         #-1
131    JMP(lr)
132END_COMPILERRT_FUNCTION(__gtsf2)
133DEFINE_COMPILERRT_FUNCTION_ALIAS(__gesf2, __gtsf2)
134
135.p2align 2
136DEFINE_COMPILERRT_FUNCTION(__unordsf2)
137    // Return 1 for NaN values, 0 otherwise.
138    mov     r2,         r0, lsl #1
139    mov     r3,         r1, lsl #1
140    mov     r0,         #0
141    cmp     r2,         #0xff000000
142    ite ls
143    cmpls   r3,         #0xff000000
144    movhi   r0,         #1
145    JMP(lr)
146END_COMPILERRT_FUNCTION(__unordsf2)
147
148DEFINE_AEABI_FUNCTION_ALIAS(__aeabi_fcmpun, __unordsf2)
149