Unified · Split
| @@ -3,6 +3,7 @@ · 2 unmodified lines | ||
| 3 | 3 | Random snippets of code that are in public domain. |
| 4 | 4 | Just copy and use for whatever you want. |
| 5 | 5 | |
| 6 | +* `c_builtins*` a few __builtin_* for Plan 9, useful for porting other software | |
| 6 | 7 | * `lrint.c` lrint* implementation |
| 7 | 8 | * `msr.c` MSR reading tool |
| 8 | 9 | * `nanosec.c` nanosec(), a replacement for (way more expensive) nsec() |
| @@ -0,0 +1,29 @@ | ||
| 1 | +int | |
| 2 | +__builtin_ctz(unsigned int x) | |
| 3 | +{ | |
| 4 | + unsigned int r; | |
| 5 | + if(x == 0) | |
| 6 | + return 32; | |
| 7 | + for(r = 0; (x & 1) == 0; x >>= 1, r++); | |
| 8 | + return r; | |
| 9 | +} | |
| 10 | + | |
| 11 | +int | |
| 12 | +__builtin_clz(unsigned int x) | |
| 13 | +{ | |
| 14 | + unsigned int r; | |
| 15 | + if(x == 0) | |
| 16 | + return 32; | |
| 17 | + for(r = 0; (x & (1UL<<31)) == 0; x <<= 1, r++); | |
| 18 | + return r; | |
| 19 | +} | |
| 20 | + | |
| 21 | +int | |
| 22 | +__builtin_clzll(unsigned long long x) | |
| 23 | +{ | |
| 24 | + unsigned long long r; | |
| 25 | + if(x == 0) | |
| 26 | + return 64; | |
| 27 | + for(r = 0; (x & (1ULL<<63)) == 0; x <<= 1, r++); | |
| 28 | + return r; | |
| 29 | +} | |
| @@ -0,0 +1,13 @@ | ||
| 1 | +TEXT __builtin_ctz(SB),1,$0 | |
| 2 | + BYTE $0x0F; BYTE $0xBC; BYTE $0xC5 /* BSFL RARG, AX */ | |
| 3 | + RET | |
| 4 | + | |
| 5 | +TEXT __builtin_clz(SB),1,$0 | |
| 6 | + BYTE $0x0F; BYTE $0xBD; BYTE $0xC5 /* BSRL RARG, AX */ | |
| 7 | + XORL $31, AX | |
| 8 | + RET | |
| 9 | + | |
| 10 | +TEXT __builtin_clzll(SB),1,$0 | |
| 11 | + BYTE $0x48; BYTE $0x0F; BYTE $0xBD; BYTE $0xC5 /* BSRQ RARG, AX */ | |
| 12 | + XORL $63, AX | |
| 13 | + RET | |