cosmopolitan/libc/bits/safemacros.h
Justine Tunney f4f4caab0e Add x86_64-linux-gnu emulator
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
2020-08-25 04:43:42 -07:00

89 lines
2.7 KiB
C

#ifndef COSMOPOLITAN_LIBC_BITS_SAFEMACROS_H_
#define COSMOPOLITAN_LIBC_BITS_SAFEMACROS_H_
#ifndef __STRICT_ANSI__
#include "libc/limits.h"
#include "libc/macros.h"
#include "libc/runtime/runtime.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
long min(long, long);
long max(long, long);
long roundup(long, long);
long rounddown(long, long);
bool isempty(const char *);
const char *nulltoempty(const char *);
const char *emptytonull(const char *);
const char *firstnonnull(const char *, const char *);
uint64_t(unsignedsubtract)(uint64_t, uint64_t) pureconst;
#if !defined(__STRICT_ANSI__) && defined(__GNUC__)
#define min(x, y) \
({ \
autotype(x) MinX = (x); \
autotype(y) MinY = (y); \
MinX < MinY ? MinX : MinY; \
})
#define max(x, y) \
({ \
autotype(x) MaxX = (x); \
autotype(y) MaxY = (y); \
MaxX > MaxY ? MaxX : MaxY; \
})
#define roundup(x, k) \
({ \
autotype(x) RoundupX = (x); \
autotype(k) RoundupK = (k); \
ROUNDUP(RoundupX, RoundupK); \
})
#define rounddown(x, k) \
({ \
autotype(x) RounddownX = (x); \
autotype(k) RounddownK = (k); \
ROUNDDOWN(RounddownX, RounddownK); \
})
#define isempty(s) \
({ \
autotype(s) IsEmptyS = (s); \
!IsEmptyS || !(*IsEmptyS); \
})
#define nulltoempty(s) \
({ \
autotype(s) NullToEmptyS = (s); \
NullToEmptyS ? NullToEmptyS : ""; \
})
#define firstnonnull(a, b) \
({ \
autotype(a) FirstNonNullA = (a); \
autotype(a) FirstNonNullB = (b); \
if (!FirstNonNullA && !FirstNonNullB) abort(); \
FirstNonNullA ? FirstNonNullA : FirstNonNullB; \
})
#define emptytonull(s) \
({ \
autotype(s) EmptyToNullS = (s); \
EmptyToNullS && !(*EmptyToNullS) ? NULL : EmptyToNullS; \
})
#define unsignedsubtract(a, b) \
({ \
uint64_t UnsubA = (a); \
uint64_t UnsubB = (b); \
UnsubA >= UnsubB ? UnsubA - UnsubB : ~UnsubB + UnsubA + 1; \
})
#endif /* GNU && !ANSI */
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* !ANSI */
#endif /* COSMOPOLITAN_LIBC_BITS_SAFEMACROS_H_ */