Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
7.4 KiB

  1. /* file: round.c */
  2. /*
  3. **
  4. ** COPYRIGHT (c) 1989 BY
  5. ** DIGITAL EQUIPMENT CORPORATION, MAYNARD, MASSACHUSETTS.
  6. ** ALL RIGHTS RESERVED.
  7. **
  8. ** THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND COPIED
  9. ** ONLY IN ACCORDANCE WITH THE TERMS OF SUCH LICENSE AND WITH THE
  10. ** INCLUSION OF THE ABOVE COPYRIGHT NOTICE. THIS SOFTWARE OR ANY OTHER
  11. ** COPIES THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
  12. ** OTHER PERSON. NO TITLE TO AND OWNERSHIP OF THE SOFTWARE IS HEREBY
  13. ** TRANSFERRED.
  14. **
  15. ** THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE
  16. ** AND SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT
  17. ** CORPORATION.
  18. **
  19. ** DIGITAL ASSUMES NO RESPONSIBILITY FOR THE USE OR RELIABILITY OF ITS
  20. ** SOFTWARE ON EQUIPMENT WHICH IS NOT SUPPLIED BY DIGITAL.
  21. **
  22. */
  23. /*
  24. **++
  25. ** Facility:
  26. **
  27. ** CVT Run-Time Library
  28. **
  29. ** Abstract:
  30. **
  31. ** This module is an include file.
  32. **
  33. ** This module rounds CVT floating point data to any specified position.
  34. ** Any of the following rounding modes can be applied:
  35. **
  36. ** Note: None of the following implementations ever perform true truncation
  37. ** on their values. Whenever truncation becomes necessary - either
  38. ** by being specified directly or by being required indirectly
  39. ** through rounding - values are actually left untouched. Users
  40. ** of this routine must zero out fractional fields themselves if
  41. ** true truncation is needed.
  42. **
  43. ** VAX ROUNDING
  44. **
  45. ** Input data are rounded such that the representable value nearest
  46. ** the infinitely precise result is delivered; if two representable
  47. ** values are equally near, the one greatest in magnitude is
  48. ** delivered.
  49. **
  50. ** ROUND TO NEAREST
  51. **
  52. ** Input data are rounded such that the representable value nearest
  53. ** the infinitely precise result is delivered; if two representable
  54. ** values are equally near, the one with its least significant bit
  55. ** zero is delivered.
  56. **
  57. ** ROUND TO POSITIVE INFINITY
  58. **
  59. ** Input data are rounded such that the representable value closest
  60. ** to and no less than the infinitely precise result is delivered.
  61. **
  62. ** ROUND TO NEGATIVE INFINITY
  63. **
  64. ** Input data are rounded such that the representable value closest
  65. ** to and no greater than the infinitely precise result is
  66. ** delivered.
  67. **
  68. ** TRUNCATION (ROUND TOWARDS ZERO)
  69. **
  70. ** True truncation is not implemented here. Input values are
  71. ** delivered in their original, untouched form.
  72. **
  73. ** A definition of "true" truncation follows: Truncation, or
  74. ** rounding towards zero, implies input data are rounded such
  75. ** that the representable value closest to and no greater in
  76. ** magnitude than the infinitely precise result is delivered.
  77. **
  78. ** Authors:
  79. **
  80. ** Math RTL
  81. **
  82. ** Creation Date: December 5, 1989.
  83. **
  84. ** Modification History:
  85. **
  86. ** 1-001 Original created.
  87. ** MRTL 5-Dec-1989.
  88. **
  89. **--
  90. */
  91. /*
  92. **
  93. ** Implicit input/output:
  94. **
  95. ** r On input, a valid CVT floating point number.
  96. ** On output, a rounded representation of the
  97. ** input.
  98. **
  99. **
  100. ** Implicit input:
  101. **
  102. ** round_bit_position An integer specifying the position to round to.
  103. ** 0 <= round_bit_position <= 127.
  104. **
  105. ** Note: Valid CVT mantissa bits are addressed as 1
  106. ** through 128. Accordingly, specifying 0 as a
  107. ** position to round to implies an exponent
  108. ** increase whenever rounding occurs. As for
  109. ** truncation: truncation allways leaves a CVT
  110. ** number untouched.
  111. **
  112. ** options A valid CVT options bit mask in which at least
  113. ** one, and only one, CVT rounding mode is
  114. ** specified. If no rounding mode is specified,
  115. ** results are unpredictable. Rounding is
  116. ** performed in accordance with this mask.
  117. **
  118. ** i An uninitialized integer used for indexing.
  119. **
  120. **
  121. ** Note: for efficiency this routine performs no explicit error checking.
  122. **
  123. */
  124. {
  125. int roundup, more_bits;
  126. unsigned long bit_mask;
  127. /* Check TRUNCATE option */
  128. if ( ! (options & CVT_C_TRUNCATE) ) {
  129. /* Determine which word the round bit resides in */
  130. i = (round_bit_position >> 5) + 1;
  131. /* Create a mask isolating the round bit */
  132. bit_mask = 0x1L << (31 - (round_bit_position & 0x1FL));
  133. /* Check VAX ROUNDING option */
  134. if (options & CVT_C_VAX_ROUNDING)
  135. roundup = r[i] & bit_mask;
  136. else {
  137. roundup = 0;
  138. switch ( r[i] & bit_mask ) {
  139. /* If round bit is clear, and ROUND TO NEAREST option */
  140. /* is selected we truncate */
  141. case 0 : if (options & CVT_C_ROUND_TO_NEAREST)
  142. break;
  143. /* Otherwise, make note of wheather there are any bits set */
  144. /* after the round bit, and then check the remaining cases */
  145. default : if ( ! (more_bits = r[i] & (bit_mask - 1)) )
  146. switch ( i ) {
  147. case 1 : more_bits = r[2];
  148. case 2 : more_bits |= r[3];
  149. case 3 : more_bits |= r[4];
  150. default : break;
  151. }
  152. /* Re-check ROUND TO NEAREST option. NOTE: if we've reached */
  153. /* this point and ROUND TO NEAREST has been selected, the */
  154. /* round bit is set. */
  155. if (options & CVT_C_ROUND_TO_NEAREST) {
  156. if ( ! ( roundup = more_bits ) )
  157. if ( bit_mask << 1 )
  158. roundup = r[i] & (bit_mask << 1);
  159. else if (i != 1)
  160. roundup = r[i-1] & 1;
  161. /* Check ROUND TO POSITIVE INFINITY option */
  162. } else if (options & CVT_C_ROUND_TO_POS) {
  163. if ( !(r[U_R_FLAGS] & U_R_NEGATIVE) )
  164. roundup = (r[i] & bit_mask) | more_bits;
  165. /* Check ROUND TO NEGITIVE INFINITY option */
  166. } else if (r[U_R_FLAGS] & U_R_NEGATIVE)
  167. roundup = (r[i] & bit_mask) | more_bits;
  168. }
  169. }
  170. if ( roundup ) { /* Perform rounding if necessary */
  171. /* Add 1 at round position */
  172. bit_mask <<= 1;
  173. r[i] = (r[i] & ~(bit_mask - 1)) + bit_mask;
  174. /* Propagate any carry */
  175. while ( ! r[i] )
  176. r[--i] += 1;
  177. /* If carry reaches exponent MSB gets zeroed and must be reset */
  178. if ( ! i )
  179. r[1] = 0x80000000L;
  180. }
  181. }
  182. }