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

@ -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;
}

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) {
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) {

View file

@ -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;

View file

@ -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;

View file

@ -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));
}

View file

@ -18,12 +18,14 @@
#if 1
template <bool _P, typename _T, typename _U>
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 <typename _T, typename _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;
};
#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))
#endif
#endif
#ifdef __aarch64__
/* todo jart whyyyy */
#define _Float16 __fp16
#endif

View file

@ -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);

View file

@ -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 */

View file

@ -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; }

View file

@ -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 */

View file

@ -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);

View file

@ -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;

View file

@ -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);

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) {
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) {

View file

@ -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);

View file

@ -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 {

View file

@ -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);

View file

@ -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

View file

@ -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) {

View file

@ -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)));

View file

@ -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;

View file

@ -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);
}
/**

View file

@ -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_ */

View file

@ -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) {

View file

@ -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;
}
}