mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 19:43:32 +00:00
f4f4caab0e
I wanted a tiny scriptable meltdown proof way to run userspace programs and visualize how program execution impacts memory. It helps to explain how things like Actually Portable Executable works. It can show you how the GCC generated code is going about manipulating matrices and more. I didn't feel fully comfortable with Qemu and Bochs because I'm not smart enough to understand them. I wanted something like gVisor but with much stronger levels of assurances. I wanted a single binary that'll run, on all major operating systems with an embedded GPL barrier ZIP filesystem that is tiny enough to transpile to JavaScript and run in browsers too. https://justine.storage.googleapis.com/emulator625.mp4
48 lines
2.4 KiB
C
48 lines
2.4 KiB
C
#ifndef COSMOPOLITAN_LIBC_BITS_BSWAP_H_
|
|
#define COSMOPOLITAN_LIBC_BITS_BSWAP_H_
|
|
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
|
COSMOPOLITAN_C_START_
|
|
|
|
uint16_t bswap_16(uint16_t) pureconst;
|
|
uint32_t bswap_32(uint32_t) pureconst;
|
|
uint32_t bswap_64(uint32_t) pureconst;
|
|
|
|
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
|
|
|
|
#define bswap_16(U16) \
|
|
(isconstant(U16) ? ((((U16)&0xff00) >> 010) | (((U16)&0x00ff) << 010)) : ({ \
|
|
uint16_t Swapped16, Werd16 = (U16); \
|
|
asm("xchg\t%b0,%h0" : "=Q"(Swapped16) : "0"(Werd16)); \
|
|
Swapped16; \
|
|
}))
|
|
|
|
#define bswap_32(U32) \
|
|
(isconstant(U32) \
|
|
? ((((U32)&0xff000000) >> 030) | (((U32)&0x000000ff) << 030) | \
|
|
(((U32)&0x00ff0000) >> 010) | (((U32)&0x0000ff00) << 010)) \
|
|
: ({ \
|
|
uint32_t Swapped32, Werd32 = (U32); \
|
|
asm("bswap\t%0" : "=r"(Swapped32) : "0"(Werd32)); \
|
|
Swapped32; \
|
|
}))
|
|
|
|
#define bswap_64(U64) \
|
|
(isconstant(U64) ? ((((U64)&0xff00000000000000ul) >> 070) | \
|
|
(((U64)&0x00000000000000fful) << 070) | \
|
|
(((U64)&0x00ff000000000000ul) >> 050) | \
|
|
(((U64)&0x000000000000ff00ul) << 050) | \
|
|
(((U64)&0x0000ff0000000000ul) >> 030) | \
|
|
(((U64)&0x0000000000ff0000ul) << 030) | \
|
|
(((U64)&0x000000ff00000000ul) >> 010) | \
|
|
(((U64)&0x00000000ff000000ul) << 010)) \
|
|
: ({ \
|
|
uint64_t Swapped64, Werd64 = (U64); \
|
|
asm("bswap\t%0" : "=r"(Swapped64) : "0"(Werd64)); \
|
|
Swapped64; \
|
|
}))
|
|
|
|
#endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) */
|
|
|
|
COSMOPOLITAN_C_END_
|
|
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
|
#endif /* COSMOPOLITAN_LIBC_BITS_BSWAP_H_ */
|