xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/builtins/arm/comparesf2.S (revision 30308f423af31fedfebb54e1e3833af8bd10f23b)
1156cd587Sjoerg//===-- comparesf2.S - Implement single-precision soft-float comparisons --===//
2156cd587Sjoerg//
3156cd587Sjoerg//                     The LLVM Compiler Infrastructure
4156cd587Sjoerg//
5156cd587Sjoerg// This file is dual licensed under the MIT and the University of Illinois Open
6156cd587Sjoerg// Source Licenses. See LICENSE.TXT for details.
7156cd587Sjoerg//
8156cd587Sjoerg//===----------------------------------------------------------------------===//
9156cd587Sjoerg//
10156cd587Sjoerg// This file implements the following soft-fp_t comparison routines:
11156cd587Sjoerg//
12156cd587Sjoerg//   __eqsf2   __gesf2   __unordsf2
13156cd587Sjoerg//   __lesf2   __gtsf2
14156cd587Sjoerg//   __ltsf2
15156cd587Sjoerg//   __nesf2
16156cd587Sjoerg//
17156cd587Sjoerg// The semantics of the routines grouped in each column are identical, so there
18156cd587Sjoerg// is a single implementation for each, with multiple names.
19156cd587Sjoerg//
20156cd587Sjoerg// The routines behave as follows:
21156cd587Sjoerg//
22156cd587Sjoerg//   __lesf2(a,b) returns -1 if a < b
23156cd587Sjoerg//                         0 if a == b
24156cd587Sjoerg//                         1 if a > b
25156cd587Sjoerg//                         1 if either a or b is NaN
26156cd587Sjoerg//
27156cd587Sjoerg//   __gesf2(a,b) returns -1 if a < b
28156cd587Sjoerg//                         0 if a == b
29156cd587Sjoerg//                         1 if a > b
30156cd587Sjoerg//                        -1 if either a or b is NaN
31156cd587Sjoerg//
32156cd587Sjoerg//   __unordsf2(a,b) returns 0 if both a and b are numbers
33156cd587Sjoerg//                           1 if either a or b is NaN
34156cd587Sjoerg//
35156cd587Sjoerg// Note that __lesf2( ) and __gesf2( ) are identical except in their handling of
36156cd587Sjoerg// NaN values.
37156cd587Sjoerg//
38156cd587Sjoerg//===----------------------------------------------------------------------===//
39156cd587Sjoerg
40156cd587Sjoerg#include "../assembly.h"
41156cd587Sjoerg.syntax unified
42156cd587Sjoerg
4361f2f256Sjoerg.p2align 2
44156cd587SjoergDEFINE_COMPILERRT_FUNCTION(__eqsf2)
45156cd587Sjoerg    // Make copies of a and b with the sign bit shifted off the top.  These will
46156cd587Sjoerg    // be used to detect zeros and NaNs.
47156cd587Sjoerg    mov     r2,         r0, lsl #1
48156cd587Sjoerg    mov     r3,         r1, lsl #1
49156cd587Sjoerg
50156cd587Sjoerg    // We do the comparison in three stages (ignoring NaN values for the time
51156cd587Sjoerg    // being).  First, we orr the absolute values of a and b; this sets the Z
52156cd587Sjoerg    // flag if both a and b are zero (of either sign).  The shift of r3 doesn't
53156cd587Sjoerg    // effect this at all, but it *does* make sure that the C flag is clear for
54156cd587Sjoerg    // the subsequent operations.
55156cd587Sjoerg    orrs    r12,    r2, r3, lsr #1
56156cd587Sjoerg
57156cd587Sjoerg    // Next, we check if a and b have the same or different signs.  If they have
58156cd587Sjoerg    // opposite signs, this eor will set the N flag.
59156cd587Sjoerg    it ne
60156cd587Sjoerg    eorsne  r12,    r0, r1
61156cd587Sjoerg
62156cd587Sjoerg    // If a and b are equal (either both zeros or bit identical; again, we're
63156cd587Sjoerg    // ignoring NaNs for now), this subtract will zero out r0.  If they have the
64156cd587Sjoerg    // same sign, the flags are updated as they would be for a comparison of the
65156cd587Sjoerg    // absolute values of a and b.
66156cd587Sjoerg    it pl
67156cd587Sjoerg    subspl  r0,     r2, r3
68156cd587Sjoerg
69156cd587Sjoerg    // If a is smaller in magnitude than b and both have the same sign, place
70156cd587Sjoerg    // the negation of the sign of b in r0.  Thus, if both are negative and
71156cd587Sjoerg    // a > b, this sets r0 to 0; if both are positive and a < b, this sets
72156cd587Sjoerg    // r0 to -1.
73156cd587Sjoerg    //
74156cd587Sjoerg    // This is also done if a and b have opposite signs and are not both zero,
75156cd587Sjoerg    // because in that case the subtract was not performed and the C flag is
76156cd587Sjoerg    // still clear from the shift argument in orrs; if a is positive and b
77156cd587Sjoerg    // negative, this places 0 in r0; if a is negative and b positive, -1 is
78156cd587Sjoerg    // placed in r0.
79156cd587Sjoerg    it lo
80156cd587Sjoerg    mvnlo   r0,         r1, asr #31
81156cd587Sjoerg
82156cd587Sjoerg    // If a is greater in magnitude than b and both have the same sign, place
83156cd587Sjoerg    // the sign of b in r0.  Thus, if both are negative and a < b, -1 is placed
84156cd587Sjoerg    // in r0, which is the desired result.  Conversely, if both are positive
85156cd587Sjoerg    // and a > b, zero is placed in r0.
86156cd587Sjoerg    it hi
87156cd587Sjoerg    movhi   r0,         r1, asr #31
88156cd587Sjoerg
89156cd587Sjoerg    // If you've been keeping track, at this point r0 contains -1 if a < b and
90156cd587Sjoerg    // 0 if a >= b.  All that remains to be done is to set it to 1 if a > b.
91156cd587Sjoerg    // If a == b, then the Z flag is set, so we can get the correct final value
92156cd587Sjoerg    // into r0 by simply or'ing with 1 if Z is clear.
93156cd587Sjoerg    it ne
94156cd587Sjoerg    orrne   r0,     r0, #1
95156cd587Sjoerg
96156cd587Sjoerg    // Finally, we need to deal with NaNs.  If either argument is NaN, replace
97156cd587Sjoerg    // the value in r0 with 1.
98156cd587Sjoerg    cmp     r2,         #0xff000000
99156cd587Sjoerg    ite ls
100156cd587Sjoerg    cmpls   r3,         #0xff000000
101156cd587Sjoerg    movhi   r0,         #1
102156cd587Sjoerg    JMP(lr)
103156cd587SjoergEND_COMPILERRT_FUNCTION(__eqsf2)
104156cd587SjoergDEFINE_COMPILERRT_FUNCTION_ALIAS(__lesf2, __eqsf2)
105156cd587SjoergDEFINE_COMPILERRT_FUNCTION_ALIAS(__ltsf2, __eqsf2)
106156cd587SjoergDEFINE_COMPILERRT_FUNCTION_ALIAS(__nesf2, __eqsf2)
107156cd587Sjoerg
10861f2f256Sjoerg.p2align 2
109156cd587SjoergDEFINE_COMPILERRT_FUNCTION(__gtsf2)
110*30308f42Sjoerg    // Identical to the preceding except in that we return -1 for NaN values.
111156cd587Sjoerg    // Given that the two paths share so much code, one might be tempted to
112156cd587Sjoerg    // unify them; however, the extra code needed to do so makes the code size
113156cd587Sjoerg    // to performance tradeoff very hard to justify for such small functions.
114156cd587Sjoerg    mov     r2,         r0, lsl #1
115156cd587Sjoerg    mov     r3,         r1, lsl #1
116156cd587Sjoerg    orrs    r12,    r2, r3, lsr #1
117156cd587Sjoerg    it ne
118156cd587Sjoerg    eorsne  r12,    r0, r1
119156cd587Sjoerg    it pl
120156cd587Sjoerg    subspl  r0,     r2, r3
121156cd587Sjoerg    it lo
122156cd587Sjoerg    mvnlo   r0,         r1, asr #31
123156cd587Sjoerg    it hi
124156cd587Sjoerg    movhi   r0,         r1, asr #31
125156cd587Sjoerg    it ne
126156cd587Sjoerg    orrne   r0,     r0, #1
127156cd587Sjoerg    cmp     r2,         #0xff000000
128156cd587Sjoerg    ite ls
129156cd587Sjoerg    cmpls   r3,         #0xff000000
130156cd587Sjoerg    movhi   r0,         #-1
131156cd587Sjoerg    JMP(lr)
132156cd587SjoergEND_COMPILERRT_FUNCTION(__gtsf2)
133156cd587SjoergDEFINE_COMPILERRT_FUNCTION_ALIAS(__gesf2, __gtsf2)
134156cd587Sjoerg
13561f2f256Sjoerg.p2align 2
136156cd587SjoergDEFINE_COMPILERRT_FUNCTION(__unordsf2)
137156cd587Sjoerg    // Return 1 for NaN values, 0 otherwise.
138156cd587Sjoerg    mov     r2,         r0, lsl #1
139156cd587Sjoerg    mov     r3,         r1, lsl #1
140156cd587Sjoerg    mov     r0,         #0
141156cd587Sjoerg    cmp     r2,         #0xff000000
142156cd587Sjoerg    ite ls
143156cd587Sjoerg    cmpls   r3,         #0xff000000
144156cd587Sjoerg    movhi   r0,         #1
145156cd587Sjoerg    JMP(lr)
146156cd587SjoergEND_COMPILERRT_FUNCTION(__unordsf2)
147156cd587Sjoerg
148156cd587SjoergDEFINE_AEABI_FUNCTION_ALIAS(__aeabi_fcmpun, __unordsf2)
149