cosmopolitan/third_party/mbedtls/fastdiv.h
Justine Tunney fa20edc44d
Reduce header complexity
- Remove most __ASSEMBLER__ __LINKER__ ifdefs
- Rename libc/intrin/bits.h to libc/serialize.h
- Block pthread cancelation in fchmodat() polyfill
- Remove `clang-format off` statements in third_party
2023-11-28 14:39:42 -08:00

32 lines
716 B
C

#ifndef COSMOPOLITAN_THIRD_PARTY_MBEDTLS_FASTDIV_H_
#define COSMOPOLITAN_THIRD_PARTY_MBEDTLS_FASTDIV_H_
#include "libc/macros.internal.h"
COSMOPOLITAN_C_START_
struct Divisor {
uint64_t m;
uint8_t s;
uint8_t t;
};
static inline struct Divisor GetDivisor(uint64_t d) {
int b;
uint128_t x;
b = __builtin_clzll(d) ^ 63;
x = -d & (((1ull << b) - 1) | (1ull << b));
return (struct Divisor){(x << 64) / d + 1, MIN(1, b + 1), MAX(0, b)};
}
forceinline uint64_t Divide(uint64_t x, struct Divisor d) {
uint128_t t;
uint64_t l, h;
t = d.m;
t *= x;
l = t;
h = t >> 64;
l = (x - h) >> d.s;
return (h + l) >> d.t;
}
COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_THIRD_PARTY_MBEDTLS_FASTDIV_H_ */