💾 Archived View for gmi.noulin.net › gitRepositories › dynamicArray › file › libdarray.c.gmi captured on 2023-01-29 at 13:21:43. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
libdarray.c (910B)
1 2 #include "libdarray.h" 3 4 bool dArrayIsValidIndex(intmax_t index) { 5 if (index >= ((intmax_t)1)<<dArrayAddrSpace) { 6 return false; 7 } 8 return true; 9 } 10 11 dArrayIxt dArrayIndex(intmax_t index) { 12 dArrayIxt r; 13 if (index >= ((intmax_t)1)<<dArrayAddrSpace) { 14 r.error = true; 15 return r; 16 } 17 r.error = false; 18 19 intmax_t bufx = index >> dArrayBaseBits; 20 if (!bufx) { 21 r.ix = 0; 22 r.offset = index; 23 } 24 else { 25 //int pos = 0; 26 /* for (int i = (bufx >> 1); i != 0; pos++) { */ 27 /* i >>= 1; */ 28 /* } */ 29 /* find position of MSB */ 30 #if __LP64__ 31 asm ("bsrq %1, %0" : "=r" (r.ix) : "r" (bufx)); 32 #else 33 asm ("bsrl %1, %0" : "=r" (r.ix) : "r" (bufx)); 34 #endif 35 intmax_t mask = ~(0xFFFFFFFFFFFFFFFF << (dArrayBaseBits+r.ix)); 36 r.ix++; 37 r.offset = index & mask; 38 } 39 return r; 40 }