Rename _bsr/_bsf to bsr/bsf

Now that these functions are behind _COSMO_SOURCE there's no reason for
having the ugly underscore anymore. To use these functions, you need to
pass -mcosmo to cosmocc.
This commit is contained in:
Justine Tunney 2024-03-04 17:33:26 -08:00
parent a6baba1b07
commit 8bfd56b59e
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
53 changed files with 110 additions and 93 deletions

View file

@ -202,8 +202,8 @@ void *Gyarados(long dyw, long dxw, int dst[dyw][dxw], long syw, long sxw,
CHECK_LE(sxn, sxw); CHECK_LE(sxn, sxw);
CHECK_LE(dyn, dyw); CHECK_LE(dyn, dyw);
CHECK_LE(dxn, dxw); CHECK_LE(dxn, dxw);
CHECK_LT(_bsrl(syn) + _bsrl(sxn), 32); CHECK_LT(bsrl(syn) + bsrl(sxn), 32);
CHECK_LT(_bsrl(dyn) + _bsrl(dxn), 32); CHECK_LT(bsrl(dyn) + bsrl(dxn), 32);
CHECK_LE(dyw, 0x7fff); CHECK_LE(dyw, 0x7fff);
CHECK_LE(dxw, 0x7fff); CHECK_LE(dxw, 0x7fff);
CHECK_LE(syw, 0x7fff); CHECK_LE(syw, 0x7fff);

View file

@ -71,7 +71,7 @@ static textwindows long GetSizeOfReparsePoint(int64_t fh) {
} }
} }
if (x >= 0200) { if (x >= 0200) {
z += _bsrl(tpenc(x)) >> 3; z += bsrl(tpenc(x)) >> 3;
} }
++z; ++z;
} }

View file

@ -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) { 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) { static privileged int GetPid(void) {

View file

@ -97,7 +97,7 @@ static textwindows int __sig_getter(atomic_ulong *sigs, sigset_t masked) {
for (;;) { for (;;) {
pending = atomic_load_explicit(sigs, memory_order_acquire); pending = atomic_load_explicit(sigs, memory_order_acquire);
if ((deliverable = pending & ~masked)) { if ((deliverable = pending & ~masked)) {
sig = _bsfl(deliverable) + 1; sig = bsfl(deliverable) + 1;
bit = 1ull << (sig - 1); bit = 1ull << (sig - 1);
if (atomic_fetch_and_explicit(sigs, ~bit, memory_order_acq_rel) & bit) { if (atomic_fetch_and_explicit(sigs, ~bit, memory_order_acq_rel) & bit) {
return sig; return sig;

View file

@ -45,7 +45,7 @@ char *FormatBinary64(char p[hasatleast 67], uint64_t x, char z) {
*p++ = '0'; *p++ = '0';
*p++ = 'b'; *p++ = 'b';
} }
i = PickGoodWidth(_bsrl(x)); i = PickGoodWidth(bsrl(x));
do { do {
b = 1; b = 1;
b <<= i; b <<= i;

View file

@ -22,5 +22,5 @@
#include "libc/macros.internal.h" #include "libc/macros.internal.h"
size_t uint64toarray_radix16(uint64_t x, char b[hasatleast 17]) { 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));
} }

View file

@ -18,12 +18,14 @@
#if 1 #if 1
template <bool _P, typename _T, typename _U> template <bool _P, typename _T, typename _U>
struct __cxx_choose_expr { 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; const _T _value;
}; };
template <typename _T, typename _U> template <typename _T, typename _U>
struct __cxx_choose_expr<false, _T, _U> { struct __cxx_choose_expr<false, _T, _U> {
__cxx_choose_expr(_T _a, _U _b) : _value(_b) {} __cxx_choose_expr(_T _a, _U _b) : _value(_b) {
}
const _U _value; const _U _value;
}; };
#define __builtin_choose_expr(X, A, B) \ #define __builtin_choose_expr(X, A, B) \
@ -32,3 +34,8 @@ struct __cxx_choose_expr<false, _T, _U> {
#define __builtin_choose_expr(X, A, B) ((X) ? (A) : (B)) #define __builtin_choose_expr(X, A, B) ((X) ? (A) : (B))
#endif #endif
#endif #endif
#ifdef __aarch64__
/* todo jart whyyyy */
#define _Float16 __fp16
#endif

View file

@ -22,7 +22,7 @@
* Returns position of first bit set. * Returns position of first bit set.
* *
* ctz(𝑥) 31^clz(𝑥) clz(𝑥) * ctz(𝑥) 31^clz(𝑥) clz(𝑥)
* uint32 𝑥 _bsf(𝑥) tzcnt(𝑥) ffs(𝑥) _bsr(𝑥) lzcnt(𝑥) * uint32 𝑥 bsf(𝑥) tzcnt(𝑥) ffs(𝑥) bsr(𝑥) lzcnt(𝑥)
* 0x00000000 wut 32 0 wut 32 * 0x00000000 wut 32 0 wut 32
* 0x00000001 0 0 1 0 31 * 0x00000001 0 0 1 0 31
* 0x80000001 0 0 1 31 0 * 0x80000001 0 0 1 31 0
@ -35,7 +35,7 @@
* @param 𝑥 is a 64-bit integer * @param 𝑥 is a 64-bit integer
* @return number in range 0..63 or undefined if 𝑥 is 0 * @return number in range 0..63 or undefined if 𝑥 is 0
*/ */
int(_bsfl)(long x) { int(bsfl)(long x) {
unsigned l, r; unsigned l, r;
x &= -x; x &= -x;
l = x | x >> 32; l = x | x >> 32;
@ -52,7 +52,7 @@ int(_bsfl)(long x) {
* Returns position of first bit set. * Returns position of first bit set.
* *
* ctz(𝑥) 31^clz(𝑥) clz(𝑥) * ctz(𝑥) 31^clz(𝑥) clz(𝑥)
* uint32 𝑥 _bsf(𝑥) tzcnt(𝑥) ffs(𝑥) _bsr(𝑥) lzcnt(𝑥) * uint32 𝑥 bsf(𝑥) tzcnt(𝑥) ffs(𝑥) bsr(𝑥) lzcnt(𝑥)
* 0x00000000 wut 32 0 wut 32 * 0x00000000 wut 32 0 wut 32
* 0x00000001 0 0 1 0 31 * 0x00000001 0 0 1 0 31
* 0x80000001 0 0 1 31 0 * 0x80000001 0 0 1 31 0
@ -65,8 +65,8 @@ int(_bsfl)(long x) {
* @param x is a 32-bit integer * @param x is a 32-bit integer
* @return number in range 0..31 or undefined if 𝑥 is 0 * @return number in range 0..31 or undefined if 𝑥 is 0
*/ */
int(_bsf)(int x) { int(bsf)(int x) {
return _bsf((unsigned)x); return bsf((unsigned)x);
} }
__weak_reference(_bsfl, _bsfll); __weak_reference(bsfl, bsfll);

View file

@ -1,16 +1,18 @@
#ifdef _COSMO_SOURCE
#ifndef COSMOPOLITAN_LIBC_NEXGEN32E_BSF_H_ #ifndef COSMOPOLITAN_LIBC_NEXGEN32E_BSF_H_
#define COSMOPOLITAN_LIBC_NEXGEN32E_BSF_H_ #define COSMOPOLITAN_LIBC_NEXGEN32E_BSF_H_
COSMOPOLITAN_C_START_ COSMOPOLITAN_C_START_
libcesque int _bsf(int) pureconst; libcesque int bsf(int) pureconst;
libcesque int _bsfl(long) pureconst; libcesque int bsfl(long) pureconst;
libcesque int _bsfll(long long) pureconst; libcesque int bsfll(long long) pureconst;
#if defined(__GNUC__) && !defined(__STRICT_ANSI__) #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#define _bsf(x) __builtin_ctz(x) #define bsf(x) __builtin_ctz(x)
#define _bsfl(x) __builtin_ctzl(x) #define bsfl(x) __builtin_ctzl(x)
#define _bsfll(x) __builtin_ctzll(x) #define bsfll(x) __builtin_ctzll(x)
#endif #endif
COSMOPOLITAN_C_END_ COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_LIBC_NEXGEN32E_BSF_H_ */ #endif /* COSMOPOLITAN_LIBC_NEXGEN32E_BSF_H_ */
#endif /* _COSMO_SOURCE */

View file

@ -23,7 +23,7 @@
* Returns binary logarithm of 𝑥. * Returns binary logarithm of 𝑥.
* *
* ctz(𝑥) 31^clz(𝑥) clz(𝑥) * ctz(𝑥) 31^clz(𝑥) clz(𝑥)
* uint32 𝑥 _bsf(𝑥) tzcnt(𝑥) ffs(𝑥) _bsr(𝑥) lzcnt(𝑥) * uint32 𝑥 bsf(𝑥) tzcnt(𝑥) ffs(𝑥) bsr(𝑥) lzcnt(𝑥)
* 0x00000000 wut 32 0 wut 32 * 0x00000000 wut 32 0 wut 32
* 0x00000001 0 0 1 0 31 * 0x00000001 0 0 1 0 31
* 0x80000001 0 0 1 31 0 * 0x80000001 0 0 1 31 0
@ -36,7 +36,7 @@
* @param x is a 32-bit integer * @param x is a 32-bit integer
* @return number in range 0..31 or undefined if 𝑥 is 0 * @return number in range 0..31 or undefined if 𝑥 is 0
*/ */
int(_bsr)(int x) { int(bsr)(int x) {
int r = 0; int r = 0;
if(x & 0xFFFF0000u) { x >>= 16; r |= 16; } if(x & 0xFFFF0000u) { x >>= 16; r |= 16; }
if(x & 0xFF00) { x >>= 8; r |= 8; } if(x & 0xFF00) { x >>= 8; r |= 8; }

View file

@ -1,16 +1,18 @@
#ifdef _COSMO_SOURCE
#ifndef COSMOPOLITAN_LIBC_NEXGEN32E_BSR_H_ #ifndef COSMOPOLITAN_LIBC_NEXGEN32E_BSR_H_
#define COSMOPOLITAN_LIBC_NEXGEN32E_BSR_H_ #define COSMOPOLITAN_LIBC_NEXGEN32E_BSR_H_
COSMOPOLITAN_C_START_ COSMOPOLITAN_C_START_
libcesque int _bsr(int) pureconst; libcesque int bsr(int) pureconst;
libcesque int _bsrl(long) pureconst; libcesque int bsrl(long) pureconst;
libcesque int _bsrll(long long) pureconst; libcesque int bsrll(long long) pureconst;
#if defined(__GNUC__) && !defined(__STRICT_ANSI__) #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#define _bsr(x) (__builtin_clz(x) ^ (sizeof(int) * 8 - 1)) #define bsr(x) (__builtin_clz(x) ^ (sizeof(int) * 8 - 1))
#define _bsrl(x) (__builtin_clzl(x) ^ (sizeof(long) * 8 - 1)) #define bsrl(x) (__builtin_clzl(x) ^ (sizeof(long) * 8 - 1))
#define _bsrll(x) (__builtin_clzll(x) ^ (sizeof(long long) * 8 - 1)) #define bsrll(x) (__builtin_clzll(x) ^ (sizeof(long long) * 8 - 1))
#endif #endif
COSMOPOLITAN_C_END_ COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_LIBC_NEXGEN32E_BSR_H_ */ #endif /* COSMOPOLITAN_LIBC_NEXGEN32E_BSR_H_ */
#endif /* _COSMO_SOURCE */

View file

@ -29,7 +29,7 @@ static const char kDebruijn[64] = {
* Returns binary logarithm of 𝑥. * Returns binary logarithm of 𝑥.
* *
* ctz(𝑥) 31^clz(𝑥) clz(𝑥) * ctz(𝑥) 31^clz(𝑥) clz(𝑥)
* uint32 𝑥 _bsf(𝑥) tzcnt(𝑥) ffs(𝑥) _bsr(𝑥) lzcnt(𝑥) * uint32 𝑥 bsf(𝑥) tzcnt(𝑥) ffs(𝑥) bsr(𝑥) lzcnt(𝑥)
* 0x00000000 wut 32 0 wut 32 * 0x00000000 wut 32 0 wut 32
* 0x00000001 0 0 1 0 31 * 0x00000001 0 0 1 0 31
* 0x80000001 0 0 1 31 0 * 0x80000001 0 0 1 31 0
@ -42,7 +42,7 @@ static const char kDebruijn[64] = {
* @param x is a 64-bit integer * @param x is a 64-bit integer
* @return number in range 0..63 or undefined if 𝑥 is 0 * @return number in range 0..63 or undefined if 𝑥 is 0
*/ */
int(_bsrl)(long x) { int(bsrl)(long x) {
x |= x >> 1; x |= x >> 1;
x |= x >> 2; x |= x >> 2;
x |= x >> 4; x |= x >> 4;
@ -52,4 +52,4 @@ int(_bsrl)(long x) {
return kDebruijn[(x * 0x03f79d71b4cb0a89ull) >> 58]; return kDebruijn[(x * 0x03f79d71b4cb0a89ull) >> 58];
} }
__weak_reference(_bsrl, _bsrll); __weak_reference(bsrl, bsrll);

View file

@ -58,7 +58,7 @@ int __cxa_atexit(void *fp, void *arg, void *pred) {
return enomem(); return enomem();
} }
} }
i = _bsr(~b->mask); i = bsr(~b->mask);
b->mask |= 1u << i; b->mask |= 1u << i;
b->p[i].fp = fp; b->p[i].fp = fp;
b->p[i].arg = arg; b->p[i].arg = arg;

View file

@ -43,7 +43,7 @@ StartOverLocked:
for (;;) { for (;;) {
mask = b->mask; mask = b->mask;
while (mask) { while (mask) {
i = _bsf(mask); i = bsf(mask);
mask &= ~(1u << i); mask &= ~(1u << i);
if (!pred || pred == b->p[i].pred) { if (!pred || pred == b->p[i].pred) {
b->mask &= ~(1u << i); b->mask &= ~(1u << i);

View file

@ -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) { for (int fd = 0; fd < nfds; fd += 64) {
uint64_t w = fds->fds_bits[fd >> 6]; uint64_t w = fds->fds_bits[fd >> 6];
while (w) { while (w) {
unsigned m = _bsr(w); unsigned m = bsr(w);
w &= ~((uint64_t)1 << m); w &= ~((uint64_t)1 << m);
if (fd + m < nfds) { if (fd + m < nfds) {
if (!gotsome) { if (!gotsome) {

View file

@ -48,7 +48,7 @@ char *FormatHex64(char p[hasatleast 19], uint64_t x, char z) {
*p++ = '0'; *p++ = '0';
*p++ = 'x'; *p++ = 'x';
} }
i = PickGoodWidth(_bsrl(x), z); i = PickGoodWidth(bsrl(x), z);
do { do {
*p++ = "0123456789abcdef"[(x >> (i -= 4)) & 15]; *p++ = "0123456789abcdef"[(x >> (i -= 4)) & 15];
} while (i); } while (i);

View file

@ -34,7 +34,7 @@ uint64_t tpenc(uint32_t c) {
int e, n; int e, n;
uint64_t w; uint64_t w;
if (0 <= c && c <= 127) return c; if (0 <= c && c <= 127) return c;
e = kTpEnc[_bsr(c) - 7]; e = kTpEnc[bsr(c) - 7];
n = e & 0xff; n = e & 0xff;
w = 0; w = 0;
do { do {

View file

@ -41,7 +41,7 @@ void __cxa_printexits(FILE *f, void *pred) {
do { do {
mask = b->mask; mask = b->mask;
while (mask) { while (mask) {
i = _bsf(mask); i = bsf(mask);
mask &= ~(1u << i); mask &= ~(1u << i);
if (!pred || pred == b->p[i].pred) { if (!pred || pred == b->p[i].pred) {
symbol = GetSymbolByAddr((intptr_t)b->p[i].fp); symbol = GetSymbolByAddr((intptr_t)b->p[i].fp);

View file

@ -587,7 +587,7 @@ errno_t posix_spawn(int *pid, const char *path,
if (flags & POSIX_SPAWN_SETRLIMIT) { if (flags & POSIX_SPAWN_SETRLIMIT) {
int rlimset = (*attrp)->rlimset; int rlimset = (*attrp)->rlimset;
while (rlimset) { while (rlimset) {
int resource = _bsf(rlimset); int resource = bsf(rlimset);
rlimset &= ~(1u << resource); rlimset &= ~(1u << resource);
if (setrlimit(resource, (*attrp)->rlim + resource)) { if (setrlimit(resource, (*attrp)->rlim + resource)) {
// MacOS ARM64 RLIMIT_STACK always returns EINVAL // MacOS ARM64 RLIMIT_STACK always returns EINVAL

View file

@ -73,7 +73,7 @@
#define FRAME(x) ((int)((intptr_t)(x) >> 16)) #define FRAME(x) ((int)((intptr_t)(x) >> 16))
static inline pureconst unsigned long __rounddown2pow(unsigned long x) { 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) { static wontreturn void __mmap_die(const char *s) {

View file

@ -55,7 +55,7 @@ ssize_t appendr(char **b, size_t i) {
z = appendz((p = *b)); z = appendz((p = *b));
if (i != z.i || !p) { if (i != z.i || !p) {
n = ROUNDUP(i + 1, 8) + W; 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))) { if ((p = realloc(p, n))) {
z.n = malloc_usable_size(p); z.n = malloc_usable_size(p);
unassert(!(z.n & (W - 1))); unassert(!(z.n & (W - 1)));

View file

@ -18,10 +18,10 @@
*/ */
#include "libc/assert.h" #include "libc/assert.h"
#include "libc/dce.h" #include "libc/dce.h"
#include "libc/serialize.h"
#include "libc/intrin/bsr.h" #include "libc/intrin/bsr.h"
#include "libc/macros.internal.h" #include "libc/macros.internal.h"
#include "libc/mem/mem.h" #include "libc/mem/mem.h"
#include "libc/serialize.h"
#include "libc/stdio/append.h" #include "libc/stdio/append.h"
#define W sizeof(size_t) #define W sizeof(size_t)
@ -59,7 +59,7 @@ ssize_t appendw(char **b, uint64_t w) {
char *p, *q; char *p, *q;
struct appendz z; struct appendz z;
z = appendz((p = *b)); 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; n = ROUNDUP(z.i + 8 + 1, 8) + W;
if (n > z.n) { if (n > z.n) {
if (!z.n) z.n = W * 2; if (!z.n) z.n = W * 2;

View file

@ -359,14 +359,14 @@ static int __fmt_stoa_wide(out_f out, void *a, uint64_t w) {
char buf[8]; char buf[8];
if (!isascii(w)) w = tpenc(w); if (!isascii(w)) w = tpenc(w);
WRITE64LE(buf, 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) { static int __fmt_stoa_bing(out_f out, void *a, uint64_t w) {
char buf[8]; char buf[8];
w = tpenc(kCp437[w & 0xFF]); w = tpenc(kCp437[w & 0xFF]);
WRITE64LE(buf, 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_quoted(out_f out, void *a, uint64_t w) { 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); w = tpenc(w);
} }
WRITE64LE(buf, w); WRITE64LE(buf, w);
return out(buf, a, w ? (_bsr(w) >> 3) + 1 : 1); return out(buf, a, w ? (bsr(w) >> 3) + 1 : 1);
} }
/** /**

View file

@ -5,7 +5,7 @@
#define ThomPikeCont(x) (0200 == (0300 & (x))) #define ThomPikeCont(x) (0200 == (0300 & (x)))
#define ThomPikeByte(x) ((x) & (((1 << ThomPikeMsb(x)) - 1) | 3)) #define ThomPikeByte(x) ((x) & (((1 << ThomPikeMsb(x)) - 1) | 3))
#define ThomPikeLen(x) (7 - ThomPikeMsb(x)) #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))) #define ThomPikeMerge(x, y) ((x) << 6 | (077 & (y)))
#endif /* COSMOPOLITAN_LIBC_STR_THOMPIKE_H_ */ #endif /* COSMOPOLITAN_LIBC_STR_THOMPIKE_H_ */

View file

@ -18,7 +18,7 @@ forceinline int tpdecodecb(wint_t *out, int first,
if ((wc = get(arg, i++)) == -1) return -1; if ((wc = get(arg, i++)) == -1) return -1;
} }
if (__builtin_expect(!(0 <= wc && wc <= 0x7F), 0)) { 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; need = 7 - msb;
wc &= ((1u << msb) - 1) | 0b00000011; wc &= ((1u << msb) - 1) | 0b00000011;
for (j = 1; j < need; ++j) { for (j = 1; j < need; ++j) {

View file

@ -16,7 +16,6 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/serialize.h"
#include "libc/intrin/bsr.h" #include "libc/intrin/bsr.h"
#include "libc/intrin/packsswb.h" #include "libc/intrin/packsswb.h"
#include "libc/intrin/pandn.h" #include "libc/intrin/pandn.h"
@ -26,6 +25,7 @@
#include "libc/intrin/punpckhbw.h" #include "libc/intrin/punpckhbw.h"
#include "libc/intrin/punpcklbw.h" #include "libc/intrin/punpcklbw.h"
#include "libc/mem/mem.h" #include "libc/mem/mem.h"
#include "libc/serialize.h"
#include "libc/str/str.h" #include "libc/str/str.h"
#include "libc/str/thompike.h" #include "libc/str/thompike.h"
#include "libc/str/utf16.h" #include "libc/str/utf16.h"
@ -78,7 +78,7 @@ char *utf16to8(const char16_t *p, size_t n, size_t *z) {
} else { } else {
w = tpenc(x); w = tpenc(x);
WRITE64LE(q, w); WRITE64LE(q, w);
q += _bsr(w) >> 3; q += bsr(w) >> 3;
q += 1; q += 1;
} }
} }

View file

@ -36,7 +36,7 @@ uint32_t FingerSyn(const char *p, size_t n) {
// ttl<=128 Windows, OpenVMS 8+ // ttl<=128 Windows, OpenVMS 8+
// ttl<=64 Mac, Linux, BSD, Solaris 8+, Tru64, HP-UX // ttl<=64 Mac, Linux, BSD, Solaris 8+, Tru64, HP-UX
ttl = p[8] & 255; ttl = p[8] & 255;
h += _bsr(MAX(1, ttl - 1)); h += bsr(MAX(1, ttl - 1));
h *= 0x9e3779b1; h *= 0x9e3779b1;
// TCP Options // TCP Options
// We care about the order and presence of leading common options. // We care about the order and presence of leading common options.

View file

@ -21,7 +21,7 @@
const char *GetOsName(int os) { const char *GetOsName(int os) {
if (os) { if (os) {
switch (_bsr(os)) { switch (bsr(os)) {
case 0: case 0:
return "LINUX"; return "LINUX";
case 1: case 1:

View file

@ -1538,7 +1538,7 @@ bool GeneratePlot(struct Asset *out, long block, long cash) {
CHECK_SQL(sqlite3_exec(db, "BEGIN TRANSACTION", 0, 0, 0)); CHECK_SQL(sqlite3_exec(db, "BEGIN TRANSACTION", 0, 0, 0));
area = XN * YN; area = XN * YN;
mask = area - 1; mask = area - 1;
clump = 32 - _bsr(area) - 8; clump = 32 - bsr(area) - 8;
while ((rc = DbStep(stmt)) != SQLITE_DONE) { while ((rc = DbStep(stmt)) != SQLITE_DONE) {
if (rc != SQLITE_ROW) CHECK_DB(rc); if (rc != SQLITE_ROW) CHECK_DB(rc);
ip = sqlite3_column_int64(stmt, 0); ip = sqlite3_column_int64(stmt, 0);

View file

@ -20,7 +20,7 @@
#include "libc/str/str.h" #include "libc/str/str.h"
#include "libc/testlib/testlib.h" #include "libc/testlib/testlib.h"
static unsigned _Bsr(unsigned x) { static unsigned Bsr(unsigned x) {
static const char kDebruijn[32] = { static const char kDebruijn[32] = {
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 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, 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]; return kDebruijn[x];
} }
TEST(_bsr, test) { TEST(bsr, test) {
for (int i = 1; i < 1000; ++i) { for (int i = 1; i < 1000; ++i) {
ASSERT_EQ(_bsr(i), _Bsr(i)); ASSERT_EQ(bsr(i), Bsr(i));
} }
} }

View file

@ -2028,7 +2028,7 @@ static int ParseModrm(struct As *a, int *disp) {
if (((reg & 070) >> 3) == 2) modrm |= HASASZ; // asz if (((reg & 070) >> 3) == 2) modrm |= HASASZ; // asz
if (IsComma(a)) { if (IsComma(a)) {
++a->i; ++a->i;
modrm |= (_bsr(GetInt(a)) & 3) << 6; modrm |= (bsr(GetInt(a)) & 3) << 6;
} }
} }
ConsumePunct(a, ')'); ConsumePunct(a, ')');

View file

@ -301,7 +301,7 @@ static void PickAsmRegisters(Asm *a) {
if (!(m = a->ops[i].regmask)) break; if (!(m = a->ops[i].regmask)) break;
if (popcnt(m) != j) break; if (popcnt(m) != j) break;
if (!(m &= regset)) CouldNotAllocateRegister(&a->ops[i], "rm"); 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; if (pick & PRECIOUS) a->regclob |= pick;
regset &= ~pick; regset &= ~pick;
a->ops[i].regmask = 0; a->ops[i].regmask = 0;
@ -309,14 +309,14 @@ static void PickAsmRegisters(Asm *a) {
case kAsmXmm: case kAsmXmm:
if (!(m = a->ops[i].regmask)) break; if (!(m = a->ops[i].regmask)) break;
if (!(m &= xmmset)) CouldNotAllocateRegister(&a->ops[i], "xmm"); 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; a->ops[i].regmask = 0;
break; break;
case kAsmFpu: case kAsmFpu:
if (!(m = a->ops[i].x87mask)) break; if (!(m = a->ops[i].x87mask)) break;
if (popcnt(m) != j) break; if (popcnt(m) != j) break;
if (!(m &= x87sts)) CouldNotAllocateRegister(&a->ops[i], "fpu"); 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; a->ops[i].x87mask = 0;
break; break;
default: default:
@ -540,7 +540,7 @@ static char *HandleAsmSpecifier(Asm *a, char *p) {
if ((i = c - '0') >= a->n) { if ((i = c - '0') >= a->n) {
error_tok(a->tok, "bad asm reference at offset %d", p - a->str); 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) { if (z > 3 && a->ops[i].type == kAsmReg) {
error_tok(a->tok, "bad asm op size"); 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); println("\tset%s\t(%%rax)", a->ops[i].str + a->ops[i].predicate);
break; break;
case kAsmReg: case kAsmReg:
z = _bsr(a->ops[i].node->ty->size); z = bsr(a->ops[i].node->ty->size);
if (a->ops[i].reg) { if (a->ops[i].reg) {
gen_addr(a->ops[i].node); gen_addr(a->ops[i].node);
if (z > 3) error_tok(a->tok, "bad asm out size"); 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) { static void PushClobbers(Asm *a) {
int i, regs = a->regclob & PRECIOUS; int i, regs = a->regclob & PRECIOUS;
while (regs) { while (regs) {
i = _bsf(regs); i = bsf(regs);
pushreg(kGreg[3][i]); pushreg(kGreg[3][i]);
regs &= ~(1 << i); regs &= ~(1 << i);
} }
@ -743,7 +743,7 @@ static void PushClobbers(Asm *a) {
static void PopClobbers(Asm *a) { static void PopClobbers(Asm *a) {
int i, regs = a->regclob & PRECIOUS; int i, regs = a->regclob & PRECIOUS;
while (regs) { while (regs) {
i = _bsr(regs); i = bsr(regs);
popreg(kGreg[3][i]); popreg(kGreg[3][i]);
regs &= ~(1 << i); regs &= ~(1 << i);
} }

View file

@ -281,7 +281,7 @@ static int CompareDoxIndexEntry(const void *p1, const void *p2, void *arg) {
} }
static unsigned long roundup2pow(unsigned long x) { 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) { static void IndexDox(struct Dox *dox) {

View file

@ -67,7 +67,7 @@ void canonicalize_newline(char *p) {
p += 16; p += 16;
q += 16; q += 16;
} else { } else {
m = _bsf(m); m = bsf(m);
memmove(q, p, m); memmove(q, p, m);
p += m; p += m;
q += m; q += m;
@ -126,7 +126,7 @@ void remove_backslash_newline(char *p) {
i += 16; i += 16;
j += 16; j += 16;
} else { } else {
m = _bsf(m); m = bsf(m);
memmove(p + j, p + i, m); memmove(p + j, p + i, m);
i += m; i += m;
j += m; j += m;

View file

@ -9,7 +9,7 @@ implicit_functions_are_long() {
} }
external_functions_are_long() { external_functions_are_long() {
if (_bsrl(0x0001000000000000) != 48) { if (bsrl(0x0001000000000000) != 48) {
__builtin_trap(); __builtin_trap();
} }
} }

View file

@ -680,7 +680,7 @@ static void convert_universal_chars(char *p) {
p += 16; p += 16;
q += 16; q += 16;
} else { } else {
m = _bsf(m); m = bsf(m);
memmove(q, p, m); memmove(q, p, m);
p += m; p += m;
q += m; q += m;

View file

@ -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 */ if (alignment < MIN_CHUNK_SIZE) /* must be at least a minimum chunk size */
alignment = MIN_CHUNK_SIZE; alignment = MIN_CHUNK_SIZE;
/* alignment is 32+ bytes rounded up to nearest two power */ /* 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 (bytes >= MAX_REQUEST - alignment) {
if (m != 0) { /* Test isn't needed but avoids compiler warning */ if (m != 0) { /* Test isn't needed but avoids compiler warning */
MALLOC_FAILURE_ACTION; MALLOC_FAILURE_ACTION;

View file

@ -369,7 +369,7 @@ static wint_t Capitalize(wint_t c) {
static struct rune DecodeUtf8(int c) { static struct rune DecodeUtf8(int c) {
struct rune r; struct rune r;
if (c < 252) { if (c < 252) {
r.n = _bsr(255 & ~c); r.n = bsr(255 & ~c);
r.c = c & (((1 << r.n) - 1) | 3); r.c = c & (((1 << r.n) - 1) | 3);
r.n = 6 - r.n; r.n = 6 - r.n;
} else { } else {
@ -645,7 +645,7 @@ static void abAppendw(struct abuf *a, unsigned long long w) {
p[5] = (0x0000ff0000000000 & w) >> 050; p[5] = (0x0000ff0000000000 & w) >> 050;
p[6] = (0x00ff000000000000 & w) >> 060; p[6] = (0x00ff000000000000 & w) >> 060;
p[7] = (0xff00000000000000 & w) >> 070; p[7] = (0xff00000000000000 & w) >> 070;
a->len += w ? (_bsrll(w) >> 3) + 1 : 1; a->len += w ? (bsrll(w) >> 3) + 1 : 1;
p[8] = 0; p[8] = 0;
} }
@ -1634,7 +1634,7 @@ static size_t linenoiseEscape(char *d, const char *s, size_t n) {
break; break;
} }
WRITE32LE(p, w); WRITE32LE(p, w);
p += (_bsr(w) >> 3) + 1; p += (bsr(w) >> 3) + 1;
l = w; l = w;
} }
return p - d; return p - d;

View file

@ -785,7 +785,7 @@ LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t srclen);
** Computes ceil(log2(x)) ** Computes ceil(log2(x))
*/ */
static inline int luaO_ceillog2 (unsigned int x) { static inline int luaO_ceillog2 (unsigned int x) {
return --x ? _bsr(x) + 1 : 0; return --x ? bsr(x) + 1 : 0;
} }
#endif #endif

View file

@ -31,7 +31,7 @@ fancy_roundup(int x)
/* Round up to the closest power of 2 >= n. */ /* Round up to the closest power of 2 >= n. */
int r; int r;
assert(x > 128); 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; return r > 0 ? r : -1;
} }

View file

@ -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) { 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: case 0b000:
return *p; return *p;
case 0b100: case 0b100:

View file

@ -32,7 +32,7 @@ static int tpdecode(const char *s, wint_t *out) {
if ((wc = s[i++] & 255) == -1) return -1; if ((wc = s[i++] & 255) == -1) return -1;
} }
if (!(0 <= wc && wc <= 0x7F)) { if (!(0 <= wc && wc <= 0x7F)) {
msb = wc < 252 ? _bsr(~wc & 0xff) : 1; msb = wc < 252 ? bsr(~wc & 0xff) : 1;
need = 7 - msb; need = 7 - msb;
wc &= ((1u << msb) - 1) | 0003; wc &= ((1u << msb) - 1) | 0003;
for (j = 1; j < need; ++j) { for (j = 1; j < need; ++j) {

View file

@ -190,6 +190,9 @@
"__integer_pack" "__integer_pack"
"__is_same")) "__is_same"))
(cuda-builtin-types
'("__syncthreads"))
(cosmopolitan-builtin-defines (cosmopolitan-builtin-defines
'("COSMOPOLITAN_CXX_START_" '("COSMOPOLITAN_CXX_START_"
"COSMOPOLITAN_CXX_END_" "COSMOPOLITAN_CXX_END_"
@ -225,6 +228,7 @@
gcc-builtin-functions-sync gcc-builtin-functions-sync
gcc-builtin-functions-ia32 gcc-builtin-functions-ia32
gxx-builtin-type-traits gxx-builtin-type-traits
cuda-builtin-types
cosmopolitan-builtin-defines cosmopolitan-builtin-defines
cosmopolitan-builtin-functions)) cosmopolitan-builtin-functions))
"\\_>"))) "\\_>")))

View file

@ -74,11 +74,13 @@
"_OPENMP")) "_OPENMP"))
(cuda (cuda
'("__NVCC__")) '("__NVCC__"
"__CUDA_ARCH__"))
(hip (hip
'("__HIP__" '("__HIP__"
"__HIPCC__" "__HIPCC__"
"__HIP_PLATFORM_AMD__"
"__HIP_DEVICE_COMPILE__" "__HIP_DEVICE_COMPILE__"
"__AMD__" "__AMD__"
"__AMDGCN__" "__AMDGCN__"

View file

@ -342,7 +342,7 @@ int LuaPopcnt(lua_State *L) {
int LuaBsr(lua_State *L) { int LuaBsr(lua_State *L) {
long x; long x;
if ((x = luaL_checkinteger(L, 1))) { if ((x = luaL_checkinteger(L, 1))) {
lua_pushinteger(L, _bsrl(x)); lua_pushinteger(L, bsrl(x));
return 1; return 1;
} else { } else {
luaL_argerror(L, 1, "zero"); luaL_argerror(L, 1, "zero");
@ -353,7 +353,7 @@ int LuaBsr(lua_State *L) {
int LuaBsf(lua_State *L) { int LuaBsf(lua_State *L) {
long x; long x;
if ((x = luaL_checkinteger(L, 1))) { if ((x = luaL_checkinteger(L, 1))) {
lua_pushinteger(L, _bsfl(x)); lua_pushinteger(L, bsfl(x));
return 1; return 1;
} else { } else {
luaL_argerror(L, 1, "zero"); luaL_argerror(L, 1, "zero");

View file

@ -2147,7 +2147,7 @@ static void FreeStrings(struct Strings *l) {
} }
static unsigned long roundup2pow(unsigned long x) { 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) { static void IndexAssets(void) {
@ -2943,7 +2943,7 @@ static int GetDecimalWidth(long x) {
} }
static int GetOctalWidth(int 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) { static const char *DescribeCompressionRatio(char rb[8], uint8_t *zcf) {

View file

@ -93,7 +93,7 @@ int Census(struct Gc *G) {
if (!~G->M[j]) { if (!~G->M[j]) {
l += DWBITS; l += DWBITS;
} else { } else {
l += _bsfl(~G->M[j]); l += bsfl(~G->M[j]);
break; break;
} }
} }
@ -124,7 +124,7 @@ void Sweep(struct Gc *G) {
for (; i < G->n; ++i) { for (; i < G->n; ++i) {
m = G->M[i]; m = G->M[i];
if (~m) { if (~m) {
j = _bsfl(~m); j = bsfl(~m);
m >>= j; m >>= j;
m <<= j; m <<= j;
d -= j; d -= j;
@ -136,7 +136,7 @@ void Sweep(struct Gc *G) {
} }
for (; i < G->n; b -= DWBITS, m = G->M[++i]) { for (; i < G->n; b -= DWBITS, m = G->M[++i]) {
for (; m; m &= ~((dword)1 << j)) { 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))))); Set(--d, MAKE(Relocate(G, LO(Get(a))), Relocate(G, HI(Get(a)))));
SetShadow(d, MAKE(LO(GetShadow(a)), Relocate(G, HI(GetShadow(a))))); SetShadow(d, MAKE(LO(GetShadow(a)), Relocate(G, HI(GetShadow(a)))));
} }

View file

@ -7,7 +7,7 @@ COSMOPOLITAN_C_START_
#define HISTO(H, X) \ #define HISTO(H, X) \
do { \ do { \
uint64_t x_ = X; \ uint64_t x_ = X; \
x_ = x_ ? _bsrl(x_) + 1 : x_; \ x_ = x_ ? bsrl(x_) + 1 : x_; \
++H[MIN(x_, ARRAYLEN(H) - 1)]; \ ++H[MIN(x_, ARRAYLEN(H) - 1)]; \
} while (0) } while (0)

View file

@ -31,7 +31,7 @@ int PrintChar(int fd, int s) {
if (c == L'\n') Flush(fd); if (c == L'\n') Flush(fd);
} else { } else {
d = c; d = c;
e = kTpEnc[_bsrl(d) - 7]; e = kTpEnc[bsrl(d) - 7];
i = n = e & 255; i = n = e & 255;
do g_buffer[fd][bp[fd] + i--] = 0200 | (d & 077); do g_buffer[fd][bp[fd] + i--] = 0200 | (d & 077);
while (d >>= 6, i); while (d >>= 6, i);

View file

@ -51,7 +51,7 @@ void PrintBar(unsigned char *p, size_t n) {
} }
for (i = 0; i < j; ++i) { for (i = 0; i < j; ++i) {
if (p[i]) { if (p[i]) {
fputwc(u"░░▒▒▓▓██"[_bsr(p[i])], stdout); fputwc(u"░░▒▒▓▓██"[bsr(p[i])], stdout);
} else { } else {
fputc(' ', stdout); fputc(' ', stdout);
} }

View file

@ -82,8 +82,8 @@ void *BilinearScale(long dcw, long dyw, long dxw,
DCHECK_LE(sxn, sxw); DCHECK_LE(sxn, sxw);
DCHECK_LE(dyn, dyw); DCHECK_LE(dyn, dyw);
DCHECK_LE(dxn, dxw); DCHECK_LE(dxn, dxw);
DCHECK_LT(_bsrl(cn) + _bsrl(syn) + _bsrl(sxn), 32); DCHECK_LT(bsrl(cn) + bsrl(syn) + bsrl(sxn), 32);
DCHECK_LT(_bsrl(cn) + _bsrl(dyn) + _bsrl(dxn), 32); DCHECK_LT(bsrl(cn) + bsrl(dyn) + bsrl(dxn), 32);
BilinearScaler(dcw, dyw, dxw, dst, scw, syw, sxw, src, c0, cn, dyn, dxn, BilinearScaler(dcw, dyw, dxw, dst, scw, syw, sxw, src, c0, cn, dyn, dxn,
syn, sxn, ry, rx, oy, ox, syn, sxn, ry, rx, oy, ox,
gc(xmemalign(64, ROUNDUP(sizeof(int) * (dyn + 1), 64))), gc(xmemalign(64, ROUNDUP(sizeof(int) * (dyn + 1), 64))),

View file

@ -84,11 +84,11 @@ struct YCbCr {
}; };
static unsigned long roundup2pow(unsigned long x) { 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) { static unsigned long rounddown2pow(unsigned long x) {
return x ? 1ul << _bsrl(x) : 0; return x ? 1ul << bsrl(x) : 0;
} }
/** /**

View file

@ -190,7 +190,7 @@ static void LeaveScreen(void) {
} }
static unsigned long rounddown2pow(unsigned long x) { static unsigned long rounddown2pow(unsigned long x) {
return x ? 1ul << _bsrl(x) : 0; return x ? 1ul << bsrl(x) : 0;
} }
static void GetTtySize(void) { static void GetTtySize(void) {