comparesf2.S revision 214152
1214152Sed//===-- comparesf2.S - Implement single-precision soft-float comparisons --===// 2214152Sed// 3214152Sed// The LLVM Compiler Infrastructure 4214152Sed// 5214152Sed// This file is distributed under the University of Illinois Open Source 6214152Sed// License. See LICENSE.TXT for details. 7214152Sed// 8214152Sed//===----------------------------------------------------------------------===// 9214152Sed// 10214152Sed// This file implements the following soft-fp_t comparison routines: 11214152Sed// 12214152Sed// __eqsf2 __gesf2 __unordsf2 13214152Sed// __lesf2 __gtsf2 14214152Sed// __ltsf2 15214152Sed// __nesf2 16214152Sed// 17214152Sed// The semantics of the routines grouped in each column are identical, so there 18214152Sed// is a single implementation for each, with multiple names. 19214152Sed// 20214152Sed// The routines behave as follows: 21214152Sed// 22214152Sed// __lesf2(a,b) returns -1 if a < b 23214152Sed// 0 if a == b 24214152Sed// 1 if a > b 25214152Sed// 1 if either a or b is NaN 26214152Sed// 27214152Sed// __gesf2(a,b) returns -1 if a < b 28214152Sed// 0 if a == b 29214152Sed// 1 if a > b 30214152Sed// -1 if either a or b is NaN 31214152Sed// 32214152Sed// __unordsf2(a,b) returns 0 if both a and b are numbers 33214152Sed// 1 if either a or b is NaN 34214152Sed// 35214152Sed// Note that __lesf2( ) and __gesf2( ) are identical except in their handling of 36214152Sed// NaN values. 37214152Sed// 38214152Sed//===----------------------------------------------------------------------===// 39214152Sed 40214152Sed#include "../assembly.h" 41214152Sed.syntax unified 42214152Sed 43214152Sed.align 2 44214152SedDEFINE_COMPILERRT_FUNCTION(__eqsf2) 45214152SedDEFINE_COMPILERRT_FUNCTION(__lesf2) 46214152SedDEFINE_COMPILERRT_FUNCTION(__ltsf2) 47214152SedDEFINE_COMPILERRT_FUNCTION(__nesf2) 48214152Sed // Make copies of a and b with the sign bit shifted off the top. These will 49214152Sed // be used to detect zeros and NaNs. 50214152Sed mov r2, r0, lsl #1 51214152Sed mov r3, r1, lsl #1 52214152Sed 53214152Sed // We do the comparison in three stages (ignoring NaN values for the time 54214152Sed // being). First, we orr the absolute values of a and b; this sets the Z 55214152Sed // flag if both a and b are zero (of either sign). The shift of r3 doesn't 56214152Sed // effect this at all, but it *does* make sure that the C flag is clear for 57214152Sed // the subsequent operations. 58214152Sed orrs r12, r2, r3, lsr #1 59214152Sed 60214152Sed // Next, we check if a and b have the same or different signs. If they have 61214152Sed // opposite signs, this eor will set the N flag. 62214152Sed eorsne r12, r0, r1 63214152Sed 64214152Sed // If a and b are equal (either both zeros or bit identical; again, we're 65214152Sed // ignoring NaNs for now), this subtract will zero out r0. If they have the 66214152Sed // same sign, the flags are updated as they would be for a comparison of the 67214152Sed // absolute values of a and b. 68214152Sed subspl r0, r2, r3 69214152Sed 70214152Sed // If a is smaller in magnitude than b and both have the same sign, place 71214152Sed // the negation of the sign of b in r0. Thus, if both are negative and 72214152Sed // a > b, this sets r0 to 0; if both are positive and a < b, this sets 73214152Sed // r0 to -1. 74214152Sed // 75214152Sed // This is also done if a and b have opposite signs and are not both zero, 76214152Sed // because in that case the subtract was not performed and the C flag is 77214152Sed // still clear from the shift argument in orrs; if a is positive and b 78214152Sed // negative, this places 0 in r0; if a is negative and b positive, -1 is 79214152Sed // placed in r0. 80214152Sed mvnlo r0, r1, asr #31 81214152Sed 82214152Sed // If a is greater in magnitude than b and both have the same sign, place 83214152Sed // the sign of b in r0. Thus, if both are negative and a < b, -1 is placed 84214152Sed // in r0, which is the desired result. Conversely, if both are positive 85214152Sed // and a > b, zero is placed in r0. 86214152Sed movhi r0, r1, asr #31 87214152Sed 88214152Sed // If you've been keeping track, at this point r0 contains -1 if a < b and 89214152Sed // 0 if a >= b. All that remains to be done is to set it to 1 if a > b. 90214152Sed // If a == b, then the Z flag is set, so we can get the correct final value 91214152Sed // into r0 by simply or'ing with 1 if Z is clear. 92214152Sed orrne r0, r0, #1 93214152Sed 94214152Sed // Finally, we need to deal with NaNs. If either argument is NaN, replace 95214152Sed // the value in r0 with 1. 96214152Sed cmp r2, #0xff000000 97214152Sed cmpls r3, #0xff000000 98214152Sed movhi r0, #1 99214152Sed bx lr 100214152Sed 101214152Sed.align 2 102214152SedDEFINE_COMPILERRT_FUNCTION(__gesf2) 103214152SedDEFINE_COMPILERRT_FUNCTION(__gtsf2) 104214152Sed // Identical to the preceeding except in that we return -1 for NaN values. 105214152Sed // Given that the two paths share so much code, one might be tempted to 106214152Sed // unify them; however, the extra code needed to do so makes the code size 107214152Sed // to performance tradeoff very hard to justify for such small functions. 108214152Sed mov r2, r0, lsl #1 109214152Sed mov r3, r1, lsl #1 110214152Sed orrs r12, r2, r3, lsr #1 111214152Sed eorsne r12, r0, r1 112214152Sed subspl r0, r2, r3 113214152Sed mvnlo r0, r1, asr #31 114214152Sed movhi r0, r1, asr #31 115214152Sed orrne r0, r0, #1 116214152Sed cmp r2, #0xff000000 117214152Sed cmpls r3, #0xff000000 118214152Sed movhi r0, #-1 119214152Sed bx lr 120214152Sed 121214152Sed.align 2 122214152SedDEFINE_COMPILERRT_FUNCTION(__unordsf2) 123214152Sed // Return 1 for NaN values, 0 otherwise. 124214152Sed mov r2, r0, lsl #1 125214152Sed mov r3, r1, lsl #1 126214152Sed mov r0, #0 127214152Sed cmp r2, #0xff000000 128214152Sed cmpls r3, #0xff000000 129214152Sed movhi r0, #1 130214152Sed bx lr 131