dfcmp.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Linux/PA-RISC Project (http://www.parisc-linux.org/)
  4. *
  5. * Floating-point emulation code
  6. * Copyright (C) 2001 Hewlett-Packard (Paul Bame) <[email protected]>
  7. */
  8. /*
  9. * BEGIN_DESC
  10. *
  11. * File:
  12. * @(#) pa/spmath/dfcmp.c $Revision: 1.1 $
  13. *
  14. * Purpose:
  15. * dbl_cmp: compare two values
  16. *
  17. * External Interfaces:
  18. * dbl_fcmp(leftptr, rightptr, cond, status)
  19. *
  20. * Internal Interfaces:
  21. *
  22. * Theory:
  23. * <<please update with a overview of the operation of this file>>
  24. *
  25. * END_DESC
  26. */
  27. #include "float.h"
  28. #include "dbl_float.h"
  29. /*
  30. * dbl_cmp: compare two values
  31. */
  32. int
  33. dbl_fcmp (dbl_floating_point * leftptr, dbl_floating_point * rightptr,
  34. unsigned int cond, unsigned int *status)
  35. /* The predicate to be tested */
  36. {
  37. register unsigned int leftp1, leftp2, rightp1, rightp2;
  38. register int xorresult;
  39. /* Create local copies of the numbers */
  40. Dbl_copyfromptr(leftptr,leftp1,leftp2);
  41. Dbl_copyfromptr(rightptr,rightp1,rightp2);
  42. /*
  43. * Test for NaN
  44. */
  45. if( (Dbl_exponent(leftp1) == DBL_INFINITY_EXPONENT)
  46. || (Dbl_exponent(rightp1) == DBL_INFINITY_EXPONENT) )
  47. {
  48. /* Check if a NaN is involved. Signal an invalid exception when
  49. * comparing a signaling NaN or when comparing quiet NaNs and the
  50. * low bit of the condition is set */
  51. if( ((Dbl_exponent(leftp1) == DBL_INFINITY_EXPONENT)
  52. && Dbl_isnotzero_mantissa(leftp1,leftp2)
  53. && (Exception(cond) || Dbl_isone_signaling(leftp1)))
  54. ||
  55. ((Dbl_exponent(rightp1) == DBL_INFINITY_EXPONENT)
  56. && Dbl_isnotzero_mantissa(rightp1,rightp2)
  57. && (Exception(cond) || Dbl_isone_signaling(rightp1))) )
  58. {
  59. if( Is_invalidtrap_enabled() ) {
  60. Set_status_cbit(Unordered(cond));
  61. return(INVALIDEXCEPTION);
  62. }
  63. else Set_invalidflag();
  64. Set_status_cbit(Unordered(cond));
  65. return(NOEXCEPTION);
  66. }
  67. /* All the exceptional conditions are handled, now special case
  68. NaN compares */
  69. else if( ((Dbl_exponent(leftp1) == DBL_INFINITY_EXPONENT)
  70. && Dbl_isnotzero_mantissa(leftp1,leftp2))
  71. ||
  72. ((Dbl_exponent(rightp1) == DBL_INFINITY_EXPONENT)
  73. && Dbl_isnotzero_mantissa(rightp1,rightp2)) )
  74. {
  75. /* NaNs always compare unordered. */
  76. Set_status_cbit(Unordered(cond));
  77. return(NOEXCEPTION);
  78. }
  79. /* infinities will drop down to the normal compare mechanisms */
  80. }
  81. /* First compare for unequal signs => less or greater or
  82. * special equal case */
  83. Dbl_xortointp1(leftp1,rightp1,xorresult);
  84. if( xorresult < 0 )
  85. {
  86. /* left negative => less, left positive => greater.
  87. * equal is possible if both operands are zeros. */
  88. if( Dbl_iszero_exponentmantissa(leftp1,leftp2)
  89. && Dbl_iszero_exponentmantissa(rightp1,rightp2) )
  90. {
  91. Set_status_cbit(Equal(cond));
  92. }
  93. else if( Dbl_isone_sign(leftp1) )
  94. {
  95. Set_status_cbit(Lessthan(cond));
  96. }
  97. else
  98. {
  99. Set_status_cbit(Greaterthan(cond));
  100. }
  101. }
  102. /* Signs are the same. Treat negative numbers separately
  103. * from the positives because of the reversed sense. */
  104. else if(Dbl_isequal(leftp1,leftp2,rightp1,rightp2))
  105. {
  106. Set_status_cbit(Equal(cond));
  107. }
  108. else if( Dbl_iszero_sign(leftp1) )
  109. {
  110. /* Positive compare */
  111. if( Dbl_allp1(leftp1) < Dbl_allp1(rightp1) )
  112. {
  113. Set_status_cbit(Lessthan(cond));
  114. }
  115. else if( Dbl_allp1(leftp1) > Dbl_allp1(rightp1) )
  116. {
  117. Set_status_cbit(Greaterthan(cond));
  118. }
  119. else
  120. {
  121. /* Equal first parts. Now we must use unsigned compares to
  122. * resolve the two possibilities. */
  123. if( Dbl_allp2(leftp2) < Dbl_allp2(rightp2) )
  124. {
  125. Set_status_cbit(Lessthan(cond));
  126. }
  127. else
  128. {
  129. Set_status_cbit(Greaterthan(cond));
  130. }
  131. }
  132. }
  133. else
  134. {
  135. /* Negative compare. Signed or unsigned compares
  136. * both work the same. That distinction is only
  137. * important when the sign bits differ. */
  138. if( Dbl_allp1(leftp1) > Dbl_allp1(rightp1) )
  139. {
  140. Set_status_cbit(Lessthan(cond));
  141. }
  142. else if( Dbl_allp1(leftp1) < Dbl_allp1(rightp1) )
  143. {
  144. Set_status_cbit(Greaterthan(cond));
  145. }
  146. else
  147. {
  148. /* Equal first parts. Now we must use unsigned compares to
  149. * resolve the two possibilities. */
  150. if( Dbl_allp2(leftp2) > Dbl_allp2(rightp2) )
  151. {
  152. Set_status_cbit(Lessthan(cond));
  153. }
  154. else
  155. {
  156. Set_status_cbit(Greaterthan(cond));
  157. }
  158. }
  159. }
  160. return(NOEXCEPTION);
  161. }