varianceDataMemoryDefinition.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. ===============================================================================
  3. This C source file is part of the SoftFloat IEC/IEEE Floating-point
  4. Arithmetic Package, Release 2.
  5. Written by John R. Hauser. This work was made possible in part by the
  6. International Computer Science Institute, located at Suite 600, 1947 Center
  7. Street, Berkeley, California 94704. Funding was partially provided by the
  8. National Science Foundation under grant MIP-9311980. The original version
  9. of this code was written as part of a project to build a fixed-point vector
  10. processor in collaboration with the University of California at Berkeley,
  11. overseen by Profs. Nelson Morgan and John Wawrzynek. More information
  12. is available through the web page
  13. http://www.jhauser.us/arithmetic/SoftFloat-2b/SoftFloat-source.txt
  14. THIS SOFTWARE IS DISTRIBUTED AS IS, FOR FREE. Although reasonable effort
  15. has been made to avoid it, THIS SOFTWARE MAY CONTAIN FAULTS THAT WILL AT
  16. TIMES RESULT IN INCORRECT BEHAVIOR. USE OF THIS SOFTWARE IS RESTRICTED TO
  17. PERSONS AND ORGANIZATIONS WHO CAN AND WILL TAKE FULL RESPONSIBILITY FOR ANY
  18. AND ALL LOSSES, COSTS, OR OTHER PROBLEMS ARISING FROM ITS USE.
  19. Derivative works are acceptable, even for commercial purposes, so long as
  20. (1) they include prominent notice that the work is derivative, and (2) they
  21. include prominent notice akin to these three paragraphs for those parts of
  22. this code that are retained.
  23. ===============================================================================
  24. */
  25. #include <asm/div64.h>
  26. #include "fpa11.h"
  27. //#include "milieu.h"
  28. //#include "softfloat.h"
  29. /*
  30. -------------------------------------------------------------------------------
  31. Primitive arithmetic functions, including multi-word arithmetic, and
  32. division and square root approximations. (Can be specialized to target if
  33. desired.)
  34. -------------------------------------------------------------------------------
  35. */
  36. #include "softfloat-macros"
  37. /*
  38. -------------------------------------------------------------------------------
  39. Functions and definitions to determine: (1) whether tininess for underflow
  40. is detected before or after rounding by default, (2) what (if anything)
  41. happens when exceptions are raised, (3) how signaling NaNs are distinguished
  42. from quiet NaNs, (4) the default generated quiet NaNs, and (5) how NaNs
  43. are propagated from function inputs to output. These details are target-
  44. specific.
  45. -------------------------------------------------------------------------------
  46. */
  47. #include "softfloat-specialize"
  48. /*
  49. -------------------------------------------------------------------------------
  50. Takes a 64-bit fixed-point value `absZ' with binary point between bits 6
  51. and 7, and returns the properly rounded 32-bit integer corresponding to the
  52. input. If `zSign' is nonzero, the input is negated before being converted
  53. to an integer. Bit 63 of `absZ' must be zero. Ordinarily, the fixed-point
  54. input is simply rounded to an integer, with the inexact exception raised if
  55. the input cannot be represented exactly as an integer. If the fixed-point
  56. input is too large, however, the invalid exception is raised and the largest
  57. positive or negative integer is returned.
  58. -------------------------------------------------------------------------------
  59. */
  60. static int32 roundAndPackInt32( struct roundingData *roundData, flag zSign, bits64 absZ )
  61. {
  62. int8 roundingMode;
  63. flag roundNearestEven;
  64. int8 roundIncrement, roundBits;
  65. int32 z;
  66. roundingMode = roundData->mode;
  67. roundNearestEven = ( roundingMode == float_round_nearest_even );
  68. roundIncrement = 0x40;
  69. if ( ! roundNearestEven ) {
  70. if ( roundingMode == float_round_to_zero ) {
  71. roundIncrement = 0;
  72. }
  73. else {
  74. roundIncrement = 0x7F;
  75. if ( zSign ) {
  76. if ( roundingMode == float_round_up ) roundIncrement = 0;
  77. }
  78. else {
  79. if ( roundingMode == float_round_down ) roundIncrement = 0;
  80. }
  81. }
  82. }
  83. roundBits = absZ & 0x7F;
  84. absZ = ( absZ + roundIncrement )>>7;
  85. absZ &= ~ ( ( ( roundBits ^ 0x40 ) == 0 ) & roundNearestEven );
  86. z = absZ;
  87. if ( zSign ) z = - z;
  88. if ( ( absZ>>32 ) || ( z && ( ( z < 0 ) ^ zSign ) ) ) {
  89. roundData->exception |= float_flag_invalid;
  90. return zSign ? 0x80000000 : 0x7FFFFFFF;
  91. }
  92. if ( roundBits ) roundData->exception |= float_flag_inexact;
  93. return z;
  94. }
  95. /*
  96. -------------------------------------------------------------------------------
  97. Returns the fraction bits of the single-precision floating-point value `a'.
  98. -------------------------------------------------------------------------------
  99. */
  100. INLINE bits32 extractFloat32Frac( float32 a )
  101. {
  102. return a & 0x007FFFFF;
  103. }
  104. /*
  105. -------------------------------------------------------------------------------
  106. Returns the exponent bits of the single-precision floating-point value `a'.
  107. -------------------------------------------------------------------------------
  108. */
  109. INLINE int16 extractFloat32Exp( float32 a )
  110. {
  111. return ( a>>23 ) & 0xFF;
  112. }
  113. /*
  114. -------------------------------------------------------------------------------
  115. Returns the sign bit of the single-precision floating-point value `a'.
  116. -------------------------------------------------------------------------------
  117. */
  118. #if 0 /* in softfloat.h */
  119. INLINE flag extractFloat32Sign( float32 a )
  120. {
  121. return a>>31;
  122. }
  123. #endif
  124. /*
  125. -------------------------------------------------------------------------------
  126. Normalizes the subnormal single-precision floating-point value represented
  127. by the denormalized significand `aSig'. The normalized exponent and
  128. significand are stored at the locations pointed to by `zExpPtr' and
  129. `zSigPtr', respectively.
  130. -------------------------------------------------------------------------------
  131. */
  132. static void
  133. normalizeFloat32Subnormal( bits32 aSig, int16 *zExpPtr, bits32 *zSigPtr )
  134. {
  135. int8 shiftCount;
  136. shiftCount = countLeadingZeros32( aSig ) - 8;
  137. *zSigPtr = aSig<<shiftCount;
  138. *zExpPtr = 1 - shiftCount;
  139. }
  140. /*
  141. -------------------------------------------------------------------------------
  142. Packs the sign `zSign', exponent `zExp', and significand `zSig' into a
  143. single-precision floating-point value, returning the result. After being
  144. shifted into the proper positions, the three fields are simply added
  145. together to form the result. This means that any integer portion of `zSig'
  146. will be added into the exponent. Since a properly normalized significand
  147. will have an integer portion equal to 1, the `zExp' input should be 1 less