cosmopolitan/libc/intrin/bits.h
Justine Tunney 0a24b4fc3c
Clean up more code
The *NSYNC linked list API is good enough that it deserves to be part of
the C libray, so this change writes an improved version of it which uses
that offsetof() trick from the Linux Kernel. We vendor all of the *NSYNC
tests in third_party which helped confirm the needed refactoring is safe

This change also deletes more old code that didn't pan out. My goal here
is to work towards a vision where the Cosmopolitan core libraries become
less experimental and more focused on curation. This better reflects the
current level of quality we've managed to achieve.
2023-07-06 08:03:24 -07:00

108 lines
3.4 KiB
C

#ifndef COSMOPOLITAN_LIBC_BITS_H_
#define COSMOPOLITAN_LIBC_BITS_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#ifdef COSMO
int _bitreverse8(int) pureconst;
int _bitreverse16(int) pureconst;
uint32_t _bitreverse32(uint32_t) pureconst;
uint64_t _bitreverse64(uint64_t) pureconst;
unsigned long _roundup2pow(unsigned long) pureconst;
unsigned long _roundup2log(unsigned long) pureconst;
unsigned long _rounddown2pow(unsigned long) pureconst;
#define READ16LE(P) \
(__extension__({ \
uint16_t __x; \
__builtin_memcpy(&__x, P, 16 / 8); \
__x; \
}))
#define READ16BE(P) \
(__extension__({ \
uint16_t __x; \
__builtin_memcpy(&__x, P, 16 / 8); \
__builtin_bswap16(__x); \
}))
#define READ32LE(P) \
(__extension__({ \
uint32_t __x; \
__builtin_memcpy(&__x, P, 32 / 8); \
__x; \
}))
#define READ32BE(P) \
(__extension__({ \
uint32_t __x; \
__builtin_memcpy(&__x, P, 32 / 8); \
__builtin_bswap32(__x); \
}))
#define READ64LE(P) \
(__extension__({ \
uint64_t __x; \
__builtin_memcpy(&__x, P, 64 / 8); \
__x; \
}))
#define READ64BE(P) \
(__extension__({ \
uint64_t __x; \
__builtin_memcpy(&__x, P, 64 / 8); \
__builtin_bswap64(__x); \
}))
#define WRITE16LE(P, X) \
(__extension__({ \
__typeof__(&(P)[0]) __p = (P); \
uint16_t __x = (X); \
__builtin_memcpy(__p, &__x, 16 / 8); \
__p + 16 / 8; \
}))
#define WRITE16BE(P, X) \
(__extension__({ \
__typeof__(&(P)[0]) __p = (P); \
uint16_t __x = __builtin_bswap16(X); \
__builtin_memcpy(__p, &__x, 16 / 8); \
__p + 16 / 8; \
}))
#define WRITE32LE(P, X) \
(__extension__({ \
__typeof__(&(P)[0]) __p = (P); \
uint32_t __x = (X); \
__builtin_memcpy(__p, &__x, 32 / 8); \
__p + 32 / 8; \
}))
#define WRITE32BE(P, X) \
(__extension__({ \
__typeof__(&(P)[0]) __p = (P); \
uint32_t __x = __builtin_bswap32(X); \
__builtin_memcpy(__p, &__x, 32 / 8); \
__p + 32 / 8; \
}))
#define WRITE64LE(P, X) \
(__extension__({ \
__typeof__(&(P)[0]) __p = (P); \
uint64_t __x = (X); \
__builtin_memcpy(__p, &__x, 64 / 8); \
__p + 64 / 8; \
}))
#define WRITE64BE(P, X) \
(__extension__({ \
__typeof__(&(P)[0]) __p = (P); \
uint64_t __x = __builtin_bswap64(X); \
__builtin_memcpy(__p, &__x, 64 / 8); \
__p + 64 / 8; \
}))
#endif /* COSMO */
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_BITS_H_ */