libdivide.h 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  1. // libdivide.h - Optimized integer division
  2. // https://libdivide.com
  3. //
  4. // Copyright (C) 2010 - 2019 ridiculous_fish, <libdivide@ridiculousfish.com>
  5. // Copyright (C) 2016 - 2019 Kim Walisch, <kim.walisch@gmail.com>
  6. //
  7. // libdivide is dual-licensed under the Boost or zlib licenses.
  8. // You may use libdivide under the terms of either of these.
  9. // See LICENSE.txt for more details.
  10. #ifndef LIBDIVIDE_H
  11. #define LIBDIVIDE_H
  12. #define LIBDIVIDE_VERSION "3.0"
  13. #define LIBDIVIDE_VERSION_MAJOR 3
  14. #define LIBDIVIDE_VERSION_MINOR 0
  15. #include <stdint.h>
  16. #if defined(__cplusplus)
  17. #include <cstdlib>
  18. #include <cstdio>
  19. #include <type_traits>
  20. #else
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #endif
  24. #if defined(LIBDIVIDE_AVX512)
  25. #include <immintrin.h>
  26. #elif defined(LIBDIVIDE_AVX2)
  27. #include <immintrin.h>
  28. #elif defined(LIBDIVIDE_SSE2)
  29. #include <emmintrin.h>
  30. #endif
  31. #if defined(_MSC_VER)
  32. #include <intrin.h>
  33. // disable warning C4146: unary minus operator applied
  34. // to unsigned type, result still unsigned
  35. #pragma warning(disable: 4146)
  36. #define LIBDIVIDE_VC
  37. #endif
  38. #if !defined(__has_builtin)
  39. #define __has_builtin(x) 0
  40. #endif
  41. #if defined(__SIZEOF_INT128__)
  42. #define HAS_INT128_T
  43. // clang-cl on Windows does not yet support 128-bit division
  44. #if !(defined(__clang__) && defined(LIBDIVIDE_VC))
  45. #define HAS_INT128_DIV
  46. #endif
  47. #endif
  48. #if defined(__x86_64__) || defined(_M_X64)
  49. #define LIBDIVIDE_X86_64
  50. #endif
  51. #if defined(__i386__)
  52. #define LIBDIVIDE_i386
  53. #endif
  54. #if defined(__GNUC__) || defined(__clang__)
  55. #define LIBDIVIDE_GCC_STYLE_ASM
  56. #endif
  57. #if defined(__cplusplus) || defined(LIBDIVIDE_VC)
  58. #define LIBDIVIDE_FUNCTION __FUNCTION__
  59. #else
  60. #define LIBDIVIDE_FUNCTION __func__
  61. #endif
  62. #define LIBDIVIDE_ERROR(msg) \
  63. do { \
  64. fprintf(stderr, "libdivide.h:%d: %s(): Error: %s\n", \
  65. __LINE__, LIBDIVIDE_FUNCTION, msg); \
  66. exit(-1); \
  67. } while (0)
  68. #if defined(LIBDIVIDE_ASSERTIONS_ON)
  69. #define LIBDIVIDE_ASSERT(x) \
  70. do { \
  71. if (!(x)) { \
  72. fprintf(stderr, "libdivide.h:%d: %s(): Assertion failed: %s\n", \
  73. __LINE__, LIBDIVIDE_FUNCTION, #x); \
  74. exit(-1); \
  75. } \
  76. } while (0)
  77. #else
  78. #define LIBDIVIDE_ASSERT(x)
  79. #endif
  80. #ifdef __cplusplus
  81. namespace libdivide {
  82. #endif
  83. // pack divider structs to prevent compilers from padding.
  84. // This reduces memory usage by up to 43% when using a large
  85. // array of libdivide dividers and improves performance
  86. // by up to 10% because of reduced memory bandwidth.
  87. #pragma pack(push, 1)
  88. struct libdivide_u32_t {
  89. uint32_t magic;
  90. uint8_t more;
  91. };
  92. struct libdivide_s32_t {
  93. int32_t magic;
  94. uint8_t more;
  95. };
  96. struct libdivide_u64_t {
  97. uint64_t magic;
  98. uint8_t more;
  99. };
  100. struct libdivide_s64_t {
  101. int64_t magic;
  102. uint8_t more;
  103. };
  104. struct libdivide_u32_branchfree_t {
  105. uint32_t magic;
  106. uint8_t more;
  107. };
  108. struct libdivide_s32_branchfree_t {
  109. int32_t magic;
  110. uint8_t more;
  111. };
  112. struct libdivide_u64_branchfree_t {
  113. uint64_t magic;
  114. uint8_t more;
  115. };
  116. struct libdivide_s64_branchfree_t {
  117. int64_t magic;
  118. uint8_t more;
  119. };
  120. #pragma pack(pop)
  121. // Explanation of the "more" field:
  122. //
  123. // * Bits 0-5 is the shift value (for shift path or mult path).
  124. // * Bit 6 is the add indicator for mult path.
  125. // * Bit 7 is set if the divisor is negative. We use bit 7 as the negative
  126. // divisor indicator so that we can efficiently use sign extension to
  127. // create a bitmask with all bits set to 1 (if the divisor is negative)
  128. // or 0 (if the divisor is positive).
  129. //
  130. // u32: [0-4] shift value
  131. // [5] ignored
  132. // [6] add indicator
  133. // magic number of 0 indicates shift path
  134. //
  135. // s32: [0-4] shift value
  136. // [5] ignored
  137. // [6] add indicator
  138. // [7] indicates negative divisor
  139. // magic number of 0 indicates shift path
  140. //
  141. // u64: [0-5] shift value
  142. // [6] add indicator
  143. // magic number of 0 indicates shift path
  144. //
  145. // s64: [0-5] shift value
  146. // [6] add indicator
  147. // [7] indicates negative divisor
  148. // magic number of 0 indicates shift path
  149. //
  150. // In s32 and s64 branchfree modes, the magic number is negated according to
  151. // whether the divisor is negated. In branchfree strategy, it is not negated.
  152. enum {
  153. LIBDIVIDE_32_SHIFT_MASK = 0x1F,
  154. LIBDIVIDE_64_SHIFT_MASK = 0x3F,
  155. LIBDIVIDE_ADD_MARKER = 0x40,
  156. LIBDIVIDE_NEGATIVE_DIVISOR = 0x80
  157. };
  158. static inline struct libdivide_s32_t libdivide_s32_gen(int32_t d);
  159. static inline struct libdivide_u32_t libdivide_u32_gen(uint32_t d);
  160. static inline struct libdivide_s64_t libdivide_s64_gen(int64_t d);
  161. static inline struct libdivide_u64_t libdivide_u64_gen(uint64_t d);
  162. static inline struct libdivide_s32_branchfree_t libdivide_s32_branchfree_gen(int32_t d);
  163. static inline struct libdivide_u32_branchfree_t libdivide_u32_branchfree_gen(uint32_t d);
  164. static inline struct libdivide_s64_branchfree_t libdivide_s64_branchfree_gen(int64_t d);
  165. static inline struct libdivide_u64_branchfree_t libdivide_u64_branchfree_gen(uint64_t d);
  166. static inline int32_t libdivide_s32_do(int32_t numer, const struct libdivide_s32_t *denom);
  167. static inline uint32_t libdivide_u32_do(uint32_t numer, const struct libdivide_u32_t *denom);
  168. static inline int64_t libdivide_s64_do(int64_t numer, const struct libdivide_s64_t *denom);
  169. static inline uint64_t libdivide_u64_do(uint64_t numer, const struct libdivide_u64_t *denom);
  170. static inline int32_t libdivide_s32_branchfree_do(int32_t numer, const struct libdivide_s32_branchfree_t *denom);
  171. static inline uint32_t libdivide_u32_branchfree_do(uint32_t numer, const struct libdivide_u32_branchfree_t *denom);
  172. static inline int64_t libdivide_s64_branchfree_do(int64_t numer, const struct libdivide_s64_branchfree_t *denom);
  173. static inline uint64_t libdivide_u64_branchfree_do(uint64_t numer, const struct libdivide_u64_branchfree_t *denom);
  174. static inline int32_t libdivide_s32_recover(const struct libdivide_s32_t *denom);
  175. static inline uint32_t libdivide_u32_recover(const struct libdivide_u32_t *denom);
  176. static inline int64_t libdivide_s64_recover(const struct libdivide_s64_t *denom);
  177. static inline uint64_t libdivide_u64_recover(const struct libdivide_u64_t *denom);
  178. static inline int32_t libdivide_s32_branchfree_recover(const struct libdivide_s32_branchfree_t *denom);
  179. static inline uint32_t libdivide_u32_branchfree_recover(const struct libdivide_u32_branchfree_t *denom);
  180. static inline int64_t libdivide_s64_branchfree_recover(const struct libdivide_s64_branchfree_t *denom);
  181. static inline uint64_t libdivide_u64_branchfree_recover(const struct libdivide_u64_branchfree_t *denom);
  182. //////// Internal Utility Functions
  183. static inline uint32_t libdivide_mullhi_u32(uint32_t x, uint32_t y) {
  184. uint64_t xl = x, yl = y;
  185. uint64_t rl = xl * yl;
  186. return (uint32_t)(rl >> 32);
  187. }
  188. static inline int32_t libdivide_mullhi_s32(int32_t x, int32_t y) {
  189. int64_t xl = x, yl = y;
  190. int64_t rl = xl * yl;
  191. // needs to be arithmetic shift
  192. return (int32_t)(rl >> 32);
  193. }
  194. static inline uint64_t libdivide_mullhi_u64(uint64_t x, uint64_t y) {
  195. #if defined(LIBDIVIDE_VC) && \
  196. defined(LIBDIVIDE_X86_64)
  197. return __umulh(x, y);
  198. #elif defined(HAS_INT128_T)
  199. __uint128_t xl = x, yl = y;
  200. __uint128_t rl = xl * yl;
  201. return (uint64_t)(rl >> 64);
  202. #else
  203. // full 128 bits are x0 * y0 + (x0 * y1 << 32) + (x1 * y0 << 32) + (x1 * y1 << 64)
  204. uint32_t mask = 0xFFFFFFFF;
  205. uint32_t x0 = (uint32_t)(x & mask);
  206. uint32_t x1 = (uint32_t)(x >> 32);
  207. uint32_t y0 = (uint32_t)(y & mask);
  208. uint32_t y1 = (uint32_t)(y >> 32);
  209. uint32_t x0y0_hi = libdivide_mullhi_u32(x0, y0);
  210. uint64_t x0y1 = x0 * (uint64_t)y1;
  211. uint64_t x1y0 = x1 * (uint64_t)y0;
  212. uint64_t x1y1 = x1 * (uint64_t)y1;
  213. uint64_t temp = x1y0 + x0y0_hi;
  214. uint64_t temp_lo = temp & mask;
  215. uint64_t temp_hi = temp >> 32;
  216. return x1y1 + temp_hi + ((temp_lo + x0y1) >> 32);
  217. #endif
  218. }
  219. static inline int64_t libdivide_mullhi_s64(int64_t x, int64_t y) {
  220. #if defined(LIBDIVIDE_VC) && \
  221. defined(LIBDIVIDE_X86_64)
  222. return __mulh(x, y);
  223. #elif defined(HAS_INT128_T)
  224. __int128_t xl = x, yl = y;
  225. __int128_t rl = xl * yl;
  226. return (int64_t)(rl >> 64);
  227. #else
  228. // full 128 bits are x0 * y0 + (x0 * y1 << 32) + (x1 * y0 << 32) + (x1 * y1 << 64)
  229. uint32_t mask = 0xFFFFFFFF;
  230. uint32_t x0 = (uint32_t)(x & mask);
  231. uint32_t y0 = (uint32_t)(y & mask);
  232. int32_t x1 = (int32_t)(x >> 32);
  233. int32_t y1 = (int32_t)(y >> 32);
  234. uint32_t x0y0_hi = libdivide_mullhi_u32(x0, y0);
  235. int64_t t = x1 * (int64_t)y0 + x0y0_hi;
  236. int64_t w1 = x0 * (int64_t)y1 + (t & mask);
  237. return x1 * (int64_t)y1 + (t >> 32) + (w1 >> 32);
  238. #endif
  239. }
  240. static inline int32_t libdivide_count_leading_zeros32(uint32_t val) {
  241. #if defined(__GNUC__) || \
  242. __has_builtin(__builtin_clz)
  243. // Fast way to count leading zeros
  244. return __builtin_clz(val);
  245. #elif defined(LIBDIVIDE_VC)
  246. unsigned long result;
  247. if (_BitScanReverse(&result, val)) {
  248. return 31 - result;
  249. }
  250. return 0;
  251. #else
  252. int32_t result = 0;
  253. uint32_t hi = 1U << 31;
  254. for (; ~val & hi; hi >>= 1) {
  255. result++;
  256. }
  257. return result;
  258. #endif
  259. }
  260. static inline int32_t libdivide_count_leading_zeros64(uint64_t val) {
  261. #if defined(__GNUC__) || \
  262. __has_builtin(__builtin_clzll)
  263. // Fast way to count leading zeros
  264. return __builtin_clzll(val);
  265. #elif defined(LIBDIVIDE_VC) && defined(_WIN64)
  266. unsigned long result;
  267. if (_BitScanReverse64(&result, val)) {
  268. return 63 - result;
  269. }
  270. return 0;
  271. #else
  272. uint32_t hi = val >> 32;
  273. uint32_t lo = val & 0xFFFFFFFF;
  274. if (hi != 0) return libdivide_count_leading_zeros32(hi);
  275. return 32 + libdivide_count_leading_zeros32(lo);
  276. #endif
  277. }
  278. // libdivide_64_div_32_to_32: divides a 64-bit uint {u1, u0} by a 32-bit
  279. // uint {v}. The result must fit in 32 bits.
  280. // Returns the quotient directly and the remainder in *r
  281. static inline uint32_t libdivide_64_div_32_to_32(uint32_t u1, uint32_t u0, uint32_t v, uint32_t *r) {
  282. #if (defined(LIBDIVIDE_i386) || defined(LIBDIVIDE_X86_64)) && \
  283. defined(LIBDIVIDE_GCC_STYLE_ASM)
  284. uint32_t result;
  285. __asm__("divl %[v]"
  286. : "=a"(result), "=d"(*r)
  287. : [v] "r"(v), "a"(u0), "d"(u1)
  288. );
  289. return result;
  290. #else
  291. uint64_t n = ((uint64_t)u1 << 32) | u0;
  292. uint32_t result = (uint32_t)(n / v);
  293. *r = (uint32_t)(n - result * (uint64_t)v);
  294. return result;
  295. #endif
  296. }
  297. // libdivide_128_div_64_to_64: divides a 128-bit uint {u1, u0} by a 64-bit
  298. // uint {v}. The result must fit in 64 bits.
  299. // Returns the quotient directly and the remainder in *r
  300. static uint64_t libdivide_128_div_64_to_64(uint64_t u1, uint64_t u0, uint64_t v, uint64_t *r) {
  301. #if defined(LIBDIVIDE_X86_64) && \
  302. defined(LIBDIVIDE_GCC_STYLE_ASM)
  303. uint64_t result;
  304. __asm__("divq %[v]"
  305. : "=a"(result), "=d"(*r)
  306. : [v] "r"(v), "a"(u0), "d"(u1)
  307. );
  308. return result;
  309. #elif defined(HAS_INT128_T) && \
  310. defined(HAS_INT128_DIV)
  311. __uint128_t n = ((__uint128_t)u1 << 64) | u0;
  312. uint64_t result = (uint64_t)(n / v);
  313. *r = (uint64_t)(n - result * (__uint128_t)v);
  314. return result;
  315. #else
  316. // Code taken from Hacker's Delight:
  317. // http://www.hackersdelight.org/HDcode/divlu.c.
  318. // License permits inclusion here per:
  319. // http://www.hackersdelight.org/permissions.htm
  320. const uint64_t b = (1ULL << 32); // Number base (32 bits)
  321. uint64_t un1, un0; // Norm. dividend LSD's
  322. uint64_t vn1, vn0; // Norm. divisor digits
  323. uint64_t q1, q0; // Quotient digits
  324. uint64_t un64, un21, un10; // Dividend digit pairs
  325. uint64_t rhat; // A remainder
  326. int32_t s; // Shift amount for norm
  327. // If overflow, set rem. to an impossible value,
  328. // and return the largest possible quotient
  329. if (u1 >= v) {
  330. *r = (uint64_t) -1;
  331. return (uint64_t) -1;
  332. }
  333. // count leading zeros
  334. s = libdivide_count_leading_zeros64(v);
  335. if (s > 0) {
  336. // Normalize divisor
  337. v = v << s;
  338. un64 = (u1 << s) | (u0 >> (64 - s));
  339. un10 = u0 << s; // Shift dividend left
  340. } else {
  341. // Avoid undefined behavior of (u0 >> 64).
  342. // The behavior is undefined if the right operand is
  343. // negative, or greater than or equal to the length
  344. // in bits of the promoted left operand.
  345. un64 = u1;
  346. un10 = u0;
  347. }
  348. // Break divisor up into two 32-bit digits
  349. vn1 = v >> 32;
  350. vn0 = v & 0xFFFFFFFF;
  351. // Break right half of dividend into two digits
  352. un1 = un10 >> 32;
  353. un0 = un10 & 0xFFFFFFFF;
  354. // Compute the first quotient digit, q1
  355. q1 = un64 / vn1;
  356. rhat = un64 - q1 * vn1;
  357. while (q1 >= b || q1 * vn0 > b * rhat + un1) {
  358. q1 = q1 - 1;
  359. rhat = rhat + vn1;
  360. if (rhat >= b)
  361. break;
  362. }
  363. // Multiply and subtract
  364. un21 = un64 * b + un1 - q1 * v;
  365. // Compute the second quotient digit
  366. q0 = un21 / vn1;
  367. rhat = un21 - q0 * vn1;
  368. while (q0 >= b || q0 * vn0 > b * rhat + un0) {
  369. q0 = q0 - 1;
  370. rhat = rhat + vn1;
  371. if (rhat >= b)
  372. break;
  373. }
  374. *r = (un21 * b + un0 - q0 * v) >> s;
  375. return q1 * b + q0;
  376. #endif
  377. }
  378. // Bitshift a u128 in place, left (signed_shift > 0) or right (signed_shift < 0)
  379. static inline void libdivide_u128_shift(uint64_t *u1, uint64_t *u0, int32_t signed_shift) {
  380. if (signed_shift > 0) {
  381. uint32_t shift = signed_shift;
  382. *u1 <<= shift;
  383. *u1 |= *u0 >> (64 - shift);
  384. *u0 <<= shift;
  385. }
  386. else if (signed_shift < 0) {
  387. uint32_t shift = -signed_shift;
  388. *u0 >>= shift;
  389. *u0 |= *u1 << (64 - shift);
  390. *u1 >>= shift;
  391. }
  392. }
  393. // Computes a 128 / 128 -> 64 bit division, with a 128 bit remainder.
  394. static uint64_t libdivide_128_div_128_to_64(uint64_t u_hi, uint64_t u_lo, uint64_t v_hi, uint64_t v_lo, uint64_t *r_hi, uint64_t *r_lo) {
  395. #if defined(HAS_INT128_T) && \
  396. defined(HAS_INT128_DIV)
  397. __uint128_t ufull = u_hi;
  398. __uint128_t vfull = v_hi;
  399. ufull = (ufull << 64) | u_lo;
  400. vfull = (vfull << 64) | v_lo;
  401. uint64_t res = (uint64_t)(ufull / vfull);
  402. __uint128_t remainder = ufull - (vfull * res);
  403. *r_lo = (uint64_t)remainder;
  404. *r_hi = (uint64_t)(remainder >> 64);
  405. return res;
  406. #else
  407. // Adapted from "Unsigned Doubleword Division" in Hacker's Delight
  408. // We want to compute u / v
  409. typedef struct { uint64_t hi; uint64_t lo; } u128_t;
  410. u128_t u = {u_hi, u_lo};
  411. u128_t v = {v_hi, v_lo};
  412. if (v.hi == 0) {
  413. // divisor v is a 64 bit value, so we just need one 128/64 division
  414. // Note that we are simpler than Hacker's Delight here, because we know
  415. // the quotient fits in 64 bits whereas Hacker's Delight demands a full
  416. // 128 bit quotient
  417. *r_hi = 0;
  418. return libdivide_128_div_64_to_64(u.hi, u.lo, v.lo, r_lo);
  419. }
  420. // Here v >= 2**64
  421. // We know that v.hi != 0, so count leading zeros is OK
  422. // We have 0 <= n <= 63
  423. uint32_t n = libdivide_count_leading_zeros64(v.hi);
  424. // Normalize the divisor so its MSB is 1
  425. u128_t v1t = v;
  426. libdivide_u128_shift(&v1t.hi, &v1t.lo, n);
  427. uint64_t v1 = v1t.hi; // i.e. v1 = v1t >> 64
  428. // To ensure no overflow
  429. u128_t u1 = u;
  430. libdivide_u128_shift(&u1.hi, &u1.lo, -1);
  431. // Get quotient from divide unsigned insn.
  432. uint64_t rem_ignored;
  433. uint64_t q1 = libdivide_128_div_64_to_64(u1.hi, u1.lo, v1, &rem_ignored);
  434. // Undo normalization and division of u by 2.
  435. u128_t q0 = {0, q1};
  436. libdivide_u128_shift(&q0.hi, &q0.lo, n);
  437. libdivide_u128_shift(&q0.hi, &q0.lo, -63);
  438. // Make q0 correct or too small by 1
  439. // Equivalent to `if (q0 != 0) q0 = q0 - 1;`
  440. if (q0.hi != 0 || q0.lo != 0) {
  441. q0.hi -= (q0.lo == 0); // borrow
  442. q0.lo -= 1;
  443. }
  444. // Now q0 is correct.
  445. // Compute q0 * v as q0v
  446. // = (q0.hi << 64 + q0.lo) * (v.hi << 64 + v.lo)
  447. // = (q0.hi * v.hi << 128) + (q0.hi * v.lo << 64) +
  448. // (q0.lo * v.hi << 64) + q0.lo * v.lo)
  449. // Each term is 128 bit
  450. // High half of full product (upper 128 bits!) are dropped
  451. u128_t q0v = {0, 0};
  452. q0v.hi = q0.hi*v.lo + q0.lo*v.hi + libdivide_mullhi_u64(q0.lo, v.lo);
  453. q0v.lo = q0.lo*v.lo;
  454. // Compute u - q0v as u_q0v
  455. // This is the remainder
  456. u128_t u_q0v = u;
  457. u_q0v.hi -= q0v.hi + (u.lo < q0v.lo); // second term is borrow
  458. u_q0v.lo -= q0v.lo;
  459. // Check if u_q0v >= v
  460. // This checks if our remainder is larger than the divisor
  461. if ((u_q0v.hi > v.hi) ||
  462. (u_q0v.hi == v.hi && u_q0v.lo >= v.lo)) {
  463. // Increment q0
  464. q0.lo += 1;
  465. q0.hi += (q0.lo == 0); // carry
  466. // Subtract v from remainder
  467. u_q0v.hi -= v.hi + (u_q0v.lo < v.lo);
  468. u_q0v.lo -= v.lo;
  469. }
  470. *r_hi = u_q0v.hi;
  471. *r_lo = u_q0v.lo;
  472. LIBDIVIDE_ASSERT(q0.hi == 0);
  473. return q0.lo;
  474. #endif
  475. }
  476. ////////// UINT32
  477. static inline struct libdivide_u32_t libdivide_internal_u32_gen(uint32_t d, int branchfree) {
  478. if (d == 0) {
  479. LIBDIVIDE_ERROR("divider must be != 0");
  480. }
  481. struct libdivide_u32_t result;
  482. uint32_t floor_log_2_d = 31 - libdivide_count_leading_zeros32(d);
  483. // Power of 2
  484. if ((d & (d - 1)) == 0) {
  485. // We need to subtract 1 from the shift value in case of an unsigned
  486. // branchfree divider because there is a hardcoded right shift by 1
  487. // in its division algorithm. Because of this we also need to add back
  488. // 1 in its recovery algorithm.
  489. result.magic = 0;
  490. result.more = (uint8_t)(floor_log_2_d - (branchfree != 0));
  491. } else {
  492. uint8_t more;
  493. uint32_t rem, proposed_m;
  494. proposed_m = libdivide_64_div_32_to_32(1U << floor_log_2_d, 0, d, &rem);
  495. LIBDIVIDE_ASSERT(rem > 0 && rem < d);
  496. const uint32_t e = d - rem;
  497. // This power works if e < 2**floor_log_2_d.
  498. if (!branchfree && (e < (1U << floor_log_2_d))) {
  499. // This power works
  500. more = floor_log_2_d;
  501. } else {
  502. // We have to use the general 33-bit algorithm. We need to compute
  503. // (2**power) / d. However, we already have (2**(power-1))/d and
  504. // its remainder. By doubling both, and then correcting the
  505. // remainder, we can compute the larger division.
  506. // don't care about overflow here - in fact, we expect it
  507. proposed_m += proposed_m;
  508. const uint32_t twice_rem = rem + rem;
  509. if (twice_rem >= d || twice_rem < rem) proposed_m += 1;
  510. more = floor_log_2_d | LIBDIVIDE_ADD_MARKER;
  511. }
  512. result.magic = 1 + proposed_m;
  513. result.more = more;
  514. // result.more's shift should in general be ceil_log_2_d. But if we
  515. // used the smaller power, we subtract one from the shift because we're
  516. // using the smaller power. If we're using the larger power, we
  517. // subtract one from the shift because it's taken care of by the add
  518. // indicator. So floor_log_2_d happens to be correct in both cases.
  519. }
  520. return result;
  521. }
  522. struct libdivide_u32_t libdivide_u32_gen(uint32_t d) {
  523. return libdivide_internal_u32_gen(d, 0);
  524. }
  525. struct libdivide_u32_branchfree_t libdivide_u32_branchfree_gen(uint32_t d) {
  526. if (d == 1) {
  527. LIBDIVIDE_ERROR("branchfree divider must be != 1");
  528. }
  529. struct libdivide_u32_t tmp = libdivide_internal_u32_gen(d, 1);
  530. struct libdivide_u32_branchfree_t ret = {tmp.magic, (uint8_t)(tmp.more & LIBDIVIDE_32_SHIFT_MASK)};
  531. return ret;
  532. }
  533. uint32_t libdivide_u32_do(uint32_t numer, const struct libdivide_u32_t *denom) {
  534. uint8_t more = denom->more;
  535. if (!denom->magic) {
  536. return numer >> more;
  537. }
  538. else {
  539. uint32_t q = libdivide_mullhi_u32(denom->magic, numer);
  540. if (more & LIBDIVIDE_ADD_MARKER) {
  541. uint32_t t = ((numer - q) >> 1) + q;
  542. return t >> (more & LIBDIVIDE_32_SHIFT_MASK);
  543. }
  544. else {
  545. // All upper bits are 0,
  546. // don't need to mask them off.
  547. return q >> more;
  548. }
  549. }
  550. }
  551. uint32_t libdivide_u32_branchfree_do(uint32_t numer, const struct libdivide_u32_branchfree_t *denom) {
  552. uint32_t q = libdivide_mullhi_u32(denom->magic, numer);
  553. uint32_t t = ((numer - q) >> 1) + q;
  554. return t >> denom->more;
  555. }
  556. uint32_t libdivide_u32_recover(const struct libdivide_u32_t *denom) {
  557. uint8_t more = denom->more;
  558. uint8_t shift = more & LIBDIVIDE_32_SHIFT_MASK;
  559. if (!denom->magic) {
  560. return 1U << shift;
  561. } else if (!(more & LIBDIVIDE_ADD_MARKER)) {
  562. // We compute q = n/d = n*m / 2^(32 + shift)
  563. // Therefore we have d = 2^(32 + shift) / m
  564. // We need to ceil it.
  565. // We know d is not a power of 2, so m is not a power of 2,
  566. // so we can just add 1 to the floor
  567. uint32_t hi_dividend = 1U << shift;
  568. uint32_t rem_ignored;
  569. return 1 + libdivide_64_div_32_to_32(hi_dividend, 0, denom->magic, &rem_ignored);
  570. } else {
  571. // Here we wish to compute d = 2^(32+shift+1)/(m+2^32).
  572. // Notice (m + 2^32) is a 33 bit number. Use 64 bit division for now
  573. // Also note that shift may be as high as 31, so shift + 1 will
  574. // overflow. So we have to compute it as 2^(32+shift)/(m+2^32), and
  575. // then double the quotient and remainder.
  576. uint64_t half_n = 1ULL << (32 + shift);
  577. uint64_t d = (1ULL << 32) | denom->magic;
  578. // Note that the quotient is guaranteed <= 32 bits, but the remainder
  579. // may need 33!
  580. uint32_t half_q = (uint32_t)(half_n / d);
  581. uint64_t rem = half_n % d;
  582. // We computed 2^(32+shift)/(m+2^32)
  583. // Need to double it, and then add 1 to the quotient if doubling th
  584. // remainder would increase the quotient.
  585. // Note that rem<<1 cannot overflow, since rem < d and d is 33 bits
  586. uint32_t full_q = half_q + half_q + ((rem<<1) >= d);
  587. // We rounded down in gen (hence +1)
  588. return full_q + 1;
  589. }
  590. }
  591. uint32_t libdivide_u32_branchfree_recover(const struct libdivide_u32_branchfree_t *denom) {
  592. uint8_t more = denom->more;
  593. uint8_t shift = more & LIBDIVIDE_32_SHIFT_MASK;
  594. if (!denom->magic) {
  595. return 1U << (shift + 1);
  596. } else {
  597. // Here we wish to compute d = 2^(32+shift+1)/(m+2^32).
  598. // Notice (m + 2^32) is a 33 bit number. Use 64 bit division for now
  599. // Also note that shift may be as high as 31, so shift + 1 will
  600. // overflow. So we have to compute it as 2^(32+shift)/(m+2^32), and
  601. // then double the quotient and remainder.
  602. uint64_t half_n = 1ULL << (32 + shift);
  603. uint64_t d = (1ULL << 32) | denom->magic;
  604. // Note that the quotient is guaranteed <= 32 bits, but the remainder
  605. // may need 33!
  606. uint32_t half_q = (uint32_t)(half_n / d);
  607. uint64_t rem = half_n % d;
  608. // We computed 2^(32+shift)/(m+2^32)
  609. // Need to double it, and then add 1 to the quotient if doubling th
  610. // remainder would increase the quotient.
  611. // Note that rem<<1 cannot overflow, since rem < d and d is 33 bits
  612. uint32_t full_q = half_q + half_q + ((rem<<1) >= d);
  613. // We rounded down in gen (hence +1)
  614. return full_q + 1;
  615. }
  616. }
  617. /////////// UINT64
  618. static inline struct libdivide_u64_t libdivide_internal_u64_gen(uint64_t d, int branchfree) {
  619. if (d == 0) {
  620. LIBDIVIDE_ERROR("divider must be != 0");
  621. }
  622. struct libdivide_u64_t result;
  623. uint32_t floor_log_2_d = 63 - libdivide_count_leading_zeros64(d);
  624. // Power of 2
  625. if ((d & (d - 1)) == 0) {
  626. // We need to subtract 1 from the shift value in case of an unsigned
  627. // branchfree divider because there is a hardcoded right shift by 1
  628. // in its division algorithm. Because of this we also need to add back
  629. // 1 in its recovery algorithm.
  630. result.magic = 0;
  631. result.more = (uint8_t)(floor_log_2_d - (branchfree != 0));
  632. } else {
  633. uint64_t proposed_m, rem;
  634. uint8_t more;
  635. // (1 << (64 + floor_log_2_d)) / d
  636. proposed_m = libdivide_128_div_64_to_64(1ULL << floor_log_2_d, 0, d, &rem);
  637. LIBDIVIDE_ASSERT(rem > 0 && rem < d);
  638. const uint64_t e = d - rem;
  639. // This power works if e < 2**floor_log_2_d.
  640. if (!branchfree && e < (1ULL << floor_log_2_d)) {
  641. // This power works
  642. more = floor_log_2_d;
  643. } else {
  644. // We have to use the general 65-bit algorithm. We need to compute
  645. // (2**power) / d. However, we already have (2**(power-1))/d and
  646. // its remainder. By doubling both, and then correcting the
  647. // remainder, we can compute the larger division.
  648. // don't care about overflow here - in fact, we expect it
  649. proposed_m += proposed_m;
  650. const uint64_t twice_rem = rem + rem;
  651. if (twice_rem >= d || twice_rem < rem) proposed_m += 1;
  652. more = floor_log_2_d | LIBDIVIDE_ADD_MARKER;
  653. }
  654. result.magic = 1 + proposed_m;
  655. result.more = more;
  656. // result.more's shift should in general be ceil_log_2_d. But if we
  657. // used the smaller power, we subtract one from the shift because we're
  658. // using the smaller power. If we're using the larger power, we
  659. // subtract one from the shift because it's taken care of by the add
  660. // indicator. So floor_log_2_d happens to be correct in both cases,
  661. // which is why we do it outside of the if statement.
  662. }
  663. return result;
  664. }
  665. struct libdivide_u64_t libdivide_u64_gen(uint64_t d) {
  666. return libdivide_internal_u64_gen(d, 0);
  667. }
  668. struct libdivide_u64_branchfree_t libdivide_u64_branchfree_gen(uint64_t d) {
  669. if (d == 1) {
  670. LIBDIVIDE_ERROR("branchfree divider must be != 1");
  671. }
  672. struct libdivide_u64_t tmp = libdivide_internal_u64_gen(d, 1);
  673. struct libdivide_u64_branchfree_t ret = {tmp.magic, (uint8_t)(tmp.more & LIBDIVIDE_64_SHIFT_MASK)};
  674. return ret;
  675. }
  676. uint64_t libdivide_u64_do(uint64_t numer, const struct libdivide_u64_t *denom) {
  677. uint8_t more = denom->more;
  678. if (!denom->magic) {
  679. return numer >> more;
  680. }
  681. else {
  682. uint64_t q = libdivide_mullhi_u64(denom->magic, numer);
  683. if (more & LIBDIVIDE_ADD_MARKER) {
  684. uint64_t t = ((numer - q) >> 1) + q;
  685. return t >> (more & LIBDIVIDE_64_SHIFT_MASK);
  686. }
  687. else {
  688. // All upper bits are 0,
  689. // don't need to mask them off.
  690. return q >> more;
  691. }
  692. }
  693. }
  694. uint64_t libdivide_u64_branchfree_do(uint64_t numer, const struct libdivide_u64_branchfree_t *denom) {
  695. uint64_t q = libdivide_mullhi_u64(denom->magic, numer);
  696. uint64_t t = ((numer - q) >> 1) + q;
  697. return t >> denom->more;
  698. }
  699. uint64_t libdivide_u64_recover(const struct libdivide_u64_t *denom) {
  700. uint8_t more = denom->more;
  701. uint8_t shift = more & LIBDIVIDE_64_SHIFT_MASK;
  702. if (!denom->magic) {
  703. return 1ULL << shift;
  704. } else if (!(more & LIBDIVIDE_ADD_MARKER)) {
  705. // We compute q = n/d = n*m / 2^(64 + shift)
  706. // Therefore we have d = 2^(64 + shift) / m
  707. // We need to ceil it.
  708. // We know d is not a power of 2, so m is not a power of 2,
  709. // so we can just add 1 to the floor
  710. uint64_t hi_dividend = 1ULL << shift;
  711. uint64_t rem_ignored;
  712. return 1 + libdivide_128_div_64_to_64(hi_dividend, 0, denom->magic, &rem_ignored);
  713. } else {
  714. // Here we wish to compute d = 2^(64+shift+1)/(m+2^64).
  715. // Notice (m + 2^64) is a 65 bit number. This gets hairy. See
  716. // libdivide_u32_recover for more on what we do here.
  717. // TODO: do something better than 128 bit math
  718. // Full n is a (potentially) 129 bit value
  719. // half_n is a 128 bit value
  720. // Compute the hi half of half_n. Low half is 0.
  721. uint64_t half_n_hi = 1ULL << shift, half_n_lo = 0;
  722. // d is a 65 bit value. The high bit is always set to 1.
  723. const uint64_t d_hi = 1, d_lo = denom->magic;
  724. // Note that the quotient is guaranteed <= 64 bits,
  725. // but the remainder may need 65!
  726. uint64_t r_hi, r_lo;
  727. uint64_t half_q = libdivide_128_div_128_to_64(half_n_hi, half_n_lo, d_hi, d_lo, &r_hi, &r_lo);
  728. // We computed 2^(64+shift)/(m+2^64)
  729. // Double the remainder ('dr') and check if that is larger than d
  730. // Note that d is a 65 bit value, so r1 is small and so r1 + r1
  731. // cannot overflow
  732. uint64_t dr_lo = r_lo + r_lo;
  733. uint64_t dr_hi = r_hi + r_hi + (dr_lo < r_lo); // last term is carry
  734. int dr_exceeds_d = (dr_hi > d_hi) || (dr_hi == d_hi && dr_lo >= d_lo);
  735. uint64_t full_q = half_q + half_q + (dr_exceeds_d ? 1 : 0);
  736. return full_q + 1;
  737. }
  738. }
  739. uint64_t libdivide_u64_branchfree_recover(const struct libdivide_u64_branchfree_t *denom) {
  740. uint8_t more = denom->more;
  741. uint8_t shift = more & LIBDIVIDE_64_SHIFT_MASK;
  742. if (!denom->magic) {
  743. return 1ULL << (shift + 1);
  744. } else {
  745. // Here we wish to compute d = 2^(64+shift+1)/(m+2^64).
  746. // Notice (m + 2^64) is a 65 bit number. This gets hairy. See
  747. // libdivide_u32_recover for more on what we do here.
  748. // TODO: do something better than 128 bit math
  749. // Full n is a (potentially) 129 bit value
  750. // half_n is a 128 bit value
  751. // Compute the hi half of half_n. Low half is 0.
  752. uint64_t half_n_hi = 1ULL << shift, half_n_lo = 0;
  753. // d is a 65 bit value. The high bit is always set to 1.
  754. const uint64_t d_hi = 1, d_lo = denom->magic;
  755. // Note that the quotient is guaranteed <= 64 bits,
  756. // but the remainder may need 65!
  757. uint64_t r_hi, r_lo;
  758. uint64_t half_q = libdivide_128_div_128_to_64(half_n_hi, half_n_lo, d_hi, d_lo, &r_hi, &r_lo);
  759. // We computed 2^(64+shift)/(m+2^64)
  760. // Double the remainder ('dr') and check if that is larger than d
  761. // Note that d is a 65 bit value, so r1 is small and so r1 + r1
  762. // cannot overflow
  763. uint64_t dr_lo = r_lo + r_lo;
  764. uint64_t dr_hi = r_hi + r_hi + (dr_lo < r_lo); // last term is carry
  765. int dr_exceeds_d = (dr_hi > d_hi) || (dr_hi == d_hi && dr_lo >= d_lo);
  766. uint64_t full_q = half_q + half_q + (dr_exceeds_d ? 1 : 0);
  767. return full_q + 1;
  768. }
  769. }
  770. /////////// SINT32
  771. static inline struct libdivide_s32_t libdivide_internal_s32_gen(int32_t d, int branchfree) {
  772. if (d == 0) {
  773. LIBDIVIDE_ERROR("divider must be != 0");
  774. }
  775. struct libdivide_s32_t result;
  776. // If d is a power of 2, or negative a power of 2, we have to use a shift.
  777. // This is especially important because the magic algorithm fails for -1.
  778. // To check if d is a power of 2 or its inverse, it suffices to check
  779. // whether its absolute value has exactly one bit set. This works even for
  780. // INT_MIN, because abs(INT_MIN) == INT_MIN, and INT_MIN has one bit set
  781. // and is a power of 2.
  782. uint32_t ud = (uint32_t)d;
  783. uint32_t absD = (d < 0) ? -ud : ud;
  784. uint32_t floor_log_2_d = 31 - libdivide_count_leading_zeros32(absD);
  785. // check if exactly one bit is set,
  786. // don't care if absD is 0 since that's divide by zero
  787. if ((absD & (absD - 1)) == 0) {
  788. // Branchfree and normal paths are exactly the same
  789. result.magic = 0;
  790. result.more = floor_log_2_d | (d < 0 ? LIBDIVIDE_NEGATIVE_DIVISOR : 0);
  791. } else {
  792. LIBDIVIDE_ASSERT(floor_log_2_d >= 1);
  793. uint8_t more;
  794. // the dividend here is 2**(floor_log_2_d + 31), so the low 32 bit word
  795. // is 0 and the high word is floor_log_2_d - 1
  796. uint32_t rem, proposed_m;
  797. proposed_m = libdivide_64_div_32_to_32(1U << (floor_log_2_d - 1), 0, absD, &rem);
  798. const uint32_t e = absD - rem;
  799. // We are going to start with a power of floor_log_2_d - 1.
  800. // This works if works if e < 2**floor_log_2_d.
  801. if (!branchfree && e < (1U << floor_log_2_d)) {
  802. // This power works
  803. more = floor_log_2_d - 1;
  804. } else {
  805. // We need to go one higher. This should not make proposed_m
  806. // overflow, but it will make it negative when interpreted as an
  807. // int32_t.
  808. proposed_m += proposed_m;
  809. const uint32_t twice_rem = rem + rem;
  810. if (twice_rem >= absD || twice_rem < rem) proposed_m += 1;
  811. more = floor_log_2_d | LIBDIVIDE_ADD_MARKER;
  812. }
  813. proposed_m += 1;
  814. int32_t magic = (int32_t)proposed_m;
  815. // Mark if we are negative. Note we only negate the magic number in the
  816. // branchfull case.
  817. if (d < 0) {
  818. more |= LIBDIVIDE_NEGATIVE_DIVISOR;
  819. if (!branchfree) {
  820. magic = -magic;
  821. }
  822. }
  823. result.more = more;
  824. result.magic = magic;
  825. }
  826. return result;
  827. }
  828. struct libdivide_s32_t libdivide_s32_gen(int32_t d) {
  829. return libdivide_internal_s32_gen(d, 0);
  830. }
  831. struct libdivide_s32_branchfree_t libdivide_s32_branchfree_gen(int32_t d) {
  832. struct libdivide_s32_t tmp = libdivide_internal_s32_gen(d, 1);
  833. struct libdivide_s32_branchfree_t result = {tmp.magic, tmp.more};
  834. return result;
  835. }
  836. int32_t libdivide_s32_do(int32_t numer, const struct libdivide_s32_t *denom) {
  837. uint8_t more = denom->more;
  838. uint8_t shift = more & LIBDIVIDE_32_SHIFT_MASK;
  839. if (!denom->magic) {
  840. uint32_t sign = (int8_t)more >> 7;
  841. uint32_t mask = (1U << shift) - 1;
  842. uint32_t uq = numer + ((numer >> 31) & mask);
  843. int32_t q = (int32_t)uq;
  844. q >>= shift;
  845. q = (q ^ sign) - sign;
  846. return q;
  847. } else {
  848. uint32_t uq = (uint32_t)libdivide_mullhi_s32(denom->magic, numer);
  849. if (more & LIBDIVIDE_ADD_MARKER) {
  850. // must be arithmetic shift and then sign extend
  851. int32_t sign = (int8_t)more >> 7;
  852. // q += (more < 0 ? -numer : numer)
  853. // cast required to avoid UB
  854. uq += ((uint32_t)numer ^ sign) - sign;
  855. }
  856. int32_t q = (int32_t)uq;
  857. q >>= shift;
  858. q += (q < 0);
  859. return q;
  860. }
  861. }
  862. int32_t libdivide_s32_branchfree_do(int32_t numer, const struct libdivide_s32_branchfree_t *denom) {
  863. uint8_t more = denom->more;
  864. uint8_t shift = more & LIBDIVIDE_32_SHIFT_MASK;
  865. // must be arithmetic shift and then sign extend
  866. int32_t sign = (int8_t)more >> 7;
  867. int32_t magic = denom->magic;
  868. int32_t q = libdivide_mullhi_s32(magic, numer);
  869. q += numer;
  870. // If q is non-negative, we have nothing to do
  871. // If q is negative, we want to add either (2**shift)-1 if d is a power of
  872. // 2, or (2**shift) if it is not a power of 2
  873. uint32_t is_power_of_2 = (magic == 0);
  874. uint32_t q_sign = (uint32_t)(q >> 31);
  875. q += q_sign & ((1U << shift) - is_power_of_2);
  876. // Now arithmetic right shift
  877. q >>= shift;
  878. // Negate if needed
  879. q = (q ^ sign) - sign;
  880. return q;
  881. }
  882. int32_t libdivide_s32_recover(const struct libdivide_s32_t *denom) {
  883. uint8_t more = denom->more;
  884. uint8_t shift = more & LIBDIVIDE_32_SHIFT_MASK;
  885. if (!denom->magic) {
  886. uint32_t absD = 1U << shift;
  887. if (more & LIBDIVIDE_NEGATIVE_DIVISOR) {
  888. absD = -absD;
  889. }
  890. return (int32_t)absD;
  891. } else {
  892. // Unsigned math is much easier
  893. // We negate the magic number only in the branchfull case, and we don't
  894. // know which case we're in. However we have enough information to
  895. // determine the correct sign of the magic number. The divisor was
  896. // negative if LIBDIVIDE_NEGATIVE_DIVISOR is set. If ADD_MARKER is set,
  897. // the magic number's sign is opposite that of the divisor.
  898. // We want to compute the positive magic number.
  899. int negative_divisor = (more & LIBDIVIDE_NEGATIVE_DIVISOR);
  900. int magic_was_negated = (more & LIBDIVIDE_ADD_MARKER)
  901. ? denom->magic > 0 : denom->magic < 0;
  902. // Handle the power of 2 case (including branchfree)
  903. if (denom->magic == 0) {
  904. int32_t result = 1U << shift;
  905. return negative_divisor ? -result : result;
  906. }
  907. uint32_t d = (uint32_t)(magic_was_negated ? -denom->magic : denom->magic);
  908. uint64_t n = 1ULL << (32 + shift); // this shift cannot exceed 30
  909. uint32_t q = (uint32_t)(n / d);
  910. int32_t result = (int32_t)q;
  911. result += 1;
  912. return negative_divisor ? -result : result;
  913. }
  914. }
  915. int32_t libdivide_s32_branchfree_recover(const struct libdivide_s32_branchfree_t *denom) {
  916. return libdivide_s32_recover((const struct libdivide_s32_t *)denom);
  917. }
  918. ///////////// SINT64
  919. static inline struct libdivide_s64_t libdivide_internal_s64_gen(int64_t d, int branchfree) {
  920. if (d == 0) {
  921. LIBDIVIDE_ERROR("divider must be != 0");
  922. }
  923. struct libdivide_s64_t result;
  924. // If d is a power of 2, or negative a power of 2, we have to use a shift.
  925. // This is especially important because the magic algorithm fails for -1.
  926. // To check if d is a power of 2 or its inverse, it suffices to check
  927. // whether its absolute value has exactly one bit set. This works even for
  928. // INT_MIN, because abs(INT_MIN) == INT_MIN, and INT_MIN has one bit set
  929. // and is a power of 2.
  930. uint64_t ud = (uint64_t)d;
  931. uint64_t absD = (d < 0) ? -ud : ud;
  932. uint32_t floor_log_2_d = 63 - libdivide_count_leading_zeros64(absD);
  933. // check if exactly one bit is set,
  934. // don't care if absD is 0 since that's divide by zero
  935. if ((absD & (absD - 1)) == 0) {
  936. // Branchfree and non-branchfree cases are the same
  937. result.magic = 0;
  938. result.more = floor_log_2_d | (d < 0 ? LIBDIVIDE_NEGATIVE_DIVISOR : 0);
  939. } else {
  940. // the dividend here is 2**(floor_log_2_d + 63), so the low 64 bit word
  941. // is 0 and the high word is floor_log_2_d - 1
  942. uint8_t more;
  943. uint64_t rem, proposed_m;
  944. proposed_m = libdivide_128_div_64_to_64(1ULL << (floor_log_2_d - 1), 0, absD, &rem);
  945. const uint64_t e = absD - rem;
  946. // We are going to start with a power of floor_log_2_d - 1.
  947. // This works if works if e < 2**floor_log_2_d.
  948. if (!branchfree && e < (1ULL << floor_log_2_d)) {
  949. // This power works
  950. more = floor_log_2_d - 1;
  951. } else {
  952. // We need to go one higher. This should not make proposed_m
  953. // overflow, but it will make it negative when interpreted as an
  954. // int32_t.
  955. proposed_m += proposed_m;
  956. const uint64_t twice_rem = rem + rem;
  957. if (twice_rem >= absD || twice_rem < rem) proposed_m += 1;
  958. // note that we only set the LIBDIVIDE_NEGATIVE_DIVISOR bit if we
  959. // also set ADD_MARKER this is an annoying optimization that
  960. // enables algorithm #4 to avoid the mask. However we always set it
  961. // in the branchfree case
  962. more = floor_log_2_d | LIBDIVIDE_ADD_MARKER;
  963. }
  964. proposed_m += 1;
  965. int64_t magic = (int64_t)proposed_m;
  966. // Mark if we are negative
  967. if (d < 0) {
  968. more |= LIBDIVIDE_NEGATIVE_DIVISOR;
  969. if (!branchfree) {
  970. magic = -magic;
  971. }
  972. }
  973. result.more = more;
  974. result.magic = magic;
  975. }
  976. return result;
  977. }
  978. struct libdivide_s64_t libdivide_s64_gen(int64_t d) {
  979. return libdivide_internal_s64_gen(d, 0);
  980. }
  981. struct libdivide_s64_branchfree_t libdivide_s64_branchfree_gen(int64_t d) {
  982. struct libdivide_s64_t tmp = libdivide_internal_s64_gen(d, 1);
  983. struct libdivide_s64_branchfree_t ret = {tmp.magic, tmp.more};
  984. return ret;
  985. }
  986. int64_t libdivide_s64_do(int64_t numer, const struct libdivide_s64_t *denom) {
  987. uint8_t more = denom->more;
  988. uint8_t shift = more & LIBDIVIDE_64_SHIFT_MASK;
  989. if (!denom->magic) { // shift path
  990. uint64_t mask = (1ULL << shift) - 1;
  991. uint64_t uq = numer + ((numer >> 63) & mask);
  992. int64_t q = (int64_t)uq;
  993. q >>= shift;
  994. // must be arithmetic shift and then sign-extend
  995. int64_t sign = (int8_t)more >> 7;
  996. q = (q ^ sign) - sign;
  997. return q;
  998. } else {
  999. uint64_t uq = (uint64_t)libdivide_mullhi_s64(denom->magic, numer);
  1000. if (more & LIBDIVIDE_ADD_MARKER) {
  1001. // must be arithmetic shift and then sign extend
  1002. int64_t sign = (int8_t)more >> 7;
  1003. // q += (more < 0 ? -numer : numer)
  1004. // cast required to avoid UB
  1005. uq += ((uint64_t)numer ^ sign) - sign;
  1006. }
  1007. int64_t q = (int64_t)uq;
  1008. q >>= shift;
  1009. q += (q < 0);
  1010. return q;
  1011. }
  1012. }
  1013. int64_t libdivide_s64_branchfree_do(int64_t numer, const struct libdivide_s64_branchfree_t *denom) {
  1014. uint8_t more = denom->more;
  1015. uint8_t shift = more & LIBDIVIDE_64_SHIFT_MASK;
  1016. // must be arithmetic shift and then sign extend
  1017. int64_t sign = (int8_t)more >> 7;
  1018. int64_t magic = denom->magic;
  1019. int64_t q = libdivide_mullhi_s64(magic, numer);
  1020. q += numer;
  1021. // If q is non-negative, we have nothing to do.
  1022. // If q is negative, we want to add either (2**shift)-1 if d is a power of
  1023. // 2, or (2**shift) if it is not a power of 2.
  1024. uint64_t is_power_of_2 = (magic == 0);
  1025. uint64_t q_sign = (uint64_t)(q >> 63);
  1026. q += q_sign & ((1ULL << shift) - is_power_of_2);
  1027. // Arithmetic right shift
  1028. q >>= shift;
  1029. // Negate if needed
  1030. q = (q ^ sign) - sign;
  1031. return q;
  1032. }
  1033. int64_t libdivide_s64_recover(const struct libdivide_s64_t *denom) {
  1034. uint8_t more = denom->more;
  1035. uint8_t shift = more & LIBDIVIDE_64_SHIFT_MASK;
  1036. if (denom->magic == 0) { // shift path
  1037. uint64_t absD = 1ULL << shift;
  1038. if (more & LIBDIVIDE_NEGATIVE_DIVISOR) {
  1039. absD = -absD;
  1040. }
  1041. return (int64_t)absD;
  1042. } else {
  1043. // Unsigned math is much easier
  1044. int negative_divisor = (more & LIBDIVIDE_NEGATIVE_DIVISOR);
  1045. int magic_was_negated = (more & LIBDIVIDE_ADD_MARKER)
  1046. ? denom->magic > 0 : denom->magic < 0;
  1047. uint64_t d = (uint64_t)(magic_was_negated ? -denom->magic : denom->magic);
  1048. uint64_t n_hi = 1ULL << shift, n_lo = 0;
  1049. uint64_t rem_ignored;
  1050. uint64_t q = libdivide_128_div_64_to_64(n_hi, n_lo, d, &rem_ignored);
  1051. int64_t result = (int64_t)(q + 1);
  1052. if (negative_divisor) {
  1053. result = -result;
  1054. }
  1055. return result;
  1056. }
  1057. }
  1058. int64_t libdivide_s64_branchfree_recover(const struct libdivide_s64_branchfree_t *denom) {
  1059. return libdivide_s64_recover((const struct libdivide_s64_t *)denom);
  1060. }
  1061. #if defined(LIBDIVIDE_AVX512)
  1062. static inline __m512i libdivide_u32_do_vector(__m512i numers, const struct libdivide_u32_t *denom);
  1063. static inline __m512i libdivide_s32_do_vector(__m512i numers, const struct libdivide_s32_t *denom);
  1064. static inline __m512i libdivide_u64_do_vector(__m512i numers, const struct libdivide_u64_t *denom);
  1065. static inline __m512i libdivide_s64_do_vector(__m512i numers, const struct libdivide_s64_t *denom);
  1066. static inline __m512i libdivide_u32_branchfree_do_vector(__m512i numers, const struct libdivide_u32_branchfree_t *denom);
  1067. static inline __m512i libdivide_s32_branchfree_do_vector(__m512i numers, const struct libdivide_s32_branchfree_t *denom);
  1068. static inline __m512i libdivide_u64_branchfree_do_vector(__m512i numers, const struct libdivide_u64_branchfree_t *denom);
  1069. static inline __m512i libdivide_s64_branchfree_do_vector(__m512i numers, const struct libdivide_s64_branchfree_t *denom);
  1070. //////// Internal Utility Functions
  1071. static inline __m512i libdivide_s64_signbits(__m512i v) {;
  1072. return _mm512_srai_epi64(v, 63);
  1073. }
  1074. static inline __m512i libdivide_s64_shift_right_vector(__m512i v, int amt) {
  1075. return _mm512_srai_epi64(v, amt);
  1076. }
  1077. // Here, b is assumed to contain one 32-bit value repeated.
  1078. static inline __m512i libdivide_mullhi_u32_vector(__m512i a, __m512i b) {
  1079. __m512i hi_product_0Z2Z = _mm512_srli_epi64(_mm512_mul_epu32(a, b), 32);
  1080. __m512i a1X3X = _mm512_srli_epi64(a, 32);
  1081. __m512i mask = _mm512_set_epi32(-1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0);
  1082. __m512i hi_product_Z1Z3 = _mm512_and_si512(_mm512_mul_epu32(a1X3X, b), mask);
  1083. return _mm512_or_si512(hi_product_0Z2Z, hi_product_Z1Z3);
  1084. }
  1085. // b is one 32-bit value repeated.
  1086. static inline __m512i libdivide_mullhi_s32_vector(__m512i a, __m512i b) {
  1087. __m512i hi_product_0Z2Z = _mm512_srli_epi64(_mm512_mul_epi32(a, b), 32);
  1088. __m512i a1X3X = _mm512_srli_epi64(a, 32);
  1089. __m512i mask = _mm512_set_epi32(-1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0);
  1090. __m512i hi_product_Z1Z3 = _mm512_and_si512(_mm512_mul_epi32(a1X3X, b), mask);
  1091. return _mm512_or_si512(hi_product_0Z2Z, hi_product_Z1Z3);
  1092. }
  1093. // Here, y is assumed to contain one 64-bit value repeated.
  1094. // https://stackoverflow.com/a/28827013
  1095. static inline __m512i libdivide_mullhi_u64_vector(__m512i x, __m512i y) {
  1096. __m512i lomask = _mm512_set1_epi64(0xffffffff);
  1097. __m512i xh = _mm512_shuffle_epi32(x, (_MM_PERM_ENUM) 0xB1);
  1098. __m512i yh = _mm512_shuffle_epi32(y, (_MM_PERM_ENUM) 0xB1);
  1099. __m512i w0 = _mm512_mul_epu32(x, y);
  1100. __m512i w1 = _mm512_mul_epu32(x, yh);
  1101. __m512i w2 = _mm512_mul_epu32(xh, y);
  1102. __m512i w3 = _mm512_mul_epu32(xh, yh);
  1103. __m512i w0h = _mm512_srli_epi64(w0, 32);
  1104. __m512i s1 = _mm512_add_epi64(w1, w0h);
  1105. __m512i s1l = _mm512_and_si512(s1, lomask);
  1106. __m512i s1h = _mm512_srli_epi64(s1, 32);
  1107. __m512i s2 = _mm512_add_epi64(w2, s1l);
  1108. __m512i s2h = _mm512_srli_epi64(s2, 32);
  1109. __m512i hi = _mm512_add_epi64(w3, s1h);
  1110. hi = _mm512_add_epi64(hi, s2h);
  1111. return hi;
  1112. }
  1113. // y is one 64-bit value repeated.
  1114. static inline __m512i libdivide_mullhi_s64_vector(__m512i x, __m512i y) {
  1115. __m512i p = libdivide_mullhi_u64_vector(x, y);
  1116. __m512i t1 = _mm512_and_si512(libdivide_s64_signbits(x), y);
  1117. __m512i t2 = _mm512_and_si512(libdivide_s64_signbits(y), x);
  1118. p = _mm512_sub_epi64(p, t1);
  1119. p = _mm512_sub_epi64(p, t2);
  1120. return p;
  1121. }
  1122. ////////// UINT32
  1123. __m512i libdivide_u32_do_vector(__m512i numers, const struct libdivide_u32_t *denom) {
  1124. uint8_t more = denom->more;
  1125. if (!denom->magic) {
  1126. return _mm512_srli_epi32(numers, more);
  1127. }
  1128. else {
  1129. __m512i q = libdivide_mullhi_u32_vector(numers, _mm512_set1_epi32(denom->magic));
  1130. if (more & LIBDIVIDE_ADD_MARKER) {
  1131. // uint32_t t = ((numer - q) >> 1) + q;
  1132. // return t >> denom->shift;
  1133. uint32_t shift = more & LIBDIVIDE_32_SHIFT_MASK;
  1134. __m512i t = _mm512_add_epi32(_mm512_srli_epi32(_mm512_sub_epi32(numers, q), 1), q);
  1135. return _mm512_srli_epi32(t, shift);
  1136. }
  1137. else {
  1138. return _mm512_srli_epi32(q, more);
  1139. }
  1140. }
  1141. }
  1142. __m512i libdivide_u32_branchfree_do_vector(__m512i numers, const struct libdivide_u32_branchfree_t *denom) {
  1143. __m512i q = libdivide_mullhi_u32_vector(numers, _mm512_set1_epi32(denom->magic));
  1144. __m512i t = _mm512_add_epi32(_mm512_srli_epi32(_mm512_sub_epi32(numers, q), 1), q);
  1145. return _mm512_srli_epi32(t, denom->more);
  1146. }
  1147. ////////// UINT64
  1148. __m512i libdivide_u64_do_vector(__m512i numers, const struct libdivide_u64_t *denom) {
  1149. uint8_t more = denom->more;
  1150. if (!denom->magic) {
  1151. return _mm512_srli_epi64(numers, more);
  1152. }
  1153. else {
  1154. __m512i q = libdivide_mullhi_u64_vector(numers, _mm512_set1_epi64(denom->magic));
  1155. if (more & LIBDIVIDE_ADD_MARKER) {
  1156. // uint32_t t = ((numer - q) >> 1) + q;
  1157. // return t >> denom->shift;
  1158. uint32_t shift = more & LIBDIVIDE_64_SHIFT_MASK;
  1159. __m512i t = _mm512_add_epi64(_mm512_srli_epi64(_mm512_sub_epi64(numers, q), 1), q);
  1160. return _mm512_srli_epi64(t, shift);
  1161. }
  1162. else {
  1163. return _mm512_srli_epi64(q, more);
  1164. }
  1165. }
  1166. }
  1167. __m512i libdivide_u64_branchfree_do_vector(__m512i numers, const struct libdivide_u64_branchfree_t *denom) {
  1168. __m512i q = libdivide_mullhi_u64_vector(numers, _mm512_set1_epi64(denom->magic));
  1169. __m512i t = _mm512_add_epi64(_mm512_srli_epi64(_mm512_sub_epi64(numers, q), 1), q);
  1170. return _mm512_srli_epi64(t, denom->more);
  1171. }
  1172. ////////// SINT32
  1173. __m512i libdivide_s32_do_vector(__m512i numers, const struct libdivide_s32_t *denom) {
  1174. uint8_t more = denom->more;
  1175. if (!denom->magic) {
  1176. uint32_t shift = more & LIBDIVIDE_32_SHIFT_MASK;
  1177. uint32_t mask = (1U << shift) - 1;
  1178. __m512i roundToZeroTweak = _mm512_set1_epi32(mask);
  1179. // q = numer + ((numer >> 31) & roundToZeroTweak);
  1180. __m512i q = _mm512_add_epi32(numers, _mm512_and_si512(_mm512_srai_epi32(numers, 31), roundToZeroTweak));
  1181. q = _mm512_srai_epi32(q, shift);
  1182. __m512i sign = _mm512_set1_epi32((int8_t)more >> 7);
  1183. // q = (q ^ sign) - sign;
  1184. q = _mm512_sub_epi32(_mm512_xor_si512(q, sign), sign);
  1185. return q;
  1186. }
  1187. else {
  1188. __m512i q = libdivide_mullhi_s32_vector(numers, _mm512_set1_epi32(denom->magic));
  1189. if (more & LIBDIVIDE_ADD_MARKER) {
  1190. // must be arithmetic shift
  1191. __m512i sign = _mm512_set1_epi32((int8_t)more >> 7);
  1192. // q += ((numer ^ sign) - sign);
  1193. q = _mm512_add_epi32(q, _mm512_sub_epi32(_mm512_xor_si512(numers, sign), sign));
  1194. }
  1195. // q >>= shift
  1196. q = _mm512_srai_epi32(q, more & LIBDIVIDE_32_SHIFT_MASK);
  1197. q = _mm512_add_epi32(q, _mm512_srli_epi32(q, 31)); // q += (q < 0)
  1198. return q;
  1199. }
  1200. }
  1201. __m512i libdivide_s32_branchfree_do_vector(__m512i numers, const struct libdivide_s32_branchfree_t *denom) {
  1202. int32_t magic = denom->magic;
  1203. uint8_t more = denom->more;
  1204. uint8_t shift = more & LIBDIVIDE_32_SHIFT_MASK;
  1205. // must be arithmetic shift
  1206. __m512i sign = _mm512_set1_epi32((int8_t)more >> 7);
  1207. __m512i q = libdivide_mullhi_s32_vector(numers, _mm512_set1_epi32(magic));
  1208. q = _mm512_add_epi32(q, numers); // q += numers
  1209. // If q is non-negative, we have nothing to do
  1210. // If q is negative, we want to add either (2**shift)-1 if d is
  1211. // a power of 2, or (2**shift) if it is not a power of 2
  1212. uint32_t is_power_of_2 = (magic == 0);
  1213. __m512i q_sign = _mm512_srai_epi32(q, 31); // q_sign = q >> 31
  1214. __m512i mask = _mm512_set1_epi32((1U << shift) - is_power_of_2);
  1215. q = _mm512_add_epi32(q, _mm512_and_si512(q_sign, mask)); // q = q + (q_sign & mask)
  1216. q = _mm512_srai_epi32(q, shift); // q >>= shift
  1217. q = _mm512_sub_epi32(_mm512_xor_si512(q, sign), sign); // q = (q ^ sign) - sign
  1218. return q;
  1219. }
  1220. ////////// SINT64
  1221. __m512i libdivide_s64_do_vector(__m512i numers, const struct libdivide_s64_t *denom) {
  1222. uint8_t more = denom->more;
  1223. int64_t magic = denom->magic;
  1224. if (magic == 0) { // shift path
  1225. uint32_t shift = more & LIBDIVIDE_64_SHIFT_MASK;
  1226. uint64_t mask = (1ULL << shift) - 1;
  1227. __m512i roundToZeroTweak = _mm512_set1_epi64(mask);
  1228. // q = numer + ((numer >> 63) & roundToZeroTweak);
  1229. __m512i q = _mm512_add_epi64(numers, _mm512_and_si512(libdivide_s64_signbits(numers), roundToZeroTweak));
  1230. q = libdivide_s64_shift_right_vector(q, shift);
  1231. __m512i sign = _mm512_set1_epi32((int8_t)more >> 7);
  1232. // q = (q ^ sign) - sign;
  1233. q = _mm512_sub_epi64(_mm512_xor_si512(q, sign), sign);
  1234. return q;
  1235. }
  1236. else {
  1237. __m512i q = libdivide_mullhi_s64_vector(numers, _mm512_set1_epi64(magic));
  1238. if (more & LIBDIVIDE_ADD_MARKER) {
  1239. // must be arithmetic shift
  1240. __m512i sign = _mm512_set1_epi32((int8_t)more >> 7);
  1241. // q += ((numer ^ sign) - sign);
  1242. q = _mm512_add_epi64(q, _mm512_sub_epi64(_mm512_xor_si512(numers, sign), sign));
  1243. }
  1244. // q >>= denom->mult_path.shift
  1245. q = libdivide_s64_shift_right_vector(q, more & LIBDIVIDE_64_SHIFT_MASK);
  1246. q = _mm512_add_epi64(q, _mm512_srli_epi64(q, 63)); // q += (q < 0)
  1247. return q;
  1248. }
  1249. }
  1250. __m512i libdivide_s64_branchfree_do_vector(__m512i numers, const struct libdivide_s64_branchfree_t *denom) {
  1251. int64_t magic = denom->magic;
  1252. uint8_t more = denom->more;
  1253. uint8_t shift = more & LIBDIVIDE_64_SHIFT_MASK;
  1254. // must be arithmetic shift
  1255. __m512i sign = _mm512_set1_epi32((int8_t)more >> 7);
  1256. // libdivide_mullhi_s64(numers, magic);
  1257. __m512i q = libdivide_mullhi_s64_vector(numers, _mm512_set1_epi64(magic));
  1258. q = _mm512_add_epi64(q, numers); // q += numers
  1259. // If q is non-negative, we have nothing to do.
  1260. // If q is negative, we want to add either (2**shift)-1 if d is
  1261. // a power of 2, or (2**shift) if it is not a power of 2.
  1262. uint32_t is_power_of_2 = (magic == 0);
  1263. __m512i q_sign = libdivide_s64_signbits(q); // q_sign = q >> 63
  1264. __m512i mask = _mm512_set1_epi64((1ULL << shift) - is_power_of_2);
  1265. q = _mm512_add_epi64(q, _mm512_and_si512(q_sign, mask)); // q = q + (q_sign & mask)
  1266. q = libdivide_s64_shift_right_vector(q, shift); // q >>= shift
  1267. q = _mm512_sub_epi64(_mm512_xor_si512(q, sign), sign); // q = (q ^ sign) - sign
  1268. return q;
  1269. }
  1270. #elif defined(LIBDIVIDE_AVX2)
  1271. static inline __m256i libdivide_u32_do_vector(__m256i numers, const struct libdivide_u32_t *denom);
  1272. static inline __m256i libdivide_s32_do_vector(__m256i numers, const struct libdivide_s32_t *denom);
  1273. static inline __m256i libdivide_u64_do_vector(__m256i numers, const struct libdivide_u64_t *denom);
  1274. static inline __m256i libdivide_s64_do_vector(__m256i numers, const struct libdivide_s64_t *denom);
  1275. static inline __m256i libdivide_u32_branchfree_do_vector(__m256i numers, const struct libdivide_u32_branchfree_t *denom);
  1276. static inline __m256i libdivide_s32_branchfree_do_vector(__m256i numers, const struct libdivide_s32_branchfree_t *denom);
  1277. static inline __m256i libdivide_u64_branchfree_do_vector(__m256i numers, const struct libdivide_u64_branchfree_t *denom);
  1278. static inline __m256i libdivide_s64_branchfree_do_vector(__m256i numers, const struct libdivide_s64_branchfree_t *denom);
  1279. //////// Internal Utility Functions
  1280. // Implementation of _mm256_srai_epi64(v, 63) (from AVX512).
  1281. static inline __m256i libdivide_s64_signbits(__m256i v) {
  1282. __m256i hiBitsDuped = _mm256_shuffle_epi32(v, _MM_SHUFFLE(3, 3, 1, 1));
  1283. __m256i signBits = _mm256_srai_epi32(hiBitsDuped, 31);
  1284. return signBits;
  1285. }
  1286. // Implementation of _mm256_srai_epi64 (from AVX512).
  1287. static inline __m256i libdivide_s64_shift_right_vector(__m256i v, int amt) {
  1288. const int b = 64 - amt;
  1289. __m256i m = _mm256_set1_epi64x(1ULL << (b - 1));
  1290. __m256i x = _mm256_srli_epi64(v, amt);
  1291. __m256i result = _mm256_sub_epi64(_mm256_xor_si256(x, m), m);
  1292. return result;
  1293. }
  1294. // Here, b is assumed to contain one 32-bit value repeated.
  1295. static inline __m256i libdivide_mullhi_u32_vector(__m256i a, __m256i b) {
  1296. __m256i hi_product_0Z2Z = _mm256_srli_epi64(_mm256_mul_epu32(a, b), 32);
  1297. __m256i a1X3X = _mm256_srli_epi64(a, 32);
  1298. __m256i mask = _mm256_set_epi32(-1, 0, -1, 0, -1, 0, -1, 0);
  1299. __m256i hi_product_Z1Z3 = _mm256_and_si256(_mm256_mul_epu32(a1X3X, b), mask);
  1300. return _mm256_or_si256(hi_product_0Z2Z, hi_product_Z1Z3);
  1301. }
  1302. // b is one 32-bit value repeated.
  1303. static inline __m256i libdivide_mullhi_s32_vector(__m256i a, __m256i b) {
  1304. __m256i hi_product_0Z2Z = _mm256_srli_epi64(_mm256_mul_epi32(a, b), 32);
  1305. __m256i a1X3X = _mm256_srli_epi64(a, 32);
  1306. __m256i mask = _mm256_set_epi32(-1, 0, -1, 0, -1, 0, -1, 0);
  1307. __m256i hi_product_Z1Z3 = _mm256_and_si256(_mm256_mul_epi32(a1X3X, b), mask);
  1308. return _mm256_or_si256(hi_product_0Z2Z, hi_product_Z1Z3);
  1309. }
  1310. // Here, y is assumed to contain one 64-bit value repeated.
  1311. // https://stackoverflow.com/a/28827013
  1312. static inline __m256i libdivide_mullhi_u64_vector(__m256i x, __m256i y) {
  1313. __m256i lomask = _mm256_set1_epi64x(0xffffffff);
  1314. __m256i xh = _mm256_shuffle_epi32(x, 0xB1); // x0l, x0h, x1l, x1h
  1315. __m256i yh = _mm256_shuffle_epi32(y, 0xB1); // y0l, y0h, y1l, y1h
  1316. __m256i w0 = _mm256_mul_epu32(x, y); // x0l*y0l, x1l*y1l
  1317. __m256i w1 = _mm256_mul_epu32(x, yh); // x0l*y0h, x1l*y1h
  1318. __m256i w2 = _mm256_mul_epu32(xh, y); // x0h*y0l, x1h*y0l
  1319. __m256i w3 = _mm256_mul_epu32(xh, yh); // x0h*y0h, x1h*y1h
  1320. __m256i w0h = _mm256_srli_epi64(w0, 32);
  1321. __m256i s1 = _mm256_add_epi64(w1, w0h);
  1322. __m256i s1l = _mm256_and_si256(s1, lomask);
  1323. __m256i s1h = _mm256_srli_epi64(s1, 32);
  1324. __m256i s2 = _mm256_add_epi64(w2, s1l);
  1325. __m256i s2h = _mm256_srli_epi64(s2, 32);
  1326. __m256i hi = _mm256_add_epi64(w3, s1h);
  1327. hi = _mm256_add_epi64(hi, s2h);
  1328. return hi;
  1329. }
  1330. // y is one 64-bit value repeated.
  1331. static inline __m256i libdivide_mullhi_s64_vector(__m256i x, __m256i y) {
  1332. __m256i p = libdivide_mullhi_u64_vector(x, y);
  1333. __m256i t1 = _mm256_and_si256(libdivide_s64_signbits(x), y);
  1334. __m256i t2 = _mm256_and_si256(libdivide_s64_signbits(y), x);
  1335. p = _mm256_sub_epi64(p, t1);
  1336. p = _mm256_sub_epi64(p, t2);
  1337. return p;
  1338. }
  1339. ////////// UINT32
  1340. __m256i libdivide_u32_do_vector(__m256i numers, const struct libdivide_u32_t *denom) {
  1341. uint8_t more = denom->more;
  1342. if (!denom->magic) {
  1343. return _mm256_srli_epi32(numers, more);
  1344. }
  1345. else {
  1346. __m256i q = libdivide_mullhi_u32_vector(numers, _mm256_set1_epi32(denom->magic));
  1347. if (more & LIBDIVIDE_ADD_MARKER) {
  1348. // uint32_t t = ((numer - q) >> 1) + q;
  1349. // return t >> denom->shift;
  1350. uint32_t shift = more & LIBDIVIDE_32_SHIFT_MASK;
  1351. __m256i t = _mm256_add_epi32(_mm256_srli_epi32(_mm256_sub_epi32(numers, q), 1), q);
  1352. return _mm256_srli_epi32(t, shift);
  1353. }
  1354. else {
  1355. return _mm256_srli_epi32(q, more);
  1356. }
  1357. }
  1358. }
  1359. __m256i libdivide_u32_branchfree_do_vector(__m256i numers, const struct libdivide_u32_branchfree_t *denom) {
  1360. __m256i q = libdivide_mullhi_u32_vector(numers, _mm256_set1_epi32(denom->magic));
  1361. __m256i t = _mm256_add_epi32(_mm256_srli_epi32(_mm256_sub_epi32(numers, q), 1), q);
  1362. return _mm256_srli_epi32(t, denom->more);
  1363. }
  1364. ////////// UINT64
  1365. __m256i libdivide_u64_do_vector(__m256i numers, const struct libdivide_u64_t *denom) {
  1366. uint8_t more = denom->more;
  1367. if (!denom->magic) {
  1368. return _mm256_srli_epi64(numers, more);
  1369. }
  1370. else {
  1371. __m256i q = libdivide_mullhi_u64_vector(numers, _mm256_set1_epi64x(denom->magic));
  1372. if (more & LIBDIVIDE_ADD_MARKER) {
  1373. // uint32_t t = ((numer - q) >> 1) + q;
  1374. // return t >> denom->shift;
  1375. uint32_t shift = more & LIBDIVIDE_64_SHIFT_MASK;
  1376. __m256i t = _mm256_add_epi64(_mm256_srli_epi64(_mm256_sub_epi64(numers, q), 1), q);
  1377. return _mm256_srli_epi64(t, shift);
  1378. }
  1379. else {
  1380. return _mm256_srli_epi64(q, more);
  1381. }
  1382. }
  1383. }
  1384. __m256i libdivide_u64_branchfree_do_vector(__m256i numers, const struct libdivide_u64_branchfree_t *denom) {
  1385. __m256i q = libdivide_mullhi_u64_vector(numers, _mm256_set1_epi64x(denom->magic));
  1386. __m256i t = _mm256_add_epi64(_mm256_srli_epi64(_mm256_sub_epi64(numers, q), 1), q);
  1387. return _mm256_srli_epi64(t, denom->more);
  1388. }
  1389. ////////// SINT32
  1390. __m256i libdivide_s32_do_vector(__m256i numers, const struct libdivide_s32_t *denom) {
  1391. uint8_t more = denom->more;
  1392. if (!denom->magic) {
  1393. uint32_t shift = more & LIBDIVIDE_32_SHIFT_MASK;
  1394. uint32_t mask = (1U << shift) - 1;
  1395. __m256i roundToZeroTweak = _mm256_set1_epi32(mask);
  1396. // q = numer + ((numer >> 31) & roundToZeroTweak);
  1397. __m256i q = _mm256_add_epi32(numers, _mm256_and_si256(_mm256_srai_epi32(numers, 31), roundToZeroTweak));
  1398. q = _mm256_srai_epi32(q, shift);
  1399. __m256i sign = _mm256_set1_epi32((int8_t)more >> 7);
  1400. // q = (q ^ sign) - sign;
  1401. q = _mm256_sub_epi32(_mm256_xor_si256(q, sign), sign);
  1402. return q;
  1403. }
  1404. else {
  1405. __m256i q = libdivide_mullhi_s32_vector(numers, _mm256_set1_epi32(denom->magic));
  1406. if (more & LIBDIVIDE_ADD_MARKER) {
  1407. // must be arithmetic shift
  1408. __m256i sign = _mm256_set1_epi32((int8_t)more >> 7);
  1409. // q += ((numer ^ sign) - sign);
  1410. q = _mm256_add_epi32(q, _mm256_sub_epi32(_mm256_xor_si256(numers, sign), sign));
  1411. }
  1412. // q >>= shift
  1413. q = _mm256_srai_epi32(q, more & LIBDIVIDE_32_SHIFT_MASK);
  1414. q = _mm256_add_epi32(q, _mm256_srli_epi32(q, 31)); // q += (q < 0)
  1415. return q;
  1416. }
  1417. }
  1418. __m256i libdivide_s32_branchfree_do_vector(__m256i numers, const struct libdivide_s32_branchfree_t *denom) {
  1419. int32_t magic = denom->magic;
  1420. uint8_t more = denom->more;
  1421. uint8_t shift = more & LIBDIVIDE_32_SHIFT_MASK;
  1422. // must be arithmetic shift
  1423. __m256i sign = _mm256_set1_epi32((int8_t)more >> 7);
  1424. __m256i q = libdivide_mullhi_s32_vector(numers, _mm256_set1_epi32(magic));
  1425. q = _mm256_add_epi32(q, numers); // q += numers
  1426. // If q is non-negative, we have nothing to do
  1427. // If q is negative, we want to add either (2**shift)-1 if d is
  1428. // a power of 2, or (2**shift) if it is not a power of 2
  1429. uint32_t is_power_of_2 = (magic == 0);
  1430. __m256i q_sign = _mm256_srai_epi32(q, 31); // q_sign = q >> 31
  1431. __m256i mask = _mm256_set1_epi32((1U << shift) - is_power_of_2);
  1432. q = _mm256_add_epi32(q, _mm256_and_si256(q_sign, mask)); // q = q + (q_sign & mask)
  1433. q = _mm256_srai_epi32(q, shift); // q >>= shift
  1434. q = _mm256_sub_epi32(_mm256_xor_si256(q, sign), sign); // q = (q ^ sign) - sign
  1435. return q;
  1436. }
  1437. ////////// SINT64
  1438. __m256i libdivide_s64_do_vector(__m256i numers, const struct libdivide_s64_t *denom) {
  1439. uint8_t more = denom->more;
  1440. int64_t magic = denom->magic;
  1441. if (magic == 0) { // shift path
  1442. uint32_t shift = more & LIBDIVIDE_64_SHIFT_MASK;
  1443. uint64_t mask = (1ULL << shift) - 1;
  1444. __m256i roundToZeroTweak = _mm256_set1_epi64x(mask);
  1445. // q = numer + ((numer >> 63) & roundToZeroTweak);
  1446. __m256i q = _mm256_add_epi64(numers, _mm256_and_si256(libdivide_s64_signbits(numers), roundToZeroTweak));
  1447. q = libdivide_s64_shift_right_vector(q, shift);
  1448. __m256i sign = _mm256_set1_epi32((int8_t)more >> 7);
  1449. // q = (q ^ sign) - sign;
  1450. q = _mm256_sub_epi64(_mm256_xor_si256(q, sign), sign);
  1451. return q;
  1452. }
  1453. else {
  1454. __m256i q = libdivide_mullhi_s64_vector(numers, _mm256_set1_epi64x(magic));
  1455. if (more & LIBDIVIDE_ADD_MARKER) {
  1456. // must be arithmetic shift
  1457. __m256i sign = _mm256_set1_epi32((int8_t)more >> 7);
  1458. // q += ((numer ^ sign) - sign);
  1459. q = _mm256_add_epi64(q, _mm256_sub_epi64(_mm256_xor_si256(numers, sign), sign));
  1460. }
  1461. // q >>= denom->mult_path.shift
  1462. q = libdivide_s64_shift_right_vector(q, more & LIBDIVIDE_64_SHIFT_MASK);
  1463. q = _mm256_add_epi64(q, _mm256_srli_epi64(q, 63)); // q += (q < 0)
  1464. return q;
  1465. }
  1466. }
  1467. __m256i libdivide_s64_branchfree_do_vector(__m256i numers, const struct libdivide_s64_branchfree_t *denom) {
  1468. int64_t magic = denom->magic;
  1469. uint8_t more = denom->more;
  1470. uint8_t shift = more & LIBDIVIDE_64_SHIFT_MASK;
  1471. // must be arithmetic shift
  1472. __m256i sign = _mm256_set1_epi32((int8_t)more >> 7);
  1473. // libdivide_mullhi_s64(numers, magic);
  1474. __m256i q = libdivide_mullhi_s64_vector(numers, _mm256_set1_epi64x(magic));
  1475. q = _mm256_add_epi64(q, numers); // q += numers
  1476. // If q is non-negative, we have nothing to do.
  1477. // If q is negative, we want to add either (2**shift)-1 if d is
  1478. // a power of 2, or (2**shift) if it is not a power of 2.
  1479. uint32_t is_power_of_2 = (magic == 0);
  1480. __m256i q_sign = libdivide_s64_signbits(q); // q_sign = q >> 63
  1481. __m256i mask = _mm256_set1_epi64x((1ULL << shift) - is_power_of_2);
  1482. q = _mm256_add_epi64(q, _mm256_and_si256(q_sign, mask)); // q = q + (q_sign & mask)
  1483. q = libdivide_s64_shift_right_vector(q, shift); // q >>= shift
  1484. q = _mm256_sub_epi64(_mm256_xor_si256(q, sign), sign); // q = (q ^ sign) - sign
  1485. return q;
  1486. }
  1487. #elif defined(LIBDIVIDE_SSE2)
  1488. static inline __m128i libdivide_u32_do_vector(__m128i numers, const struct libdivide_u32_t *denom);
  1489. static inline __m128i libdivide_s32_do_vector(__m128i numers, const struct libdivide_s32_t *denom);
  1490. static inline __m128i libdivide_u64_do_vector(__m128i numers, const struct libdivide_u64_t *denom);
  1491. static inline __m128i libdivide_s64_do_vector(__m128i numers, const struct libdivide_s64_t *denom);
  1492. static inline __m128i libdivide_u32_branchfree_do_vector(__m128i numers, const struct libdivide_u32_branchfree_t *denom);
  1493. static inline __m128i libdivide_s32_branchfree_do_vector(__m128i numers, const struct libdivide_s32_branchfree_t *denom);
  1494. static inline __m128i libdivide_u64_branchfree_do_vector(__m128i numers, const struct libdivide_u64_branchfree_t *denom);
  1495. static inline __m128i libdivide_s64_branchfree_do_vector(__m128i numers, const struct libdivide_s64_branchfree_t *denom);
  1496. //////// Internal Utility Functions
  1497. // Implementation of _mm_srai_epi64(v, 63) (from AVX512).
  1498. static inline __m128i libdivide_s64_signbits(__m128i v) {
  1499. __m128i hiBitsDuped = _mm_shuffle_epi32(v, _MM_SHUFFLE(3, 3, 1, 1));
  1500. __m128i signBits = _mm_srai_epi32(hiBitsDuped, 31);
  1501. return signBits;
  1502. }
  1503. // Implementation of _mm_srai_epi64 (from AVX512).
  1504. static inline __m128i libdivide_s64_shift_right_vector(__m128i v, int amt) {
  1505. const int b = 64 - amt;
  1506. __m128i m = _mm_set1_epi64x(1ULL << (b - 1));
  1507. __m128i x = _mm_srli_epi64(v, amt);
  1508. __m128i result = _mm_sub_epi64(_mm_xor_si128(x, m), m);
  1509. return result;
  1510. }
  1511. // Here, b is assumed to contain one 32-bit value repeated.
  1512. static inline __m128i libdivide_mullhi_u32_vector(__m128i a, __m128i b) {
  1513. __m128i hi_product_0Z2Z = _mm_srli_epi64(_mm_mul_epu32(a, b), 32);
  1514. __m128i a1X3X = _mm_srli_epi64(a, 32);
  1515. __m128i mask = _mm_set_epi32(-1, 0, -1, 0);
  1516. __m128i hi_product_Z1Z3 = _mm_and_si128(_mm_mul_epu32(a1X3X, b), mask);
  1517. return _mm_or_si128(hi_product_0Z2Z, hi_product_Z1Z3);
  1518. }
  1519. // SSE2 does not have a signed multiplication instruction, but we can convert
  1520. // unsigned to signed pretty efficiently. Again, b is just a 32 bit value
  1521. // repeated four times.
  1522. static inline __m128i libdivide_mullhi_s32_vector(__m128i a, __m128i b) {
  1523. __m128i p = libdivide_mullhi_u32_vector(a, b);
  1524. // t1 = (a >> 31) & y, arithmetic shift
  1525. __m128i t1 = _mm_and_si128(_mm_srai_epi32(a, 31), b);
  1526. __m128i t2 = _mm_and_si128(_mm_srai_epi32(b, 31), a);
  1527. p = _mm_sub_epi32(p, t1);
  1528. p = _mm_sub_epi32(p, t2);
  1529. return p;
  1530. }
  1531. // Here, y is assumed to contain one 64-bit value repeated.
  1532. // https://stackoverflow.com/a/28827013
  1533. static inline __m128i libdivide_mullhi_u64_vector(__m128i x, __m128i y) {
  1534. __m128i lomask = _mm_set1_epi64x(0xffffffff);
  1535. __m128i xh = _mm_shuffle_epi32(x, 0xB1); // x0l, x0h, x1l, x1h
  1536. __m128i yh = _mm_shuffle_epi32(y, 0xB1); // y0l, y0h, y1l, y1h
  1537. __m128i w0 = _mm_mul_epu32(x, y); // x0l*y0l, x1l*y1l
  1538. __m128i w1 = _mm_mul_epu32(x, yh); // x0l*y0h, x1l*y1h
  1539. __m128i w2 = _mm_mul_epu32(xh, y); // x0h*y0l, x1h*y0l
  1540. __m128i w3 = _mm_mul_epu32(xh, yh); // x0h*y0h, x1h*y1h
  1541. __m128i w0h = _mm_srli_epi64(w0, 32);
  1542. __m128i s1 = _mm_add_epi64(w1, w0h);
  1543. __m128i s1l = _mm_and_si128(s1, lomask);
  1544. __m128i s1h = _mm_srli_epi64(s1, 32);
  1545. __m128i s2 = _mm_add_epi64(w2, s1l);
  1546. __m128i s2h = _mm_srli_epi64(s2, 32);
  1547. __m128i hi = _mm_add_epi64(w3, s1h);
  1548. hi = _mm_add_epi64(hi, s2h);
  1549. return hi;
  1550. }
  1551. // y is one 64-bit value repeated.
  1552. static inline __m128i libdivide_mullhi_s64_vector(__m128i x, __m128i y) {
  1553. __m128i p = libdivide_mullhi_u64_vector(x, y);
  1554. __m128i t1 = _mm_and_si128(libdivide_s64_signbits(x), y);
  1555. __m128i t2 = _mm_and_si128(libdivide_s64_signbits(y), x);
  1556. p = _mm_sub_epi64(p, t1);
  1557. p = _mm_sub_epi64(p, t2);
  1558. return p;
  1559. }
  1560. ////////// UINT32
  1561. __m128i libdivide_u32_do_vector(__m128i numers, const struct libdivide_u32_t *denom) {
  1562. uint8_t more = denom->more;
  1563. if (!denom->magic) {
  1564. return _mm_srli_epi32(numers, more);
  1565. }
  1566. else {
  1567. __m128i q = libdivide_mullhi_u32_vector(numers, _mm_set1_epi32(denom->magic));
  1568. if (more & LIBDIVIDE_ADD_MARKER) {
  1569. // uint32_t t = ((numer - q) >> 1) + q;
  1570. // return t >> denom->shift;
  1571. uint32_t shift = more & LIBDIVIDE_32_SHIFT_MASK;
  1572. __m128i t = _mm_add_epi32(_mm_srli_epi32(_mm_sub_epi32(numers, q), 1), q);
  1573. return _mm_srli_epi32(t, shift);
  1574. }
  1575. else {
  1576. return _mm_srli_epi32(q, more);
  1577. }
  1578. }
  1579. }
  1580. __m128i libdivide_u32_branchfree_do_vector(__m128i numers, const struct libdivide_u32_branchfree_t *denom) {
  1581. __m128i q = libdivide_mullhi_u32_vector(numers, _mm_set1_epi32(denom->magic));
  1582. __m128i t = _mm_add_epi32(_mm_srli_epi32(_mm_sub_epi32(numers, q), 1), q);
  1583. return _mm_srli_epi32(t, denom->more);
  1584. }
  1585. ////////// UINT64
  1586. __m128i libdivide_u64_do_vector(__m128i numers, const struct libdivide_u64_t *denom) {
  1587. uint8_t more = denom->more;
  1588. if (!denom->magic) {
  1589. return _mm_srli_epi64(numers, more);
  1590. }
  1591. else {
  1592. __m128i q = libdivide_mullhi_u64_vector(numers, _mm_set1_epi64x(denom->magic));
  1593. if (more & LIBDIVIDE_ADD_MARKER) {
  1594. // uint32_t t = ((numer - q) >> 1) + q;
  1595. // return t >> denom->shift;
  1596. uint32_t shift = more & LIBDIVIDE_64_SHIFT_MASK;
  1597. __m128i t = _mm_add_epi64(_mm_srli_epi64(_mm_sub_epi64(numers, q), 1), q);
  1598. return _mm_srli_epi64(t, shift);
  1599. }
  1600. else {
  1601. return _mm_srli_epi64(q, more);
  1602. }
  1603. }
  1604. }
  1605. __m128i libdivide_u64_branchfree_do_vector(__m128i numers, const struct libdivide_u64_branchfree_t *denom) {
  1606. __m128i q = libdivide_mullhi_u64_vector(numers, _mm_set1_epi64x(denom->magic));
  1607. __m128i t = _mm_add_epi64(_mm_srli_epi64(_mm_sub_epi64(numers, q), 1), q);
  1608. return _mm_srli_epi64(t, denom->more);
  1609. }
  1610. ////////// SINT32
  1611. __m128i libdivide_s32_do_vector(__m128i numers, const struct libdivide_s32_t *denom) {
  1612. uint8_t more = denom->more;
  1613. if (!denom->magic) {
  1614. uint32_t shift = more & LIBDIVIDE_32_SHIFT_MASK;
  1615. uint32_t mask = (1U << shift) - 1;
  1616. __m128i roundToZeroTweak = _mm_set1_epi32(mask);
  1617. // q = numer + ((numer >> 31) & roundToZeroTweak);
  1618. __m128i q = _mm_add_epi32(numers, _mm_and_si128(_mm_srai_epi32(numers, 31), roundToZeroTweak));
  1619. q = _mm_srai_epi32(q, shift);
  1620. __m128i sign = _mm_set1_epi32((int8_t)more >> 7);
  1621. // q = (q ^ sign) - sign;
  1622. q = _mm_sub_epi32(_mm_xor_si128(q, sign), sign);
  1623. return q;
  1624. }
  1625. else {
  1626. __m128i q = libdivide_mullhi_s32_vector(numers, _mm_set1_epi32(denom->magic));
  1627. if (more & LIBDIVIDE_ADD_MARKER) {
  1628. // must be arithmetic shift
  1629. __m128i sign = _mm_set1_epi32((int8_t)more >> 7);
  1630. // q += ((numer ^ sign) - sign);
  1631. q = _mm_add_epi32(q, _mm_sub_epi32(_mm_xor_si128(numers, sign), sign));
  1632. }
  1633. // q >>= shift
  1634. q = _mm_srai_epi32(q, more & LIBDIVIDE_32_SHIFT_MASK);
  1635. q = _mm_add_epi32(q, _mm_srli_epi32(q, 31)); // q += (q < 0)
  1636. return q;
  1637. }
  1638. }
  1639. __m128i libdivide_s32_branchfree_do_vector(__m128i numers, const struct libdivide_s32_branchfree_t *denom) {
  1640. int32_t magic = denom->magic;
  1641. uint8_t more = denom->more;
  1642. uint8_t shift = more & LIBDIVIDE_32_SHIFT_MASK;
  1643. // must be arithmetic shift
  1644. __m128i sign = _mm_set1_epi32((int8_t)more >> 7);
  1645. __m128i q = libdivide_mullhi_s32_vector(numers, _mm_set1_epi32(magic));
  1646. q = _mm_add_epi32(q, numers); // q += numers
  1647. // If q is non-negative, we have nothing to do
  1648. // If q is negative, we want to add either (2**shift)-1 if d is
  1649. // a power of 2, or (2**shift) if it is not a power of 2
  1650. uint32_t is_power_of_2 = (magic == 0);
  1651. __m128i q_sign = _mm_srai_epi32(q, 31); // q_sign = q >> 31
  1652. __m128i mask = _mm_set1_epi32((1U << shift) - is_power_of_2);
  1653. q = _mm_add_epi32(q, _mm_and_si128(q_sign, mask)); // q = q + (q_sign & mask)
  1654. q = _mm_srai_epi32(q, shift); // q >>= shift
  1655. q = _mm_sub_epi32(_mm_xor_si128(q, sign), sign); // q = (q ^ sign) - sign
  1656. return q;
  1657. }
  1658. ////////// SINT64
  1659. __m128i libdivide_s64_do_vector(__m128i numers, const struct libdivide_s64_t *denom) {
  1660. uint8_t more = denom->more;
  1661. int64_t magic = denom->magic;
  1662. if (magic == 0) { // shift path
  1663. uint32_t shift = more & LIBDIVIDE_64_SHIFT_MASK;
  1664. uint64_t mask = (1ULL << shift) - 1;
  1665. __m128i roundToZeroTweak = _mm_set1_epi64x(mask);
  1666. // q = numer + ((numer >> 63) & roundToZeroTweak);
  1667. __m128i q = _mm_add_epi64(numers, _mm_and_si128(libdivide_s64_signbits(numers), roundToZeroTweak));
  1668. q = libdivide_s64_shift_right_vector(q, shift);
  1669. __m128i sign = _mm_set1_epi32((int8_t)more >> 7);
  1670. // q = (q ^ sign) - sign;
  1671. q = _mm_sub_epi64(_mm_xor_si128(q, sign), sign);
  1672. return q;
  1673. }
  1674. else {
  1675. __m128i q = libdivide_mullhi_s64_vector(numers, _mm_set1_epi64x(magic));
  1676. if (more & LIBDIVIDE_ADD_MARKER) {
  1677. // must be arithmetic shift
  1678. __m128i sign = _mm_set1_epi32((int8_t)more >> 7);
  1679. // q += ((numer ^ sign) - sign);
  1680. q = _mm_add_epi64(q, _mm_sub_epi64(_mm_xor_si128(numers, sign), sign));
  1681. }
  1682. // q >>= denom->mult_path.shift
  1683. q = libdivide_s64_shift_right_vector(q, more & LIBDIVIDE_64_SHIFT_MASK);
  1684. q = _mm_add_epi64(q, _mm_srli_epi64(q, 63)); // q += (q < 0)
  1685. return q;
  1686. }
  1687. }
  1688. __m128i libdivide_s64_branchfree_do_vector(__m128i numers, const struct libdivide_s64_branchfree_t *denom) {
  1689. int64_t magic = denom->magic;
  1690. uint8_t more = denom->more;
  1691. uint8_t shift = more & LIBDIVIDE_64_SHIFT_MASK;
  1692. // must be arithmetic shift
  1693. __m128i sign = _mm_set1_epi32((int8_t)more >> 7);
  1694. // libdivide_mullhi_s64(numers, magic);
  1695. __m128i q = libdivide_mullhi_s64_vector(numers, _mm_set1_epi64x(magic));
  1696. q = _mm_add_epi64(q, numers); // q += numers
  1697. // If q is non-negative, we have nothing to do.
  1698. // If q is negative, we want to add either (2**shift)-1 if d is
  1699. // a power of 2, or (2**shift) if it is not a power of 2.
  1700. uint32_t is_power_of_2 = (magic == 0);
  1701. __m128i q_sign = libdivide_s64_signbits(q); // q_sign = q >> 63
  1702. __m128i mask = _mm_set1_epi64x((1ULL << shift) - is_power_of_2);
  1703. q = _mm_add_epi64(q, _mm_and_si128(q_sign, mask)); // q = q + (q_sign & mask)
  1704. q = libdivide_s64_shift_right_vector(q, shift); // q >>= shift
  1705. q = _mm_sub_epi64(_mm_xor_si128(q, sign), sign); // q = (q ^ sign) - sign
  1706. return q;
  1707. }
  1708. #endif
  1709. /////////// C++ stuff
  1710. #ifdef __cplusplus
  1711. // The C++ divider class is templated on both an integer type
  1712. // (like uint64_t) and an algorithm type.
  1713. // * BRANCHFULL is the default algorithm type.
  1714. // * BRANCHFREE is the branchfree algorithm type.
  1715. enum {
  1716. BRANCHFULL,
  1717. BRANCHFREE
  1718. };
  1719. #if defined(LIBDIVIDE_AVX512)
  1720. #define LIBDIVIDE_VECTOR_TYPE __m512i
  1721. #elif defined(LIBDIVIDE_AVX2)
  1722. #define LIBDIVIDE_VECTOR_TYPE __m256i
  1723. #elif defined(LIBDIVIDE_SSE2)
  1724. #define LIBDIVIDE_VECTOR_TYPE __m128i
  1725. #endif
  1726. #if !defined(LIBDIVIDE_VECTOR_TYPE)
  1727. #define LIBDIVIDE_DIVIDE_VECTOR(ALGO)
  1728. #else
  1729. #define LIBDIVIDE_DIVIDE_VECTOR(ALGO) \
  1730. LIBDIVIDE_VECTOR_TYPE divide(LIBDIVIDE_VECTOR_TYPE n) const { \
  1731. return libdivide_##ALGO##_do_vector(n, &denom); \
  1732. }
  1733. #endif
  1734. // The DISPATCHER_GEN() macro generates C++ methods (for the given integer
  1735. // and algorithm types) that redirect to libdivide's C API.
  1736. #define DISPATCHER_GEN(T, ALGO) \
  1737. libdivide_##ALGO##_t denom; \
  1738. dispatcher() { } \
  1739. dispatcher(T d) \
  1740. : denom(libdivide_##ALGO##_gen(d)) \
  1741. { } \
  1742. T divide(T n) const { \
  1743. return libdivide_##ALGO##_do(n, &denom); \
  1744. } \
  1745. LIBDIVIDE_DIVIDE_VECTOR(ALGO) \
  1746. T recover() const { \
  1747. return libdivide_##ALGO##_recover(&denom); \
  1748. }
  1749. // The dispatcher selects a specific division algorithm for a given
  1750. // type and ALGO using partial template specialization.
  1751. template<bool IS_INTEGRAL, bool IS_SIGNED, int SIZEOF, int ALGO> struct dispatcher { };
  1752. template<> struct dispatcher<true, true, sizeof(int32_t), BRANCHFULL> { DISPATCHER_GEN(int32_t, s32) };
  1753. template<> struct dispatcher<true, true, sizeof(int32_t), BRANCHFREE> { DISPATCHER_GEN(int32_t, s32_branchfree) };
  1754. template<> struct dispatcher<true, false, sizeof(uint32_t), BRANCHFULL> { DISPATCHER_GEN(uint32_t, u32) };
  1755. template<> struct dispatcher<true, false, sizeof(uint32_t), BRANCHFREE> { DISPATCHER_GEN(uint32_t, u32_branchfree) };
  1756. template<> struct dispatcher<true, true, sizeof(int64_t), BRANCHFULL> { DISPATCHER_GEN(int64_t, s64) };
  1757. template<> struct dispatcher<true, true, sizeof(int64_t), BRANCHFREE> { DISPATCHER_GEN(int64_t, s64_branchfree) };
  1758. template<> struct dispatcher<true, false, sizeof(uint64_t), BRANCHFULL> { DISPATCHER_GEN(uint64_t, u64) };
  1759. template<> struct dispatcher<true, false, sizeof(uint64_t), BRANCHFREE> { DISPATCHER_GEN(uint64_t, u64_branchfree) };
  1760. // This is the main divider class for use by the user (C++ API).
  1761. // The actual division algorithm is selected using the dispatcher struct
  1762. // based on the integer and algorithm template parameters.
  1763. template<typename T, int ALGO = BRANCHFULL>
  1764. class divider {
  1765. public:
  1766. // We leave the default constructor empty so that creating
  1767. // an array of dividers and then initializing them
  1768. // later doesn't slow us down.
  1769. divider() { }
  1770. // Constructor that takes the divisor as a parameter
  1771. divider(T d) : div(d) { }
  1772. // Divides n by the divisor
  1773. T divide(T n) const {
  1774. return div.divide(n);
  1775. }
  1776. // Recovers the divisor, returns the value that was
  1777. // used to initialize this divider object.
  1778. T recover() const {
  1779. return div.recover();
  1780. }
  1781. bool operator==(const divider<T, ALGO>& other) const {
  1782. return div.denom.magic == other.denom.magic &&
  1783. div.denom.more == other.denom.more;
  1784. }
  1785. bool operator!=(const divider<T, ALGO>& other) const {
  1786. return !(*this == other);
  1787. }
  1788. #if defined(LIBDIVIDE_VECTOR_TYPE)
  1789. // Treats the vector as packed integer values with the same type as
  1790. // the divider (e.g. s32, u32, s64, u64) and divides each of
  1791. // them by the divider, returning the packed quotients.
  1792. LIBDIVIDE_VECTOR_TYPE divide(LIBDIVIDE_VECTOR_TYPE n) const {
  1793. return div.divide(n);
  1794. }
  1795. #endif
  1796. private:
  1797. // Storage for the actual divisor
  1798. dispatcher<std::is_integral<T>::value,
  1799. std::is_signed<T>::value, sizeof(T), ALGO> div;
  1800. };
  1801. // Overload of operator / for scalar division
  1802. template<typename T, int ALGO>
  1803. T operator/(T n, const divider<T, ALGO>& div) {
  1804. return div.divide(n);
  1805. }
  1806. // Overload of operator /= for scalar division
  1807. template<typename T, int ALGO>
  1808. T& operator/=(T& n, const divider<T, ALGO>& div) {
  1809. n = div.divide(n);
  1810. return n;
  1811. }
  1812. #if defined(LIBDIVIDE_VECTOR_TYPE)
  1813. // Overload of operator / for vector division
  1814. template<typename T, int ALGO>
  1815. LIBDIVIDE_VECTOR_TYPE operator/(LIBDIVIDE_VECTOR_TYPE n, const divider<T, ALGO>& div) {
  1816. return div.divide(n);
  1817. }
  1818. // Overload of operator /= for vector division
  1819. template<typename T, int ALGO>
  1820. LIBDIVIDE_VECTOR_TYPE& operator/=(LIBDIVIDE_VECTOR_TYPE& n, const divider<T, ALGO>& div) {
  1821. n = div.divide(n);
  1822. return n;
  1823. }
  1824. #endif
  1825. // libdivdie::branchfree_divider<T>
  1826. template <typename T>
  1827. using branchfree_divider = divider<T, BRANCHFREE>;
  1828. } // namespace libdivide
  1829. #endif // __cplusplus
  1830. #endif // LIBDIVIDE_H