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