1//===-- lib/comparedf2.c - Double-precision comparisons -----------*- C -*-===// 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8// 9// // This file implements the following soft-float comparison routines: 10// 11// __eqdf2 __gedf2 __unorddf2 12// __ledf2 __gtdf2 13// __ltdf2 14// __nedf2 15// 16// The semantics of the routines grouped in each column are identical, so there 17// is a single implementation for each, and wrappers to provide the other names. 18// 19// The main routines behave as follows: 20// 21// __ledf2(a,b) returns -1 if a < b 22// 0 if a == b 23// 1 if a > b 24// 1 if either a or b is NaN 25// 26// __gedf2(a,b) returns -1 if a < b 27// 0 if a == b 28// 1 if a > b 29// -1 if either a or b is NaN 30// 31// __unorddf2(a,b) returns 0 if both a and b are numbers 32// 1 if either a or b is NaN 33// 34// Note that __ledf2( ) and __gedf2( ) are identical except in their handling of 35// NaN values. 36// 37//===----------------------------------------------------------------------===// 38 39#define DOUBLE_PRECISION 40#include "fp_lib.h" 41 42enum LE_RESULT { LE_LESS = -1, LE_EQUAL = 0, LE_GREATER = 1, LE_UNORDERED = 1 }; 43 44COMPILER_RT_ABI enum LE_RESULT __ledf2(fp_t a, fp_t b) { 45 46 const srep_t aInt = toRep(a); 47 const srep_t bInt = toRep(b); 48 const rep_t aAbs = aInt & absMask; 49 const rep_t bAbs = bInt & absMask; 50 51 // If either a or b is NaN, they are unordered. 52 if (aAbs > infRep || bAbs > infRep) 53 return LE_UNORDERED; 54 55 // If a and b are both zeros, they are equal. 56 if ((aAbs | bAbs) == 0) 57 return LE_EQUAL; 58 59 // If at least one of a and b is positive, we get the same result comparing 60 // a and b as signed integers as we would with a floating-point compare. 61 if ((aInt & bInt) >= 0) { 62 if (aInt < bInt) 63 return LE_LESS; 64 else if (aInt == bInt) 65 return LE_EQUAL; 66 else 67 return LE_GREATER; 68 } 69 70 // Otherwise, both are negative, so we need to flip the sense of the 71 // comparison to get the correct result. (This assumes a twos- or ones- 72 // complement integer representation; if integers are represented in a 73 // sign-magnitude representation, then this flip is incorrect). 74 else { 75 if (aInt > bInt) 76 return LE_LESS; 77 else if (aInt == bInt) 78 return LE_EQUAL; 79 else 80 return LE_GREATER; 81 } 82} 83 84#if defined(__ELF__) 85// Alias for libgcc compatibility 86COMPILER_RT_ALIAS(__ledf2, __cmpdf2) 87#endif 88COMPILER_RT_ALIAS(__ledf2, __eqdf2) 89COMPILER_RT_ALIAS(__ledf2, __ltdf2) 90COMPILER_RT_ALIAS(__ledf2, __nedf2) 91 92enum GE_RESULT { 93 GE_LESS = -1, 94 GE_EQUAL = 0, 95 GE_GREATER = 1, 96 GE_UNORDERED = -1 // Note: different from LE_UNORDERED 97}; 98 99COMPILER_RT_ABI enum GE_RESULT __gedf2(fp_t a, fp_t b) { 100 101 const srep_t aInt = toRep(a); 102 const srep_t bInt = toRep(b); 103 const rep_t aAbs = aInt & absMask; 104 const rep_t bAbs = bInt & absMask; 105 106 if (aAbs > infRep || bAbs > infRep) 107 return GE_UNORDERED; 108 if ((aAbs | bAbs) == 0) 109 return GE_EQUAL; 110 if ((aInt & bInt) >= 0) { 111 if (aInt < bInt) 112 return GE_LESS; 113 else if (aInt == bInt) 114 return GE_EQUAL; 115 else 116 return GE_GREATER; 117 } else { 118 if (aInt > bInt) 119 return GE_LESS; 120 else if (aInt == bInt) 121 return GE_EQUAL; 122 else 123 return GE_GREATER; 124 } 125} 126 127COMPILER_RT_ALIAS(__gedf2, __gtdf2) 128 129COMPILER_RT_ABI int 130__unorddf2(fp_t a, fp_t b) { 131 const rep_t aAbs = toRep(a) & absMask; 132 const rep_t bAbs = toRep(b) & absMask; 133 return aAbs > infRep || bAbs > infRep; 134} 135 136#if defined(__ARM_EABI__) 137#if defined(COMPILER_RT_ARMHF_TARGET) 138AEABI_RTABI int __aeabi_dcmpun(fp_t a, fp_t b) { return __unorddf2(a, b); } 139#else 140COMPILER_RT_ALIAS(__unorddf2, __aeabi_dcmpun) 141#endif 142#endif 143 144#if defined(_WIN32) && !defined(__MINGW32__) 145// The alias mechanism doesn't work on Windows except for MinGW, so emit 146// wrapper functions. 147int __eqdf2(fp_t a, fp_t b) { return __ledf2(a, b); } 148int __ltdf2(fp_t a, fp_t b) { return __ledf2(a, b); } 149int __nedf2(fp_t a, fp_t b) { return __ledf2(a, b); } 150int __gtdf2(fp_t a, fp_t b) { return __gedf2(a, b); } 151#endif 152