fxp.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef JEMALLOC_INTERNAL_FXP_H
  2. #define JEMALLOC_INTERNAL_FXP_H
  3. /*
  4. * A simple fixed-point math implementation, supporting only unsigned values
  5. * (with overflow being an error).
  6. *
  7. * It's not in general safe to use floating point in core code, because various
  8. * libc implementations we get linked against can assume that malloc won't touch
  9. * floating point state and call it with an unusual calling convention.
  10. */
  11. /*
  12. * High 16 bits are the integer part, low 16 are the fractional part. Or
  13. * equivalently, repr == 2**16 * val, where we use "val" to refer to the
  14. * (imaginary) fractional representation of the true value.
  15. *
  16. * We pick a uint32_t here since it's convenient in some places to
  17. * double the representation size (i.e. multiplication and division use
  18. * 64-bit integer types), and a uint64_t is the largest type we're
  19. * certain is available.
  20. */
  21. typedef uint32_t fxp_t;
  22. #define FXP_INIT_INT(x) ((x) << 16)
  23. #define FXP_INIT_PERCENT(pct) (((pct) << 16) / 100)
  24. /*
  25. * Amount of precision used in parsing and printing numbers. The integer bound
  26. * is simply because the integer part of the number gets 16 bits, and so is
  27. * bounded by 65536.
  28. *
  29. * We use a lot of precision for the fractional part, even though most of it
  30. * gets rounded off; this lets us get exact values for the important special
  31. * case where the denominator is a small power of 2 (for instance,
  32. * 1/512 == 0.001953125 is exactly representable even with only 16 bits of
  33. * fractional precision). We need to left-shift by 16 before dividing by
  34. * 10**precision, so we pick precision to be floor(log(2**48)) = 14.
  35. */
  36. #define FXP_INTEGER_PART_DIGITS 5
  37. #define FXP_FRACTIONAL_PART_DIGITS 14
  38. /*
  39. * In addition to the integer and fractional parts of the number, we need to
  40. * include a null character and (possibly) a decimal point.
  41. */
  42. #define FXP_BUF_SIZE (FXP_INTEGER_PART_DIGITS + FXP_FRACTIONAL_PART_DIGITS + 2)
  43. static inline fxp_t
  44. fxp_add(fxp_t a, fxp_t b) {
  45. return a + b;
  46. }
  47. static inline fxp_t
  48. fxp_sub(fxp_t a, fxp_t b) {
  49. assert(a >= b);
  50. return a - b;
  51. }
  52. static inline fxp_t
  53. fxp_mul(fxp_t a, fxp_t b) {
  54. uint64_t unshifted = (uint64_t)a * (uint64_t)b;
  55. /*
  56. * Unshifted is (a.val * 2**16) * (b.val * 2**16)
  57. * == (a.val * b.val) * 2**32, but we want
  58. * (a.val * b.val) * 2 ** 16.
  59. */
  60. return (uint32_t)(unshifted >> 16);
  61. }
  62. static inline fxp_t
  63. fxp_div(fxp_t a, fxp_t b) {
  64. assert(b != 0);
  65. uint64_t unshifted = ((uint64_t)a << 32) / (uint64_t)b;
  66. /*
  67. * Unshifted is (a.val * 2**16) * (2**32) / (b.val * 2**16)
  68. * == (a.val / b.val) * (2 ** 32), which again corresponds to a right
  69. * shift of 16.
  70. */
  71. return (uint32_t)(unshifted >> 16);
  72. }
  73. static inline uint32_t
  74. fxp_round_down(fxp_t a) {
  75. return a >> 16;
  76. }
  77. static inline uint32_t
  78. fxp_round_nearest(fxp_t a) {
  79. uint32_t fractional_part = (a & ((1U << 16) - 1));
  80. uint32_t increment = (uint32_t)(fractional_part >= (1U << 15));
  81. return (a >> 16) + increment;
  82. }
  83. /*
  84. * Approximately computes x * frac, without the size limitations that would be
  85. * imposed by converting u to an fxp_t.
  86. */
  87. static inline size_t
  88. fxp_mul_frac(size_t x_orig, fxp_t frac) {
  89. assert(frac <= (1U << 16));
  90. /*
  91. * Work around an over-enthusiastic warning about type limits below (on
  92. * 32-bit platforms, a size_t is always less than 1ULL << 48).
  93. */
  94. uint64_t x = (uint64_t)x_orig;
  95. /*
  96. * If we can guarantee no overflow, multiply first before shifting, to
  97. * preserve some precision. Otherwise, shift first and then multiply.
  98. * In the latter case, we only lose the low 16 bits of a 48-bit number,
  99. * so we're still accurate to within 1/2**32.
  100. */
  101. if (x < (1ULL << 48)) {
  102. return (size_t)((x * frac) >> 16);
  103. } else {
  104. return (size_t)((x >> 16) * (uint64_t)frac);
  105. }
  106. }
  107. /*
  108. * Returns true on error. Otherwise, returns false and updates *ptr to point to
  109. * the first character not parsed (because it wasn't a digit).
  110. */
  111. bool fxp_parse(fxp_t *a, const char *ptr, char **end);
  112. void fxp_print(fxp_t a, char buf[FXP_BUF_SIZE]);
  113. #endif /* JEMALLOC_INTERNAL_FXP_H */