Home | History | Annotate | Download | only in builtins
      1 //===-- lib/comparetf2.c - Quad-precision comparisons -------------*- C -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // // This file implements the following soft-float comparison routines:
     11 //
     12 //   __eqtf2   __getf2   __unordtf2
     13 //   __letf2   __gttf2
     14 //   __lttf2
     15 //   __netf2
     16 //
     17 // The semantics of the routines grouped in each column are identical, so there
     18 // is a single implementation for each, and wrappers to provide the other names.
     19 //
     20 // The main routines behave as follows:
     21 //
     22 //   __letf2(a,b) returns -1 if a < b
     23 //                         0 if a == b
     24 //                         1 if a > b
     25 //                         1 if either a or b is NaN
     26 //
     27 //   __getf2(a,b) returns -1 if a < b
     28 //                         0 if a == b
     29 //                         1 if a > b
     30 //                        -1 if either a or b is NaN
     31 //
     32 //   __unordtf2(a,b) returns 0 if both a and b are numbers
     33 //                           1 if either a or b is NaN
     34 //
     35 // Note that __letf2( ) and __getf2( ) are identical except in their handling of
     36 // NaN values.
     37 //
     38 //===----------------------------------------------------------------------===//
     39 
     40 #define QUAD_PRECISION
     41 #include "fp_lib.h"
     42 
     43 #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)
     44 enum LE_RESULT {
     45     LE_LESS      = -1,
     46     LE_EQUAL     =  0,
     47     LE_GREATER   =  1,
     48     LE_UNORDERED =  1
     49 };
     50 
     51 COMPILER_RT_ABI enum LE_RESULT __letf2(fp_t a, fp_t b) {
     52 
     53     const srep_t aInt = toRep(a);
     54     const srep_t bInt = toRep(b);
     55     const rep_t aAbs = aInt & absMask;
     56     const rep_t bAbs = bInt & absMask;
     57 
     58     // If either a or b is NaN, they are unordered.
     59     if (aAbs > infRep || bAbs > infRep) return LE_UNORDERED;
     60 
     61     // If a and b are both zeros, they are equal.
     62     if ((aAbs | bAbs) == 0) return LE_EQUAL;
     63 
     64     // If at least one of a and b is positive, we get the same result comparing
     65     // a and b as signed integers as we would with a floating-point compare.
     66     if ((aInt & bInt) >= 0) {
     67         if (aInt < bInt) return LE_LESS;
     68         else if (aInt == bInt) return LE_EQUAL;
     69         else return LE_GREATER;
     70     }
     71     else {
     72         // Otherwise, both are negative, so we need to flip the sense of the
     73         // comparison to get the correct result.  (This assumes a twos- or ones-
     74         // complement integer representation; if integers are represented in a
     75         // sign-magnitude representation, then this flip is incorrect).
     76         if (aInt > bInt) return LE_LESS;
     77         else if (aInt == bInt) return LE_EQUAL;
     78         else return LE_GREATER;
     79     }
     80 }
     81 
     82 #if defined(__ELF__)
     83 // Alias for libgcc compatibility
     84 FNALIAS(__cmptf2, __letf2);
     85 #endif
     86 
     87 enum GE_RESULT {
     88     GE_LESS      = -1,
     89     GE_EQUAL     =  0,
     90     GE_GREATER   =  1,
     91     GE_UNORDERED = -1   // Note: different from LE_UNORDERED
     92 };
     93 
     94 COMPILER_RT_ABI enum GE_RESULT __getf2(fp_t a, fp_t b) {
     95 
     96     const srep_t aInt = toRep(a);
     97     const srep_t bInt = toRep(b);
     98     const rep_t aAbs = aInt & absMask;
     99     const rep_t bAbs = bInt & absMask;
    100 
    101     if (aAbs > infRep || bAbs > infRep) return GE_UNORDERED;
    102     if ((aAbs | bAbs) == 0) return GE_EQUAL;
    103     if ((aInt & bInt) >= 0) {
    104         if (aInt < bInt) return GE_LESS;
    105         else if (aInt == bInt) return GE_EQUAL;
    106         else return GE_GREATER;
    107     } else {
    108         if (aInt > bInt) return GE_LESS;
    109         else if (aInt == bInt) return GE_EQUAL;
    110         else return GE_GREATER;
    111     }
    112 }
    113 
    114 COMPILER_RT_ABI int __unordtf2(fp_t a, fp_t b) {
    115     const rep_t aAbs = toRep(a) & absMask;
    116     const rep_t bAbs = toRep(b) & absMask;
    117     return aAbs > infRep || bAbs > infRep;
    118 }
    119 
    120 // The following are alternative names for the preceding routines.
    121 
    122 COMPILER_RT_ABI enum LE_RESULT __eqtf2(fp_t a, fp_t b) {
    123     return __letf2(a, b);
    124 }
    125 
    126 COMPILER_RT_ABI enum LE_RESULT __lttf2(fp_t a, fp_t b) {
    127     return __letf2(a, b);
    128 }
    129 
    130 COMPILER_RT_ABI enum LE_RESULT __netf2(fp_t a, fp_t b) {
    131     return __letf2(a, b);
    132 }
    133 
    134 COMPILER_RT_ABI enum GE_RESULT __gttf2(fp_t a, fp_t b) {
    135     return __getf2(a, b);
    136 }
    137 
    138 #endif
    139