cosmopolitan/libc/intrin/bits.h
Justine Tunney 4778cd4d27
Fix bugs in termios library and cleanup code
This change fixes an issue with the tcflow() magic numbers that was
causing bash to freeze up on Linux. While auditing termios polyfills,
several other issues were identified with XNU/BSD compatibility.

Out of an abundance of caution this change undefines as much surface
area from libc/calls/struct/termios.h as possible, so that autoconf
scripts are less likely to detect non-POSIX teletypewriter APIs that
haven't been polyfilled by Cosmopolitan.

This is a *breaking change* for your static archives in /opt/cosmos if
you use the cosmocc toolchain. That's because this change disables the
ioctl() undiamonding trick for code outside the monorepo, specifically
because it'll lead to brittle ABI breakages like this. If you're using
the cosmocc toolchain, you'll need to rebuild libraries like ncurses,
readline, etc. Yes diamonds cause bloat. To work around that, consider
using tcgetwinsize() instead of ioctl(TIOCGWINSZ) since it'll help you
avoid pulling every single ioctl-related polyfill into the linkage.

The cosmocc script was specifying -DNDEBUG for some reason. It's fixed.
2023-06-14 19:30:52 -07:00

115 lines
3.6 KiB
C

#ifndef COSMOPOLITAN_LIBC_BITS_H_
#define COSMOPOLITAN_LIBC_BITS_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#ifdef COSMO
#define CheckUnsigned(x) ((x) / !((typeof(x))(-1) < 0))
extern const uint8_t kReverseBits[256];
int _bitreverse8(int) libcesque pureconst;
int _bitreverse16(int) libcesque pureconst;
uint32_t _bitreverse32(uint32_t)
libcesque pureconst;
uint64_t _bitreverse64(uint64_t)
libcesque pureconst;
unsigned long _roundup2pow(unsigned long) libcesque pureconst;
unsigned long _roundup2log(unsigned long) libcesque pureconst;
unsigned long _rounddown2pow(unsigned long) libcesque pureconst;
unsigned _bextra(const unsigned *, size_t, char);
#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_ */