Path: blob/main/contrib/llvm-project/compiler-rt/lib/builtins/arm/comparesf2.S
35291 views
//===-- comparesf2.S - Implement single-precision soft-float comparisons --===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements the following soft-fp_t comparison routines:9//10// __eqsf2 __gesf2 __unordsf211// __lesf2 __gtsf212// __ltsf213// __nesf214//15// The semantics of the routines grouped in each column are identical, so there16// is a single implementation for each, with multiple names.17//18// The routines behave as follows:19//20// __lesf2(a,b) returns -1 if a < b21// 0 if a == b22// 1 if a > b23// 1 if either a or b is NaN24//25// __gesf2(a,b) returns -1 if a < b26// 0 if a == b27// 1 if a > b28// -1 if either a or b is NaN29//30// __unordsf2(a,b) returns 0 if both a and b are numbers31// 1 if either a or b is NaN32//33// Note that __lesf2( ) and __gesf2( ) are identical except in their handling of34// NaN values.35//36//===----------------------------------------------------------------------===//3738#include "../assembly.h"3940.syntax unified41.text42DEFINE_CODE_STATE4344.macro COMPARESF2_FUNCTION_BODY handle_nan:req45#if defined(COMPILER_RT_ARMHF_TARGET)46vmov r0, s047vmov r1, s148#endif49// Make copies of a and b with the sign bit shifted off the top. These will50// be used to detect zeros and NaNs.51#if defined(USE_THUMB_1)52push {r6, lr}53lsls r2, r0, #154lsls r3, r1, #155#else56mov r2, r0, lsl #157mov r3, r1, lsl #158#endif5960// We do the comparison in three stages (ignoring NaN values for the time61// being). First, we orr the absolute values of a and b; this sets the Z62// flag if both a and b are zero (of either sign). The shift of r3 doesn't63// effect this at all, but it *does* make sure that the C flag is clear for64// the subsequent operations.65#if defined(USE_THUMB_1)66lsrs r6, r3, #167orrs r6, r268#else69orrs r12, r2, r3, lsr #170#endif71// Next, we check if a and b have the same or different signs. If they have72// opposite signs, this eor will set the N flag.73#if defined(USE_THUMB_1)74beq 1f75movs r6, r076eors r6, r1771:78#else79it ne80eorsne r12, r0, r181#endif8283// If a and b are equal (either both zeros or bit identical; again, we're84// ignoring NaNs for now), this subtract will zero out r0. If they have the85// same sign, the flags are updated as they would be for a comparison of the86// absolute values of a and b.87#if defined(USE_THUMB_1)88bmi 1f89subs r0, r2, r3901:91#else92it pl93subspl r0, r2, r394#endif9596// If a is smaller in magnitude than b and both have the same sign, place97// the negation of the sign of b in r0. Thus, if both are negative and98// a > b, this sets r0 to 0; if both are positive and a < b, this sets99// r0 to -1.100//101// This is also done if a and b have opposite signs and are not both zero,102// because in that case the subtract was not performed and the C flag is103// still clear from the shift argument in orrs; if a is positive and b104// negative, this places 0 in r0; if a is negative and b positive, -1 is105// placed in r0.106#if defined(USE_THUMB_1)107bhs 1f108// Here if a and b have the same sign and absA < absB, the result is thus109// b < 0 ? 1 : -1. Same if a and b have the opposite sign (ignoring Nan).110movs r0, #1111lsrs r1, #31112bne LOCAL_LABEL(CHECK_NAN\@)113negs r0, r0114b LOCAL_LABEL(CHECK_NAN\@)1151:116#else117it lo118mvnlo r0, r1, asr #31119#endif120121// If a is greater in magnitude than b and both have the same sign, place122// the sign of b in r0. Thus, if both are negative and a < b, -1 is placed123// in r0, which is the desired result. Conversely, if both are positive124// and a > b, zero is placed in r0.125#if defined(USE_THUMB_1)126bls 1f127// Here both have the same sign and absA > absB.128movs r0, #1129lsrs r1, #31130beq LOCAL_LABEL(CHECK_NAN\@)131negs r0, r01321:133#else134it hi135movhi r0, r1, asr #31136#endif137138// If you've been keeping track, at this point r0 contains -1 if a < b and139// 0 if a >= b. All that remains to be done is to set it to 1 if a > b.140// If a == b, then the Z flag is set, so we can get the correct final value141// into r0 by simply or'ing with 1 if Z is clear.142// For Thumb-1, r0 contains -1 if a < b, 0 if a > b and 0 if a == b.143#if !defined(USE_THUMB_1)144it ne145orrne r0, r0, #1146#endif147148// Finally, we need to deal with NaNs. If either argument is NaN, replace149// the value in r0 with 1.150#if defined(USE_THUMB_1)151LOCAL_LABEL(CHECK_NAN\@):152movs r6, #0xff153lsls r6, #24154cmp r2, r6155bhi 1f156cmp r3, r61571:158bls 2f159\handle_nan1602:161pop {r6, pc}162#else163cmp r2, #0xff000000164ite ls165cmpls r3, #0xff000000166\handle_nan167JMP(lr)168#endif169.endm170171@ int __eqsf2(float a, float b)172173.p2align 2174DEFINE_COMPILERRT_FUNCTION(__eqsf2)175176.macro __eqsf2_handle_nan177#if defined(USE_THUMB_1)178movs r0, #1179#else180movhi r0, #1181#endif182.endm183184COMPARESF2_FUNCTION_BODY __eqsf2_handle_nan185186END_COMPILERRT_FUNCTION(__eqsf2)187188DEFINE_COMPILERRT_FUNCTION_ALIAS(__lesf2, __eqsf2)189DEFINE_COMPILERRT_FUNCTION_ALIAS(__ltsf2, __eqsf2)190DEFINE_COMPILERRT_FUNCTION_ALIAS(__nesf2, __eqsf2)191192#if defined(__ELF__)193// Alias for libgcc compatibility194DEFINE_COMPILERRT_FUNCTION_ALIAS(__cmpsf2, __lesf2)195#endif196197@ int __gtsf2(float a, float b)198199.p2align 2200DEFINE_COMPILERRT_FUNCTION(__gtsf2)201202.macro __gtsf2_handle_nan203#if defined(USE_THUMB_1)204movs r0, #1205negs r0, r0206#else207movhi r0, #-1208#endif209.endm210211COMPARESF2_FUNCTION_BODY __gtsf2_handle_nan212213END_COMPILERRT_FUNCTION(__gtsf2)214215DEFINE_COMPILERRT_FUNCTION_ALIAS(__gesf2, __gtsf2)216217@ int __unordsf2(float a, float b)218219.p2align 2220DEFINE_COMPILERRT_FUNCTION(__unordsf2)221222#if defined(COMPILER_RT_ARMHF_TARGET)223vmov r0, s0224vmov r1, s1225#endif226// Return 1 for NaN values, 0 otherwise.227lsls r2, r0, #1228lsls r3, r1, #1229movs r0, #0230#if defined(USE_THUMB_1)231movs r1, #0xff232lsls r1, #24233cmp r2, r1234bhi 1f235cmp r3, r12361:237bls 2f238movs r0, #12392:240#else241cmp r2, #0xff000000242ite ls243cmpls r3, #0xff000000244movhi r0, #1245#endif246JMP(lr)247END_COMPILERRT_FUNCTION(__unordsf2)248249#if defined(COMPILER_RT_ARMHF_TARGET)250DEFINE_COMPILERRT_FUNCTION(__aeabi_fcmpun)251vmov s0, r0252vmov s1, r1253b SYMBOL_NAME(__unordsf2)254END_COMPILERRT_FUNCTION(__aeabi_fcmpun)255#else256DEFINE_AEABI_FUNCTION_ALIAS(__aeabi_fcmpun, __unordsf2)257#endif258259NO_EXEC_STACK_DIRECTIVE260261262263