comparetf2.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #define QUAD_PRECISION
  40. #include "fp_lib.h"
  41. #if defined(CRT_HAS_128BIT) && defined(CRT_LDBL_128BIT)
  42. enum LE_RESULT {
  43. LE_LESS = -1,
  44. LE_EQUAL = 0,
  45. LE_GREATER = 1,
  46. LE_UNORDERED = 1
  47. };
  48. COMPILER_RT_ABI enum LE_RESULT __letf2(fp_t a, fp_t b) {
  49. const srep_t aInt = toRep(a);
  50. const srep_t bInt = toRep(b);
  51. const rep_t aAbs = aInt & absMask;
  52. const rep_t bAbs = bInt & absMask;
  53. // If either a or b is NaN, they are unordered.
  54. if (aAbs > infRep || bAbs > infRep) return LE_UNORDERED;
  55. // If a and b are both zeros, they are equal.
  56. if ((aAbs | bAbs) == 0) return LE_EQUAL;
  57. // If at least one of a and b is positive, we get the same result comparing
  58. // a and b as signed integers as we would with a floating-point compare.
  59. if ((aInt & bInt) >= 0) {
  60. if (aInt < bInt) return LE_LESS;
  61. else if (aInt == bInt) return LE_EQUAL;
  62. else return LE_GREATER;
  63. }
  64. else {
  65. // Otherwise, both are negative, so we need to flip the sense of the
  66. // comparison to get the correct result. (This assumes a twos- or ones-
  67. // complement integer representation; if integers are represented in a
  68. // sign-magnitude representation, then this flip is incorrect).
  69. if (aInt > bInt) return LE_LESS;
  70. else if (aInt == bInt) return LE_EQUAL;
  71. else return LE_GREATER;
  72. }
  73. }
  74. #if defined(__ELF__)
  75. // Alias for libgcc compatibility
  76. FNALIAS(__cmptf2, __letf2);
  77. #endif
  78. enum GE_RESULT {
  79. GE_LESS = -1,
  80. GE_EQUAL = 0,
  81. GE_GREATER = 1,
  82. GE_UNORDERED = -1 // Note: different from LE_UNORDERED
  83. };
  84. COMPILER_RT_ABI enum GE_RESULT __getf2(fp_t a, fp_t b) {
  85. const srep_t aInt = toRep(a);
  86. const srep_t bInt = toRep(b);
  87. const rep_t aAbs = aInt & absMask;
  88. const rep_t bAbs = bInt & absMask;
  89. if (aAbs > infRep || bAbs > infRep) return GE_UNORDERED;
  90. if ((aAbs | bAbs) == 0) return GE_EQUAL;
  91. if ((aInt & bInt) >= 0) {
  92. if (aInt < bInt) return GE_LESS;
  93. else if (aInt == bInt) return GE_EQUAL;
  94. else return GE_GREATER;
  95. } else {
  96. if (aInt > bInt) return GE_LESS;
  97. else if (aInt == bInt) return GE_EQUAL;
  98. else return GE_GREATER;
  99. }
  100. }
  101. COMPILER_RT_ABI int __unordtf2(fp_t a, fp_t b) {
  102. const rep_t aAbs = toRep(a) & absMask;
  103. const rep_t bAbs = toRep(b) & absMask;
  104. return aAbs > infRep || bAbs > infRep;
  105. }
  106. // The following are alternative names for the preceding routines.
  107. COMPILER_RT_ABI enum LE_RESULT __eqtf2(fp_t a, fp_t b) {
  108. return __letf2(a, b);
  109. }
  110. COMPILER_RT_ABI enum LE_RESULT __lttf2(fp_t a, fp_t b) {
  111. return __letf2(a, b);
  112. }
  113. COMPILER_RT_ABI enum LE_RESULT __netf2(fp_t a, fp_t b) {
  114. return __letf2(a, b);
  115. }
  116. COMPILER_RT_ABI enum GE_RESULT __gttf2(fp_t a, fp_t b) {
  117. return __getf2(a, b);
  118. }
  119. #endif