diff --git a/dsp/scale/gyarados.c b/dsp/scale/gyarados.c index 464ce2959..b28abde83 100644 --- a/dsp/scale/gyarados.c +++ b/dsp/scale/gyarados.c @@ -202,8 +202,8 @@ void *Gyarados(long dyw, long dxw, int dst[dyw][dxw], long syw, long sxw, CHECK_LE(sxn, sxw); CHECK_LE(dyn, dyw); CHECK_LE(dxn, dxw); - CHECK_LT(_bsrl(syn) + _bsrl(sxn), 32); - CHECK_LT(_bsrl(dyn) + _bsrl(dxn), 32); + CHECK_LT(bsrl(syn) + bsrl(sxn), 32); + CHECK_LT(bsrl(dyn) + bsrl(dxn), 32); CHECK_LE(dyw, 0x7fff); CHECK_LE(dxw, 0x7fff); CHECK_LE(syw, 0x7fff); diff --git a/libc/calls/fstat-nt.c b/libc/calls/fstat-nt.c index f9a7a8572..c5b54def5 100644 --- a/libc/calls/fstat-nt.c +++ b/libc/calls/fstat-nt.c @@ -71,7 +71,7 @@ static textwindows long GetSizeOfReparsePoint(int64_t fh) { } } if (x >= 0200) { - z += _bsrl(tpenc(x)) >> 3; + z += bsrl(tpenc(x)) >> 3; } ++z; } diff --git a/libc/calls/pledge-linux.c b/libc/calls/pledge-linux.c index d170ec770..b682af953 100644 --- a/libc/calls/pledge-linux.c +++ b/libc/calls/pledge-linux.c @@ -1040,7 +1040,7 @@ static privileged char *FixCpy(char p[17], uint64_t x, int k) { } static privileged char *HexCpy(char p[17], uint64_t x) { - return FixCpy(p, x, ROUNDUP(x ? _bsrl(x) + 1 : 1, 4)); + return FixCpy(p, x, ROUNDUP(x ? bsrl(x) + 1 : 1, 4)); } static privileged int GetPid(void) { diff --git a/libc/calls/sig.c b/libc/calls/sig.c index cab11bec7..b0011fdfe 100644 --- a/libc/calls/sig.c +++ b/libc/calls/sig.c @@ -97,7 +97,7 @@ static textwindows int __sig_getter(atomic_ulong *sigs, sigset_t masked) { for (;;) { pending = atomic_load_explicit(sigs, memory_order_acquire); if ((deliverable = pending & ~masked)) { - sig = _bsfl(deliverable) + 1; + sig = bsfl(deliverable) + 1; bit = 1ull << (sig - 1); if (atomic_fetch_and_explicit(sigs, ~bit, memory_order_acq_rel) & bit) { return sig; diff --git a/libc/fmt/formatbinary64.c b/libc/fmt/formatbinary64.c index bd8e75def..588eaad5d 100644 --- a/libc/fmt/formatbinary64.c +++ b/libc/fmt/formatbinary64.c @@ -45,7 +45,7 @@ char *FormatBinary64(char p[hasatleast 67], uint64_t x, char z) { *p++ = '0'; *p++ = 'b'; } - i = PickGoodWidth(_bsrl(x)); + i = PickGoodWidth(bsrl(x)); do { b = 1; b <<= i; diff --git a/libc/fmt/itoa64radix16.greg.c b/libc/fmt/itoa64radix16.greg.c index d04fe8902..25c5e55a9 100644 --- a/libc/fmt/itoa64radix16.greg.c +++ b/libc/fmt/itoa64radix16.greg.c @@ -22,5 +22,5 @@ #include "libc/macros.internal.h" size_t uint64toarray_radix16(uint64_t x, char b[hasatleast 17]) { - return uint64toarray_fixed16(x, b, ROUNDUP(x ? _bsrl(x) + 1 : 1, 4)); + return uint64toarray_fixed16(x, b, ROUNDUP(x ? bsrl(x) + 1 : 1, 4)); } diff --git a/libc/integral/cxx.inc b/libc/integral/cxx.inc index 5c96555d8..10609b233 100644 --- a/libc/integral/cxx.inc +++ b/libc/integral/cxx.inc @@ -18,12 +18,14 @@ #if 1 template struct __cxx_choose_expr { - __cxx_choose_expr(_T _a, _U _b) : _value(_a) {} + __cxx_choose_expr(_T _a, _U _b) : _value(_a) { + } const _T _value; }; template struct __cxx_choose_expr { - __cxx_choose_expr(_T _a, _U _b) : _value(_b) {} + __cxx_choose_expr(_T _a, _U _b) : _value(_b) { + } const _U _value; }; #define __builtin_choose_expr(X, A, B) \ @@ -32,3 +34,8 @@ struct __cxx_choose_expr { #define __builtin_choose_expr(X, A, B) ((X) ? (A) : (B)) #endif #endif + +#ifdef __aarch64__ +/* todo jart whyyyy */ +#define _Float16 __fp16 +#endif diff --git a/libc/intrin/bsf.c b/libc/intrin/bsf.c index 069e397ce..4c78ced65 100644 --- a/libc/intrin/bsf.c +++ b/libc/intrin/bsf.c @@ -22,7 +22,7 @@ * Returns position of first bit set. * * ctz(𝑥) 31^clz(𝑥) clz(𝑥) - * uint32 𝑥 _bsf(𝑥) tzcnt(𝑥) ffs(𝑥) _bsr(𝑥) lzcnt(𝑥) + * uint32 𝑥 bsf(𝑥) tzcnt(𝑥) ffs(𝑥) bsr(𝑥) lzcnt(𝑥) * 0x00000000 wut 32 0 wut 32 * 0x00000001 0 0 1 0 31 * 0x80000001 0 0 1 31 0 @@ -35,7 +35,7 @@ * @param 𝑥 is a 64-bit integer * @return number in range 0..63 or undefined if 𝑥 is 0 */ -int(_bsfl)(long x) { +int(bsfl)(long x) { unsigned l, r; x &= -x; l = x | x >> 32; @@ -52,7 +52,7 @@ int(_bsfl)(long x) { * Returns position of first bit set. * * ctz(𝑥) 31^clz(𝑥) clz(𝑥) - * uint32 𝑥 _bsf(𝑥) tzcnt(𝑥) ffs(𝑥) _bsr(𝑥) lzcnt(𝑥) + * uint32 𝑥 bsf(𝑥) tzcnt(𝑥) ffs(𝑥) bsr(𝑥) lzcnt(𝑥) * 0x00000000 wut 32 0 wut 32 * 0x00000001 0 0 1 0 31 * 0x80000001 0 0 1 31 0 @@ -65,8 +65,8 @@ int(_bsfl)(long x) { * @param x is a 32-bit integer * @return number in range 0..31 or undefined if 𝑥 is 0 */ -int(_bsf)(int x) { - return _bsf((unsigned)x); +int(bsf)(int x) { + return bsf((unsigned)x); } -__weak_reference(_bsfl, _bsfll); +__weak_reference(bsfl, bsfll); diff --git a/libc/intrin/bsf.h b/libc/intrin/bsf.h index 69931599f..14f62d2e6 100644 --- a/libc/intrin/bsf.h +++ b/libc/intrin/bsf.h @@ -1,16 +1,18 @@ +#ifdef _COSMO_SOURCE #ifndef COSMOPOLITAN_LIBC_NEXGEN32E_BSF_H_ #define COSMOPOLITAN_LIBC_NEXGEN32E_BSF_H_ COSMOPOLITAN_C_START_ -libcesque int _bsf(int) pureconst; -libcesque int _bsfl(long) pureconst; -libcesque int _bsfll(long long) pureconst; +libcesque int bsf(int) pureconst; +libcesque int bsfl(long) pureconst; +libcesque int bsfll(long long) pureconst; #if defined(__GNUC__) && !defined(__STRICT_ANSI__) -#define _bsf(x) __builtin_ctz(x) -#define _bsfl(x) __builtin_ctzl(x) -#define _bsfll(x) __builtin_ctzll(x) +#define bsf(x) __builtin_ctz(x) +#define bsfl(x) __builtin_ctzl(x) +#define bsfll(x) __builtin_ctzll(x) #endif COSMOPOLITAN_C_END_ #endif /* COSMOPOLITAN_LIBC_NEXGEN32E_BSF_H_ */ +#endif /* _COSMO_SOURCE */ diff --git a/libc/intrin/bsr.c b/libc/intrin/bsr.c index eddc31d7b..95417420c 100644 --- a/libc/intrin/bsr.c +++ b/libc/intrin/bsr.c @@ -23,7 +23,7 @@ * Returns binary logarithm of 𝑥. * * ctz(𝑥) 31^clz(𝑥) clz(𝑥) - * uint32 𝑥 _bsf(𝑥) tzcnt(𝑥) ffs(𝑥) _bsr(𝑥) lzcnt(𝑥) + * uint32 𝑥 bsf(𝑥) tzcnt(𝑥) ffs(𝑥) bsr(𝑥) lzcnt(𝑥) * 0x00000000 wut 32 0 wut 32 * 0x00000001 0 0 1 0 31 * 0x80000001 0 0 1 31 0 @@ -36,7 +36,7 @@ * @param x is a 32-bit integer * @return number in range 0..31 or undefined if 𝑥 is 0 */ -int(_bsr)(int x) { +int(bsr)(int x) { int r = 0; if(x & 0xFFFF0000u) { x >>= 16; r |= 16; } if(x & 0xFF00) { x >>= 8; r |= 8; } diff --git a/libc/intrin/bsr.h b/libc/intrin/bsr.h index 8004eb9ee..862385c93 100644 --- a/libc/intrin/bsr.h +++ b/libc/intrin/bsr.h @@ -1,16 +1,18 @@ +#ifdef _COSMO_SOURCE #ifndef COSMOPOLITAN_LIBC_NEXGEN32E_BSR_H_ #define COSMOPOLITAN_LIBC_NEXGEN32E_BSR_H_ COSMOPOLITAN_C_START_ -libcesque int _bsr(int) pureconst; -libcesque int _bsrl(long) pureconst; -libcesque int _bsrll(long long) pureconst; +libcesque int bsr(int) pureconst; +libcesque int bsrl(long) pureconst; +libcesque int bsrll(long long) pureconst; #if defined(__GNUC__) && !defined(__STRICT_ANSI__) -#define _bsr(x) (__builtin_clz(x) ^ (sizeof(int) * 8 - 1)) -#define _bsrl(x) (__builtin_clzl(x) ^ (sizeof(long) * 8 - 1)) -#define _bsrll(x) (__builtin_clzll(x) ^ (sizeof(long long) * 8 - 1)) +#define bsr(x) (__builtin_clz(x) ^ (sizeof(int) * 8 - 1)) +#define bsrl(x) (__builtin_clzl(x) ^ (sizeof(long) * 8 - 1)) +#define bsrll(x) (__builtin_clzll(x) ^ (sizeof(long long) * 8 - 1)) #endif COSMOPOLITAN_C_END_ #endif /* COSMOPOLITAN_LIBC_NEXGEN32E_BSR_H_ */ +#endif /* _COSMO_SOURCE */ diff --git a/libc/intrin/bsrl.c b/libc/intrin/bsrl.c index 54284e965..76e31c2f7 100644 --- a/libc/intrin/bsrl.c +++ b/libc/intrin/bsrl.c @@ -29,7 +29,7 @@ static const char kDebruijn[64] = { * Returns binary logarithm of 𝑥. * * ctz(𝑥) 31^clz(𝑥) clz(𝑥) - * uint32 𝑥 _bsf(𝑥) tzcnt(𝑥) ffs(𝑥) _bsr(𝑥) lzcnt(𝑥) + * uint32 𝑥 bsf(𝑥) tzcnt(𝑥) ffs(𝑥) bsr(𝑥) lzcnt(𝑥) * 0x00000000 wut 32 0 wut 32 * 0x00000001 0 0 1 0 31 * 0x80000001 0 0 1 31 0 @@ -42,7 +42,7 @@ static const char kDebruijn[64] = { * @param x is a 64-bit integer * @return number in range 0..63 or undefined if 𝑥 is 0 */ -int(_bsrl)(long x) { +int(bsrl)(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; @@ -52,4 +52,4 @@ int(_bsrl)(long x) { return kDebruijn[(x * 0x03f79d71b4cb0a89ull) >> 58]; } -__weak_reference(_bsrl, _bsrll); +__weak_reference(bsrl, bsrll); diff --git a/libc/intrin/cxaatexit.c b/libc/intrin/cxaatexit.c index bc6b97daf..706e4af52 100644 --- a/libc/intrin/cxaatexit.c +++ b/libc/intrin/cxaatexit.c @@ -58,7 +58,7 @@ int __cxa_atexit(void *fp, void *arg, void *pred) { return enomem(); } } - i = _bsr(~b->mask); + i = bsr(~b->mask); b->mask |= 1u << i; b->p[i].fp = fp; b->p[i].arg = arg; diff --git a/libc/intrin/cxafinalize.c b/libc/intrin/cxafinalize.c index f74207ce2..119bebdd4 100644 --- a/libc/intrin/cxafinalize.c +++ b/libc/intrin/cxafinalize.c @@ -43,7 +43,7 @@ StartOverLocked: for (;;) { mask = b->mask; while (mask) { - i = _bsf(mask); + i = bsf(mask); mask &= ~(1u << i); if (!pred || pred == b->p[i].pred) { b->mask &= ~(1u << i); diff --git a/libc/intrin/describefdset.c b/libc/intrin/describefdset.c index b42c362c2..751652c1e 100644 --- a/libc/intrin/describefdset.c +++ b/libc/intrin/describefdset.c @@ -43,7 +43,7 @@ const char *(DescribeFdSet)(char buf[N], ssize_t rc, int nfds, fd_set *fds) { for (int fd = 0; fd < nfds; fd += 64) { uint64_t w = fds->fds_bits[fd >> 6]; while (w) { - unsigned m = _bsr(w); + unsigned m = bsr(w); w &= ~((uint64_t)1 << m); if (fd + m < nfds) { if (!gotsome) { diff --git a/libc/intrin/formathex64.c b/libc/intrin/formathex64.c index 958924d4e..1324662d7 100644 --- a/libc/intrin/formathex64.c +++ b/libc/intrin/formathex64.c @@ -48,7 +48,7 @@ char *FormatHex64(char p[hasatleast 19], uint64_t x, char z) { *p++ = '0'; *p++ = 'x'; } - i = PickGoodWidth(_bsrl(x), z); + i = PickGoodWidth(bsrl(x), z); do { *p++ = "0123456789abcdef"[(x >> (i -= 4)) & 15]; } while (i); diff --git a/libc/intrin/tpenc.c b/libc/intrin/tpenc.c index 08eb06036..571209dd6 100644 --- a/libc/intrin/tpenc.c +++ b/libc/intrin/tpenc.c @@ -34,7 +34,7 @@ uint64_t tpenc(uint32_t c) { int e, n; uint64_t w; if (0 <= c && c <= 127) return c; - e = kTpEnc[_bsr(c) - 7]; + e = kTpEnc[bsr(c) - 7]; n = e & 0xff; w = 0; do { diff --git a/libc/log/cxaprintexits.c b/libc/log/cxaprintexits.c index a9b6354f5..a7be1130c 100644 --- a/libc/log/cxaprintexits.c +++ b/libc/log/cxaprintexits.c @@ -41,7 +41,7 @@ void __cxa_printexits(FILE *f, void *pred) { do { mask = b->mask; while (mask) { - i = _bsf(mask); + i = bsf(mask); mask &= ~(1u << i); if (!pred || pred == b->p[i].pred) { symbol = GetSymbolByAddr((intptr_t)b->p[i].fp); diff --git a/libc/proc/posix_spawn.c b/libc/proc/posix_spawn.c index affd84d06..8390c5c52 100644 --- a/libc/proc/posix_spawn.c +++ b/libc/proc/posix_spawn.c @@ -587,7 +587,7 @@ errno_t posix_spawn(int *pid, const char *path, if (flags & POSIX_SPAWN_SETRLIMIT) { int rlimset = (*attrp)->rlimset; while (rlimset) { - int resource = _bsf(rlimset); + int resource = bsf(rlimset); rlimset &= ~(1u << resource); if (setrlimit(resource, (*attrp)->rlim + resource)) { // MacOS ARM64 RLIMIT_STACK always returns EINVAL diff --git a/libc/runtime/mmap.c b/libc/runtime/mmap.c index 9029916e9..68bda1c2b 100644 --- a/libc/runtime/mmap.c +++ b/libc/runtime/mmap.c @@ -73,7 +73,7 @@ #define FRAME(x) ((int)((intptr_t)(x) >> 16)) static inline pureconst unsigned long __rounddown2pow(unsigned long x) { - return x ? 1ul << _bsrl(x) : 0; + return x ? 1ul << bsrl(x) : 0; } static wontreturn void __mmap_die(const char *s) { diff --git a/libc/stdio/appendr.c b/libc/stdio/appendr.c index ce591b59a..d5554531a 100644 --- a/libc/stdio/appendr.c +++ b/libc/stdio/appendr.c @@ -55,7 +55,7 @@ ssize_t appendr(char **b, size_t i) { z = appendz((p = *b)); if (i != z.i || !p) { n = ROUNDUP(i + 1, 8) + W; - if (n > z.n || _bsrl(n) < _bsrl(z.n)) { + if (n > z.n || bsrl(n) < bsrl(z.n)) { if ((p = realloc(p, n))) { z.n = malloc_usable_size(p); unassert(!(z.n & (W - 1))); diff --git a/libc/stdio/appendw.c b/libc/stdio/appendw.c index 7d11b76a6..c5848b70d 100644 --- a/libc/stdio/appendw.c +++ b/libc/stdio/appendw.c @@ -18,10 +18,10 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/assert.h" #include "libc/dce.h" -#include "libc/serialize.h" #include "libc/intrin/bsr.h" #include "libc/macros.internal.h" #include "libc/mem/mem.h" +#include "libc/serialize.h" #include "libc/stdio/append.h" #define W sizeof(size_t) @@ -59,7 +59,7 @@ ssize_t appendw(char **b, uint64_t w) { char *p, *q; struct appendz z; z = appendz((p = *b)); - l = w ? (_bsrl(w) >> 3) + 1 : 1; + l = w ? (bsrl(w) >> 3) + 1 : 1; n = ROUNDUP(z.i + 8 + 1, 8) + W; if (n > z.n) { if (!z.n) z.n = W * 2; diff --git a/libc/stdio/fmt.c b/libc/stdio/fmt.c index 12de10d52..b90f936c8 100644 --- a/libc/stdio/fmt.c +++ b/libc/stdio/fmt.c @@ -359,14 +359,14 @@ static int __fmt_stoa_wide(out_f out, void *a, uint64_t w) { char buf[8]; if (!isascii(w)) w = tpenc(w); WRITE64LE(buf, w); - return out(buf, a, w ? (_bsr(w) >> 3) + 1 : 1); + return out(buf, a, w ? (bsr(w) >> 3) + 1 : 1); } static int __fmt_stoa_bing(out_f out, void *a, uint64_t w) { char buf[8]; w = tpenc(kCp437[w & 0xFF]); WRITE64LE(buf, w); - return out(buf, a, w ? (_bsr(w) >> 3) + 1 : 1); + return out(buf, a, w ? (bsr(w) >> 3) + 1 : 1); } static int __fmt_stoa_quoted(out_f out, void *a, uint64_t w) { @@ -377,7 +377,7 @@ static int __fmt_stoa_quoted(out_f out, void *a, uint64_t w) { w = tpenc(w); } WRITE64LE(buf, w); - return out(buf, a, w ? (_bsr(w) >> 3) + 1 : 1); + return out(buf, a, w ? (bsr(w) >> 3) + 1 : 1); } /** diff --git a/libc/str/thompike.h b/libc/str/thompike.h index c4c4512b3..cafbed09d 100644 --- a/libc/str/thompike.h +++ b/libc/str/thompike.h @@ -5,7 +5,7 @@ #define ThomPikeCont(x) (0200 == (0300 & (x))) #define ThomPikeByte(x) ((x) & (((1 << ThomPikeMsb(x)) - 1) | 3)) #define ThomPikeLen(x) (7 - ThomPikeMsb(x)) -#define ThomPikeMsb(x) ((255 & (x)) < 252 ? _bsr(255 & ~(x)) : 1) +#define ThomPikeMsb(x) ((255 & (x)) < 252 ? bsr(255 & ~(x)) : 1) #define ThomPikeMerge(x, y) ((x) << 6 | (077 & (y))) #endif /* COSMOPOLITAN_LIBC_STR_THOMPIKE_H_ */ diff --git a/libc/str/tpdecodecb.internal.h b/libc/str/tpdecodecb.internal.h index 83806be99..eddbdfd26 100644 --- a/libc/str/tpdecodecb.internal.h +++ b/libc/str/tpdecodecb.internal.h @@ -18,7 +18,7 @@ forceinline int tpdecodecb(wint_t *out, int first, if ((wc = get(arg, i++)) == -1) return -1; } if (__builtin_expect(!(0 <= wc && wc <= 0x7F), 0)) { - msb = wc < 252 ? _bsr(~wc & 0xff) : 1; + msb = wc < 252 ? bsr(~wc & 0xff) : 1; need = 7 - msb; wc &= ((1u << msb) - 1) | 0b00000011; for (j = 1; j < need; ++j) { diff --git a/libc/x/utf16to8.c b/libc/x/utf16to8.c index 034408d9c..ba29cd1bd 100644 --- a/libc/x/utf16to8.c +++ b/libc/x/utf16to8.c @@ -16,7 +16,6 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ -#include "libc/serialize.h" #include "libc/intrin/bsr.h" #include "libc/intrin/packsswb.h" #include "libc/intrin/pandn.h" @@ -26,6 +25,7 @@ #include "libc/intrin/punpckhbw.h" #include "libc/intrin/punpcklbw.h" #include "libc/mem/mem.h" +#include "libc/serialize.h" #include "libc/str/str.h" #include "libc/str/thompike.h" #include "libc/str/utf16.h" @@ -78,7 +78,7 @@ char *utf16to8(const char16_t *p, size_t n, size_t *z) { } else { w = tpenc(x); WRITE64LE(q, w); - q += _bsr(w) >> 3; + q += bsr(w) >> 3; q += 1; } } diff --git a/net/finger/fingersyn.c b/net/finger/fingersyn.c index a9e0d1370..eb3d24fab 100644 --- a/net/finger/fingersyn.c +++ b/net/finger/fingersyn.c @@ -36,7 +36,7 @@ uint32_t FingerSyn(const char *p, size_t n) { // ttl<=128 Windows, OpenVMS 8+ // ttl<=64 Mac, Linux, BSD, Solaris 8+, Tru64, HP-UX ttl = p[8] & 255; - h += _bsr(MAX(1, ttl - 1)); + h += bsr(MAX(1, ttl - 1)); h *= 0x9e3779b1; // TCP Options // We care about the order and presence of leading common options. diff --git a/net/finger/getosname.c b/net/finger/getosname.c index 2b9019460..7e6b88f8c 100644 --- a/net/finger/getosname.c +++ b/net/finger/getosname.c @@ -21,7 +21,7 @@ const char *GetOsName(int os) { if (os) { - switch (_bsr(os)) { + switch (bsr(os)) { case 0: return "LINUX"; case 1: diff --git a/net/turfwar/turfwar.c b/net/turfwar/turfwar.c index 41e7d3b30..ff0b650c1 100644 --- a/net/turfwar/turfwar.c +++ b/net/turfwar/turfwar.c @@ -1538,7 +1538,7 @@ bool GeneratePlot(struct Asset *out, long block, long cash) { CHECK_SQL(sqlite3_exec(db, "BEGIN TRANSACTION", 0, 0, 0)); area = XN * YN; mask = area - 1; - clump = 32 - _bsr(area) - 8; + clump = 32 - bsr(area) - 8; while ((rc = DbStep(stmt)) != SQLITE_DONE) { if (rc != SQLITE_ROW) CHECK_DB(rc); ip = sqlite3_column_int64(stmt, 0); diff --git a/test/libc/str/bsr_test.c b/test/libc/str/bsr_test.c index a1f3242d4..ca40c4e07 100644 --- a/test/libc/str/bsr_test.c +++ b/test/libc/str/bsr_test.c @@ -20,7 +20,7 @@ #include "libc/str/str.h" #include "libc/testlib/testlib.h" -static unsigned _Bsr(unsigned x) { +static unsigned Bsr(unsigned x) { static const char kDebruijn[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31, @@ -35,8 +35,8 @@ static unsigned _Bsr(unsigned x) { return kDebruijn[x]; } -TEST(_bsr, test) { +TEST(bsr, test) { for (int i = 1; i < 1000; ++i) { - ASSERT_EQ(_bsr(i), _Bsr(i)); + ASSERT_EQ(bsr(i), Bsr(i)); } } diff --git a/third_party/chibicc/as.c b/third_party/chibicc/as.c index d8494c479..e4aa4b02c 100644 --- a/third_party/chibicc/as.c +++ b/third_party/chibicc/as.c @@ -2028,7 +2028,7 @@ static int ParseModrm(struct As *a, int *disp) { if (((reg & 070) >> 3) == 2) modrm |= HASASZ; // asz if (IsComma(a)) { ++a->i; - modrm |= (_bsr(GetInt(a)) & 3) << 6; + modrm |= (bsr(GetInt(a)) & 3) << 6; } } ConsumePunct(a, ')'); diff --git a/third_party/chibicc/asm.c b/third_party/chibicc/asm.c index 11f95cf6d..7a10b8631 100644 --- a/third_party/chibicc/asm.c +++ b/third_party/chibicc/asm.c @@ -301,7 +301,7 @@ static void PickAsmRegisters(Asm *a) { if (!(m = a->ops[i].regmask)) break; if (popcnt(m) != j) break; if (!(m &= regset)) CouldNotAllocateRegister(&a->ops[i], "rm"); - pick = 1 << (a->ops[i].reg = _bsf(m)); + pick = 1 << (a->ops[i].reg = bsf(m)); if (pick & PRECIOUS) a->regclob |= pick; regset &= ~pick; a->ops[i].regmask = 0; @@ -309,14 +309,14 @@ static void PickAsmRegisters(Asm *a) { case kAsmXmm: if (!(m = a->ops[i].regmask)) break; if (!(m &= xmmset)) CouldNotAllocateRegister(&a->ops[i], "xmm"); - xmmset &= ~(1 << (a->ops[i].reg = _bsf(m))); + xmmset &= ~(1 << (a->ops[i].reg = bsf(m))); a->ops[i].regmask = 0; break; case kAsmFpu: if (!(m = a->ops[i].x87mask)) break; if (popcnt(m) != j) break; if (!(m &= x87sts)) CouldNotAllocateRegister(&a->ops[i], "fpu"); - x87sts &= ~(1 << (a->ops[i].reg = _bsf(m))); + x87sts &= ~(1 << (a->ops[i].reg = bsf(m))); a->ops[i].x87mask = 0; break; default: @@ -540,7 +540,7 @@ static char *HandleAsmSpecifier(Asm *a, char *p) { if ((i = c - '0') >= a->n) { error_tok(a->tok, "bad asm reference at offset %d", p - a->str); } - z = _bsr(a->ops[i].node->ty->size); + z = bsr(a->ops[i].node->ty->size); if (z > 3 && a->ops[i].type == kAsmReg) { error_tok(a->tok, "bad asm op size"); } @@ -679,7 +679,7 @@ static void StoreAsmOutputs(Asm *a) { println("\tset%s\t(%%rax)", a->ops[i].str + a->ops[i].predicate); break; case kAsmReg: - z = _bsr(a->ops[i].node->ty->size); + z = bsr(a->ops[i].node->ty->size); if (a->ops[i].reg) { gen_addr(a->ops[i].node); if (z > 3) error_tok(a->tok, "bad asm out size"); @@ -734,7 +734,7 @@ static void StoreAsmOutputs(Asm *a) { static void PushClobbers(Asm *a) { int i, regs = a->regclob & PRECIOUS; while (regs) { - i = _bsf(regs); + i = bsf(regs); pushreg(kGreg[3][i]); regs &= ~(1 << i); } @@ -743,7 +743,7 @@ static void PushClobbers(Asm *a) { static void PopClobbers(Asm *a) { int i, regs = a->regclob & PRECIOUS; while (regs) { - i = _bsr(regs); + i = bsr(regs); popreg(kGreg[3][i]); regs &= ~(1 << i); } diff --git a/third_party/chibicc/dox2.c b/third_party/chibicc/dox2.c index d034b8a4b..2dc459203 100644 --- a/third_party/chibicc/dox2.c +++ b/third_party/chibicc/dox2.c @@ -281,7 +281,7 @@ static int CompareDoxIndexEntry(const void *p1, const void *p2, void *arg) { } static unsigned long roundup2pow(unsigned long x) { - return x > 1 ? 2ul << _bsrl(x - 1) : x ? 1 : 0; + return x > 1 ? 2ul << bsrl(x - 1) : x ? 1 : 0; } static void IndexDox(struct Dox *dox) { diff --git a/third_party/chibicc/file.c b/third_party/chibicc/file.c index 6c192d606..810899418 100644 --- a/third_party/chibicc/file.c +++ b/third_party/chibicc/file.c @@ -67,7 +67,7 @@ void canonicalize_newline(char *p) { p += 16; q += 16; } else { - m = _bsf(m); + m = bsf(m); memmove(q, p, m); p += m; q += m; @@ -126,7 +126,7 @@ void remove_backslash_newline(char *p) { i += 16; j += 16; } else { - m = _bsf(m); + m = bsf(m); memmove(p + j, p + i, m); i += m; j += m; diff --git a/third_party/chibicc/test/implicit_test.c b/third_party/chibicc/test/implicit_test.c index 24f3be8a2..5447b2198 100644 --- a/third_party/chibicc/test/implicit_test.c +++ b/third_party/chibicc/test/implicit_test.c @@ -9,7 +9,7 @@ implicit_functions_are_long() { } external_functions_are_long() { - if (_bsrl(0x0001000000000000) != 48) { + if (bsrl(0x0001000000000000) != 48) { __builtin_trap(); } } diff --git a/third_party/chibicc/tokenize.c b/third_party/chibicc/tokenize.c index 9eed66f69..15ba6c6e5 100644 --- a/third_party/chibicc/tokenize.c +++ b/third_party/chibicc/tokenize.c @@ -680,7 +680,7 @@ static void convert_universal_chars(char *p) { p += 16; q += 16; } else { - m = _bsf(m); + m = bsf(m); memmove(q, p, m); p += m; q += m; diff --git a/third_party/dlmalloc/dlmalloc.c b/third_party/dlmalloc/dlmalloc.c index 5b2ae8891..d13bff0d9 100644 --- a/third_party/dlmalloc/dlmalloc.c +++ b/third_party/dlmalloc/dlmalloc.c @@ -935,7 +935,7 @@ static void* internal_memalign(mstate m, size_t alignment, size_t bytes) { if (alignment < MIN_CHUNK_SIZE) /* must be at least a minimum chunk size */ alignment = MIN_CHUNK_SIZE; /* alignment is 32+ bytes rounded up to nearest two power */ - alignment = 2ul << _bsrl(MAX(MIN_CHUNK_SIZE, alignment) - 1); + alignment = 2ul << bsrl(MAX(MIN_CHUNK_SIZE, alignment) - 1); if (bytes >= MAX_REQUEST - alignment) { if (m != 0) { /* Test isn't needed but avoids compiler warning */ MALLOC_FAILURE_ACTION; diff --git a/third_party/linenoise/linenoise.c b/third_party/linenoise/linenoise.c index cf673ba1f..b441b2af6 100644 --- a/third_party/linenoise/linenoise.c +++ b/third_party/linenoise/linenoise.c @@ -369,7 +369,7 @@ static wint_t Capitalize(wint_t c) { static struct rune DecodeUtf8(int c) { struct rune r; if (c < 252) { - r.n = _bsr(255 & ~c); + r.n = bsr(255 & ~c); r.c = c & (((1 << r.n) - 1) | 3); r.n = 6 - r.n; } else { @@ -645,7 +645,7 @@ static void abAppendw(struct abuf *a, unsigned long long w) { p[5] = (0x0000ff0000000000 & w) >> 050; p[6] = (0x00ff000000000000 & w) >> 060; p[7] = (0xff00000000000000 & w) >> 070; - a->len += w ? (_bsrll(w) >> 3) + 1 : 1; + a->len += w ? (bsrll(w) >> 3) + 1 : 1; p[8] = 0; } @@ -1634,7 +1634,7 @@ static size_t linenoiseEscape(char *d, const char *s, size_t n) { break; } WRITE32LE(p, w); - p += (_bsr(w) >> 3) + 1; + p += (bsr(w) >> 3) + 1; l = w; } return p - d; diff --git a/third_party/lua/lobject.h b/third_party/lua/lobject.h index b02a6e2ff..9f3bd5e24 100644 --- a/third_party/lua/lobject.h +++ b/third_party/lua/lobject.h @@ -785,7 +785,7 @@ LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen); ** Computes ceil(log2(x)) */ static inline int luaO_ceillog2 (unsigned int x) { - return --x ? _bsr(x) + 1 : 0; + return --x ? bsr(x) + 1 : 0; } #endif diff --git a/third_party/python/Parser/node.c b/third_party/python/Parser/node.c index 56591aec9..e2c7107b0 100644 --- a/third_party/python/Parser/node.c +++ b/third_party/python/Parser/node.c @@ -31,7 +31,7 @@ fancy_roundup(int x) /* Round up to the closest power of 2 >= n. */ int r; assert(x > 128); - r = 1u << (_bsr(x - 1) + 1); /* hacker's delight */ + r = 1u << (bsr(x - 1) + 1); /* hacker's delight */ return r > 0 ? r : -1; } diff --git a/third_party/xed/x86ild.greg.c b/third_party/xed/x86ild.greg.c index 5bcb2d927..fe70349e2 100644 --- a/third_party/xed/x86ild.greg.c +++ b/third_party/xed/x86ild.greg.c @@ -878,7 +878,7 @@ privileged static void xed_evex_scanner(struct XedDecodedInst *d) { } privileged static uint64_t xed_read_number(uint8_t *p, size_t n, bool s) { - switch (s << 2 | _bsr(n)) { + switch (s << 2 | bsr(n)) { case 0b000: return *p; case 0b100: diff --git a/tool/build/lib/panel.c b/tool/build/lib/panel.c index d247a33c1..162cf8a46 100644 --- a/tool/build/lib/panel.c +++ b/tool/build/lib/panel.c @@ -32,7 +32,7 @@ static int tpdecode(const char *s, wint_t *out) { if ((wc = s[i++] & 255) == -1) return -1; } if (!(0 <= wc && wc <= 0x7F)) { - msb = wc < 252 ? _bsr(~wc & 0xff) : 1; + msb = wc < 252 ? bsr(~wc & 0xff) : 1; need = 7 - msb; wc &= ((1u << msb) - 1) | 0003; for (j = 1; j < need; ++j) { diff --git a/tool/emacs/cosmo-c-builtins.el b/tool/emacs/cosmo-c-builtins.el index 3dee199be..eb0362b18 100644 --- a/tool/emacs/cosmo-c-builtins.el +++ b/tool/emacs/cosmo-c-builtins.el @@ -190,6 +190,9 @@ "__integer_pack" "__is_same")) + (cuda-builtin-types + '("__syncthreads")) + (cosmopolitan-builtin-defines '("COSMOPOLITAN_CXX_START_" "COSMOPOLITAN_CXX_END_" @@ -225,6 +228,7 @@ gcc-builtin-functions-sync gcc-builtin-functions-ia32 gxx-builtin-type-traits + cuda-builtin-types cosmopolitan-builtin-defines cosmopolitan-builtin-functions)) "\\_>"))) diff --git a/tool/emacs/cosmo-platform-constants.el b/tool/emacs/cosmo-platform-constants.el index ec2cc0f2c..caf232607 100644 --- a/tool/emacs/cosmo-platform-constants.el +++ b/tool/emacs/cosmo-platform-constants.el @@ -74,11 +74,13 @@ "_OPENMP")) (cuda - '("__NVCC__")) + '("__NVCC__" + "__CUDA_ARCH__")) (hip '("__HIP__" "__HIPCC__" + "__HIP_PLATFORM_AMD__" "__HIP_DEVICE_COMPILE__" "__AMD__" "__AMDGCN__" diff --git a/tool/net/lfuncs.c b/tool/net/lfuncs.c index f31c70466..1949f1649 100644 --- a/tool/net/lfuncs.c +++ b/tool/net/lfuncs.c @@ -342,7 +342,7 @@ int LuaPopcnt(lua_State *L) { int LuaBsr(lua_State *L) { long x; if ((x = luaL_checkinteger(L, 1))) { - lua_pushinteger(L, _bsrl(x)); + lua_pushinteger(L, bsrl(x)); return 1; } else { luaL_argerror(L, 1, "zero"); @@ -353,7 +353,7 @@ int LuaBsr(lua_State *L) { int LuaBsf(lua_State *L) { long x; if ((x = luaL_checkinteger(L, 1))) { - lua_pushinteger(L, _bsfl(x)); + lua_pushinteger(L, bsfl(x)); return 1; } else { luaL_argerror(L, 1, "zero"); diff --git a/tool/net/redbean.c b/tool/net/redbean.c index 762d654b3..e3c741b7a 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -2147,7 +2147,7 @@ static void FreeStrings(struct Strings *l) { } static unsigned long roundup2pow(unsigned long x) { - return x > 1 ? 2ul << _bsrl(x - 1) : x ? 1 : 0; + return x > 1 ? 2ul << bsrl(x - 1) : x ? 1 : 0; } static void IndexAssets(void) { @@ -2943,7 +2943,7 @@ static int GetDecimalWidth(long x) { } static int GetOctalWidth(int x) { - return !x ? 1 : x < 8 ? 2 : 1 + _bsr(x) / 3; + return !x ? 1 : x < 8 ? 2 : 1 + bsr(x) / 3; } static const char *DescribeCompressionRatio(char rb[8], uint8_t *zcf) { diff --git a/tool/plinko/lib/gc.c b/tool/plinko/lib/gc.c index e9ad821cb..53bb04891 100644 --- a/tool/plinko/lib/gc.c +++ b/tool/plinko/lib/gc.c @@ -93,7 +93,7 @@ int Census(struct Gc *G) { if (!~G->M[j]) { l += DWBITS; } else { - l += _bsfl(~G->M[j]); + l += bsfl(~G->M[j]); break; } } @@ -124,7 +124,7 @@ void Sweep(struct Gc *G) { for (; i < G->n; ++i) { m = G->M[i]; if (~m) { - j = _bsfl(~m); + j = bsfl(~m); m >>= j; m <<= j; d -= j; @@ -136,7 +136,7 @@ void Sweep(struct Gc *G) { } for (; i < G->n; b -= DWBITS, m = G->M[++i]) { for (; m; m &= ~((dword)1 << j)) { - a = b + ~(j = _bsfl(m)); + a = b + ~(j = bsfl(m)); Set(--d, MAKE(Relocate(G, LO(Get(a))), Relocate(G, HI(Get(a))))); SetShadow(d, MAKE(LO(GetShadow(a)), Relocate(G, HI(GetShadow(a))))); } diff --git a/tool/plinko/lib/histo.h b/tool/plinko/lib/histo.h index ddced887c..4cdc63904 100644 --- a/tool/plinko/lib/histo.h +++ b/tool/plinko/lib/histo.h @@ -7,7 +7,7 @@ COSMOPOLITAN_C_START_ #define HISTO(H, X) \ do { \ uint64_t x_ = X; \ - x_ = x_ ? _bsrl(x_) + 1 : x_; \ + x_ = x_ ? bsrl(x_) + 1 : x_; \ ++H[MIN(x_, ARRAYLEN(H) - 1)]; \ } while (0) diff --git a/tool/plinko/lib/printchar.c b/tool/plinko/lib/printchar.c index 67853400c..a396be4de 100644 --- a/tool/plinko/lib/printchar.c +++ b/tool/plinko/lib/printchar.c @@ -31,7 +31,7 @@ int PrintChar(int fd, int s) { if (c == L'\n') Flush(fd); } else { d = c; - e = kTpEnc[_bsrl(d) - 7]; + e = kTpEnc[bsrl(d) - 7]; i = n = e & 255; do g_buffer[fd][bp[fd] + i--] = 0200 | (d & 077); while (d >>= 6, i); diff --git a/tool/viz/fontspace.c b/tool/viz/fontspace.c index 51c3ebff3..875a73648 100644 --- a/tool/viz/fontspace.c +++ b/tool/viz/fontspace.c @@ -51,7 +51,7 @@ void PrintBar(unsigned char *p, size_t n) { } for (i = 0; i < j; ++i) { if (p[i]) { - fputwc(u"░░▒▒▓▓██"[_bsr(p[i])], stdout); + fputwc(u"░░▒▒▓▓██"[bsr(p[i])], stdout); } else { fputc(' ', stdout); } diff --git a/tool/viz/lib/bilinearscale.c b/tool/viz/lib/bilinearscale.c index f377db898..fd58723a9 100644 --- a/tool/viz/lib/bilinearscale.c +++ b/tool/viz/lib/bilinearscale.c @@ -82,8 +82,8 @@ void *BilinearScale(long dcw, long dyw, long dxw, DCHECK_LE(sxn, sxw); DCHECK_LE(dyn, dyw); DCHECK_LE(dxn, dxw); - DCHECK_LT(_bsrl(cn) + _bsrl(syn) + _bsrl(sxn), 32); - DCHECK_LT(_bsrl(cn) + _bsrl(dyn) + _bsrl(dxn), 32); + DCHECK_LT(bsrl(cn) + bsrl(syn) + bsrl(sxn), 32); + DCHECK_LT(bsrl(cn) + bsrl(dyn) + bsrl(dxn), 32); BilinearScaler(dcw, dyw, dxw, dst, scw, syw, sxw, src, c0, cn, dyn, dxn, syn, sxn, ry, rx, oy, ox, gc(xmemalign(64, ROUNDUP(sizeof(int) * (dyn + 1), 64))), diff --git a/tool/viz/lib/ycbcr2rgb3.c b/tool/viz/lib/ycbcr2rgb3.c index 44eb7963d..c038dd4a5 100644 --- a/tool/viz/lib/ycbcr2rgb3.c +++ b/tool/viz/lib/ycbcr2rgb3.c @@ -84,11 +84,11 @@ struct YCbCr { }; static unsigned long roundup2pow(unsigned long x) { - return x > 1 ? 2ul << _bsrl(x - 1) : x ? 1 : 0; + return x > 1 ? 2ul << bsrl(x - 1) : x ? 1 : 0; } static unsigned long rounddown2pow(unsigned long x) { - return x ? 1ul << _bsrl(x) : 0; + return x ? 1ul << bsrl(x) : 0; } /** diff --git a/tool/viz/memzoom.c b/tool/viz/memzoom.c index a0bcf9297..9ff4a2d3b 100644 --- a/tool/viz/memzoom.c +++ b/tool/viz/memzoom.c @@ -190,7 +190,7 @@ static void LeaveScreen(void) { } static unsigned long rounddown2pow(unsigned long x) { - return x ? 1ul << _bsrl(x) : 0; + return x ? 1ul << bsrl(x) : 0; } static void GetTtySize(void) {