char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
cff777f22
char/aliceai-t5-moe
1#ifndef HEX_FASTDIV_H 2#define HEX_FASTDIV_H 3 4// See https://gmplib.org/~tege/divcnst-pldi94.pdf figure 4.1. 5// Precompute mp (m' in the paper) and L such that division 6// can be computed using a multiply (high 32b of 64b result) 7// and a shift: 8// 9// n/d = (mulhi(n, mp) + n) >> L; 10struct fastdiv_values { 11uint32_t mp ; 12uint32_t l ; 13}; 14 15static inline struct fastdiv_values init_fastdiv_values (uint32_t d ) { 16struct fastdiv_values result = {0 ,0 }; 17// compute L = ceil(log2(d)); 18while (result .l < 32 && ((uint32_t )1 <<result .l )< d ) { 19++ (result .l ); 20 } 21 22result .mp = (uint32_t ) (((uint64_t )1 <<32 )* (((uint64_t )1 <<result .l )- d ) /d + 1 ); 23return result ; 24} 25 26static inline uint32_t fastdiv (uint32_t n ,const struct fastdiv_values * vals ) { 27// Compute high 32 bits of n * mp 28const uint32_t hi = (uint32_t ) (((uint64_t )n * vals -> mp ) >>32 );// mulhi(n, mp) 29// add n, apply bit shift 30return (hi + n ) >>vals -> l ; 31} 32 33static inline uint32_t fastmodulo (uint32_t n ,uint32_t d ,const struct fastdiv_values * vals ) { 34return n - fastdiv (n ,vals )* d ; 35} 36 37#endif /* HEX_FASTDIV_H */