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