Make small fixes and oops ran clang-format on dtoa

main
Justine Tunney 2020-06-30 19:55:47 -07:00
parent b5b60015f5
commit ac00be1a4e
47 changed files with 4933 additions and 5306 deletions

View File

@ -212,7 +212,7 @@ depend: o/$(MODE)/depend
tags: TAGS HTAGS
o/$(MODE)/.x:
@mkdir -p $(dir $@) && touch $@
@$(MKDIR) $(dir $@) && touch $@
o/$(MODE)/srcs.txt: o/$(MODE)/.x $(MAKEFILES) $(call uniq,$(foreach x,$(SRCS),$(dir $(x))))
$(file >$@) $(foreach x,$(SRCS),$(file >>$@,$(x)))

View File

@ -485,7 +485,6 @@ ape_disk:
apesh: .ascii "'\n#'\"\n" # sixth edition shebang
.ascii "o=\"$(command -v \"$0\")\"\n"
.ascii "set -- \"$o\" \"$@\"\n"
.ascii "if [ -d /Applications ]; then\n"
.ascii "dd if=\"$o\""
.ascii " of=\"$o\""
@ -521,14 +520,17 @@ apesh: .ascii "'\n#'\"\n" # sixth edition shebang
.ascii "' >&7\n"
.ascii "exec 7<&-\n"
.ascii "fi\n"
.ascii "exec \"$@\"\n" # etxtbsy tail recursion
.ascii "exec \"$0\" \"$@\"\n" # etxtbsy tail recursion
.ascii "R=$?\n" # architecture optimistic
.ascii "\n"
.ascii "if [ $R -eq 126 ] && [ \"$(uname -m)\" != x86_64 ]; then\n"
.ascii "if Q=\"$(command -v qemu-x86_64)\"; then\n"
.ascii "exec \"$Q\" \"$@\"\n"
.ascii "exec \"$Q\" \"$0\" \"$@\"\n"
.ascii "else\n"
.ascii "echo error: need qemu-x86_64 >&2\n"
.ascii "fi\n"
.ascii "elif [ $R -eq 127 ]; then\n" # means argv[0] was wrong
.ascii " exec \"$o\" \"$@\"\n" # so do a path resolution
.ascii "fi\n"
.ascii "exit $R\n"
.endobj apesh

View File

@ -333,7 +333,7 @@ SECTIONS {
/*BEGIN: Read only data that needn't be mapped after initialization */
/* Legal Notices */
#ifndef IM_FEELING_NAUGHTY
#if !defined(IM_FEELING_NAUGHTY) || defined(EMBED_NOTICES)
KEEP(*(.commentprologue))
KEEP(*(.comment))
KEEP(*(.commentepilogue))

View File

@ -21,6 +21,8 @@ APE = $(APE_DEPS) \
APELINK = \
ACTION=LINK.ape \
$(MKDIR) \
$(dir $@) && \
$(LINK) \
$(LINKARGS) \
$(OUTPUT_OPTION) && \
@ -30,6 +32,15 @@ APELINK = \
$(ZFLAGS) \
-f $@.map
APECOPY = \
ACTION=OBJCOPY.ape \
TARGET=$@ \
build/do \
$(OBJCOPY) \
-SO binary \
$< \
$@
DEFAULT_COPTS += -mno-red-zone
DEFAULT_LDFLAGS += -z max-page-size=0x1000
@ -41,7 +52,7 @@ APE_DEPS = $(APE_LIB)
APE_CHECKS = $(APE_HDRS:%=o/%.ok)
o/%.com: o/%.com.dbg
@ACTION=OBJCOPY.ape TARGET=$@ build/do $(OBJCOPY) -SO binary $< $@
@$(APECOPY)
o/ape/idata.inc: \
ape/idata.h \

View File

@ -148,12 +148,12 @@
0b00000000000011111111111111111111111111111111111111000000000000
6666555555555544444444443333333333222222222211111111110000000000
3210987654321098765432109876543210987654321098765432109876543210*/
#define PAGE_V /* */ 0b000000001
#define PAGE_RW /* */ 0b000000010
#define PAGE_U /* */ 0b000000100
#define PAGE_V /* */ 0b000000001
#define PAGE_RW /* */ 0b000000010
#define PAGE_U /* */ 0b000000100
#define PAGE_2MB /* */ 0b110000000
#define PAGE_1GB /* */ 0b110000000
#define PAGE_TA 0b011111111111111111111111111111111111111000000000000
#define PAGE_TA 0b11111111111111111111111111111111111111000000000000
#define PAGE_PA2 0b11111111111111111111111111111000000000000000000000
#if !(__ASSEMBLER__ + __LINKER__ + 0)

View File

@ -16,7 +16,7 @@
MKDIR=${MKDIR:-$(command -v mkdir) -p} || exit
# Ensure directory creation if -o PATH flag is passed.
OUT=
OUT="$TARGET"
FIRST=1
OUTARG=0
for x; do

View File

@ -33,7 +33,7 @@ o/%.inc: %.h; @ACTION=PREPROCESS build/compile $(PREPROCESS) $(OUTPUT_OPTION) -D
o/%.pkg:; @build/package $(OUTPUT_OPTION) $(addprefix -d,$(filter %.pkg,$^)) $(filter %.o,$^)
o/%.h.ok: %.h; @ACTION=CHECK.h build/compile $(COMPILE.c) -x c -g0 -o $@ $<
o/%.greg.o: %.greg.c; @ACTION=OBJECTIFY.greg build/compile $(OBJECTIFY.greg.c) $(OUTPUT_OPTION) $<
o/%.zip.o: %; @build/zipobj $(OUTPUT_OPTION) $<
o/%.zip.o: o/%; @build/zipobj $(OUTPUT_OPTION) $<
o/$(MODE)/%.a:; @$(ARCHIVE) $@ $^
o/$(MODE)/%.o: %.s; @TARGET=$@ build/assemble $(OBJECTIFY.s) $(OUTPUT_OPTION) $<
@ -95,4 +95,3 @@ build/bootstrap/%.c.gz: %.rl
@$(GZ) $(ZFLAGS) -f $(@:%.gz=%)
%.svgz: %.rl
@$(RAGEL) -V -p $< | $(DOT) -Tsvg | $(GZ) $(ZFLAGS) >$@

View File

@ -124,7 +124,8 @@ o/$(MODE)/examples/ispell.com.dbg: \
@$(APELINK)
o/$(MODE)/usr/share/dict/words: usr/share/dict/words.gz
$(GZ) $(ZFLAGS) <$< >$@
@$(MKDIR) $(dir $@)
@$(GZ) $(ZFLAGS) <$< >$@
.PHONY: o/$(MODE)/examples
o/$(MODE)/examples: \

View File

@ -606,11 +606,7 @@
#define printf out1fmt
/* #define putc(c, file) outc(c, file) */
/* #define putchar(c) out1c(c) */
#define FILE struct output
#undef fprintf
#define fprintf outfmt
#define fputs outstr
#define fflush flushout
#define FILE struct output
#define fileno(f) ((f)->fd)
/* #define ferror outerr */
#define INITARGS(argv)

View File

@ -16,7 +16,6 @@ extern const uint8_t kReverseBits[256];
uint16_t bswap_16(uint16_t) pureconst;
uint32_t bswap_32(uint32_t) pureconst;
uint32_t bswap_64(uint32_t) pureconst;
unsigned long popcount(unsigned long) pureconst;
uint32_t gray(uint32_t) pureconst;
uint32_t ungray(uint32_t) pureconst;
unsigned bcdadd(unsigned, unsigned) pureconst;
@ -87,36 +86,36 @@ unsigned long hamming(unsigned long, unsigned long) pureconst;
})
#endif
#define WRITE16LE(P, V) \
do { \
uint8_t *Ple = (P); \
uint16_t Vle = (V); \
Ple[0] = (uint8_t)(Vle >> 000); \
Ple[1] = (uint8_t)(Vle >> 010); \
#define WRITE16LE(P, V) \
do { \
uint8_t *Ple = (unsigned char *)(P); \
uint16_t Vle = (V); \
Ple[0] = (uint8_t)(Vle >> 000); \
Ple[1] = (uint8_t)(Vle >> 010); \
} while (0)
#define WRITE32LE(P, V) \
do { \
uint8_t *Ple = (P); \
uint32_t Vle = (V); \
Ple[0] = (uint8_t)(Vle >> 000); \
Ple[1] = (uint8_t)(Vle >> 010); \
Ple[2] = (uint8_t)(Vle >> 020); \
Ple[3] = (uint8_t)(Vle >> 030); \
#define WRITE32LE(P, V) \
do { \
uint8_t *Ple = (unsigned char *)(P); \
uint32_t Vle = (V); \
Ple[0] = (uint8_t)(Vle >> 000); \
Ple[1] = (uint8_t)(Vle >> 010); \
Ple[2] = (uint8_t)(Vle >> 020); \
Ple[3] = (uint8_t)(Vle >> 030); \
} while (0)
#define WRITE64LE(P, V) \
do { \
uint8_t *Ple = (P); \
uint64_t Vle = (V); \
Ple[0] = (uint8_t)(Vle >> 000); \
Ple[1] = (uint8_t)(Vle >> 010); \
Ple[2] = (uint8_t)(Vle >> 020); \
Ple[3] = (uint8_t)(Vle >> 030); \
Ple[4] = (uint8_t)(Vle >> 040); \
Ple[5] = (uint8_t)(Vle >> 050); \
Ple[6] = (uint8_t)(Vle >> 060); \
Ple[7] = (uint8_t)(Vle >> 070); \
#define WRITE64LE(P, V) \
do { \
uint8_t *Ple = (unsigned char *)(P); \
uint64_t Vle = (V); \
Ple[0] = (uint8_t)(Vle >> 000); \
Ple[1] = (uint8_t)(Vle >> 010); \
Ple[2] = (uint8_t)(Vle >> 020); \
Ple[3] = (uint8_t)(Vle >> 030); \
Ple[4] = (uint8_t)(Vle >> 040); \
Ple[5] = (uint8_t)(Vle >> 050); \
Ple[6] = (uint8_t)(Vle >> 060); \
Ple[7] = (uint8_t)(Vle >> 070); \
} while (0)
/* TODO(jart): these ones aren't coded correctly */
@ -135,27 +134,27 @@ unsigned long hamming(unsigned long, unsigned long) pureconst;
* @note we beseech clang devs for flag constraints
*/
#ifdef __GCC_ASM_FLAG_OUTPUTS__ /* GCC6+ CLANG10+ */
#define CF "=@ccc"
#define CFLAG(OP) OP
#define ZF "=@ccz"
#define ZFLAG(OP) OP
#define OF "=@cco"
#define OFLAG(OP) OP
#define SF "=@ccs"
#define SFLAG(SP) SP
#define ABOVEF "=@cca" /* i.e. !ZF && !CF */
#define ABOVEFLAG(OP) OP
#define CFLAG_CONSTRAINT "=@ccc"
#define CFLAG_ASM(OP) OP
#define ZFLAG_CONSTRAINT "=@ccz"
#define ZFLAG_ASM(OP) OP
#define OFLAG_CONSTRAINT "=@cco"
#define OFLAG_ASM(OP) OP
#define SFLAG_CONSTRAINT "=@ccs"
#define SFLAG_ASM(SP) SP
#define ABOVE_CONSTRAINT "=@cca" /* i.e. !ZF && !CF */
#define ABOVEFLAG_ASM(OP) OP
#else
#define CF "=q"
#define CFLAG(OP) OP "\n\tsetc\t%b0"
#define ZF "=q"
#define ZFLAG(OP) OP "\n\tsetz\t%b0"
#define OF "=q"
#define OFLAG(OP) OP "\n\tseto\t%b0"
#define SF "=q"
#define SFLAG(SP) OP "\n\tsets\t%b0"
#define ABOVEF "=@cca"
#define ABOVEFLAG(OP) OP "\n\tseta\t%b0"
#define CFLAG_CONSTRAINT "=q"
#define CFLAG_ASM(OP) OP "\n\tsetc\t%b0"
#define ZFLAG_CONSTRAINT "=q"
#define ZFLAG_ASM(OP) OP "\n\tsetz\t%b0"
#define OFLAG_CONSTRAINT "=q"
#define OFLAG_ASM(OP) OP "\n\tseto\t%b0"
#define SFLAG_CONSTRAINT "=q"
#define SFLAG_ASM(SP) OP "\n\tsets\t%b0"
#define ABOVE_CONSTRAINT "=@cca"
#define ABOVEFLAG_ASM(OP) OP "\n\tseta\t%b0"
#endif
/**
@ -226,23 +225,32 @@ unsigned long hamming(unsigned long, unsigned long) pureconst;
* @see Intel's Six Thousand Page Manual V.2A 3-113
* @see bts(), btr(), btc()
*/
#define bt(MEM, BIT) \
({ \
bool OldBit; \
if (isconstant(BIT)) { \
asm(CFLAG("bt%z1\t%2,%1") \
: CF(OldBit) \
: "m"((MEM)[(BIT) / (sizeof((MEM)[0]) * CHAR_BIT)]), \
"J"((BIT) % (sizeof((MEM)[0]) * CHAR_BIT)) \
: "cc"); \
} else if (sizeof((MEM)[0]) == 2) { \
asm(CFLAG("bt\t%w2,%1") : CF(OldBit) : "m"((MEM)[0]), "r"(BIT) : "cc"); \
} else if (sizeof((MEM)[0]) == 4) { \
asm(CFLAG("bt\t%k2,%1") : CF(OldBit) : "m"((MEM)[0]), "r"(BIT) : "cc"); \
} else if (sizeof((MEM)[0]) == 8) { \
asm(CFLAG("bt\t%q2,%1") : CF(OldBit) : "m"((MEM)[0]), "r"(BIT) : "cc"); \
} \
OldBit; \
#define bt(MEM, BIT) \
({ \
bool OldBit; \
if (isconstant(BIT)) { \
asm(CFLAG_ASM("bt%z1\t%2,%1") \
: CFLAG_CONSTRAINT(OldBit) \
: "m"((MEM)[(BIT) / (sizeof((MEM)[0]) * CHAR_BIT)]), \
"J"((BIT) % (sizeof((MEM)[0]) * CHAR_BIT)) \
: "cc"); \
} else if (sizeof((MEM)[0]) == 2) { \
asm(CFLAG_ASM("bt\t%w2,%1") \
: CFLAG_CONSTRAINT(OldBit) \
: "m"((MEM)[0]), "r"(BIT) \
: "cc"); \
} else if (sizeof((MEM)[0]) == 4) { \
asm(CFLAG_ASM("bt\t%k2,%1") \
: CFLAG_CONSTRAINT(OldBit) \
: "m"((MEM)[0]), "r"(BIT) \
: "cc"); \
} else if (sizeof((MEM)[0]) == 8) { \
asm(CFLAG_ASM("bt\t%q2,%1") \
: CFLAG_CONSTRAINT(OldBit) \
: "m"((MEM)[0]), "r"(BIT) \
: "cc"); \
} \
OldBit; \
})
#define bts(MEM, BIT) __BitOp("bts", BIT, MEM) /** bit test and set */
@ -285,27 +293,27 @@ unsigned long hamming(unsigned long, unsigned long) pureconst;
* @return true if value was exchanged, otherwise false
* @see lockcmpxchg()
*/
#define cmpxchg(IFTHING, ISEQUALTOME, REPLACEITWITHME) \
({ \
bool DidIt; \
asm(ZFLAG("cmpxchg\t%3,%1") \
: ZF(DidIt), "+m"(*(IFTHING)), "+a"(*(ISEQUALTOME)) \
: "r"((typeof(*(IFTHING)))(REPLACEITWITHME)) \
: "cc"); \
DidIt; \
#define cmpxchg(IFTHING, ISEQUALTOME, REPLACEITWITHME) \
({ \
bool DidIt; \
asm(ZFLAG_ASM("cmpxchg\t%3,%1") \
: ZFLAG_CONSTRAINT(DidIt), "+m"(*(IFTHING)), "+a"(*(ISEQUALTOME)) \
: "r"((typeof(*(IFTHING)))(REPLACEITWITHME)) \
: "cc"); \
DidIt; \
})
#define ezcmpxchg(IFTHING, ISEQUALTOME, REPLACEITWITHME) \
({ \
bool DidIt; \
autotype(IFTHING) IfThing = (IFTHING); \
typeof(*IfThing) IsEqualToMe = (ISEQUALTOME); \
typeof(*IfThing) ReplaceItWithMe = (REPLACEITWITHME); \
asm(ZFLAG("cmpxchg\t%3,%1") \
: ZF(DidIt), "+m"(*IfThing), "+a"(IsEqualToMe) \
: "r"(ReplaceItWithMe) \
: "cc"); \
DidIt; \
#define ezcmpxchg(IFTHING, ISEQUALTOME, REPLACEITWITHME) \
({ \
bool DidIt; \
autotype(IFTHING) IfThing = (IFTHING); \
typeof(*IfThing) IsEqualToMe = (ISEQUALTOME); \
typeof(*IfThing) ReplaceItWithMe = (REPLACEITWITHME); \
asm(ZFLAG_ASM("cmpxchg\t%3,%1") \
: ZFLAG_CONSTRAINT(DidIt), "+m"(*IfThing), "+a"(IsEqualToMe) \
: "r"(ReplaceItWithMe) \
: "cc"); \
DidIt; \
})
/**
@ -315,14 +323,14 @@ unsigned long hamming(unsigned long, unsigned long) pureconst;
* @return true if value was exchanged, otherwise false
* @see lockcmpxchg()
*/
#define lockcmpxchg(IFTHING, ISEQUALTOME, REPLACEITWITHME) \
({ \
bool DidIt; \
asm(ZFLAG("lock cmpxchg\t%3,%1") \
: ZF(DidIt), "+m"(*(IFTHING)), "+a"(*(ISEQUALTOME)) \
: "r"((typeof(*(IFTHING)))(REPLACEITWITHME)) \
: "cc"); \
DidIt; \
#define lockcmpxchg(IFTHING, ISEQUALTOME, REPLACEITWITHME) \
({ \
bool DidIt; \
asm(ZFLAG_ASM("lock cmpxchg\t%3,%1") \
: ZFLAG_CONSTRAINT(DidIt), "+m"(*(IFTHING)), "+a"(*(ISEQUALTOME)) \
: "r"((typeof(*(IFTHING)))(REPLACEITWITHME)) \
: "cc"); \
DidIt; \
})
/**
@ -431,19 +439,6 @@ unsigned long hamming(unsigned long, unsigned long) pureconst;
*/
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#define popcount(X) (isconstant(X) ? __builtin_popcount(X) : __popcount(X))
#define popcount$nehalem(X) \
({ \
typeof(X) BitCount; \
asm("popcnt\t%1,%0" : "=r,r"(BitCount) : "r,m"(X) : "cc"); \
BitCount; \
})
#ifdef __POPCNT__
#define __popcount(X) popcount$nehalem(X)
#else
#define __popcount(X) (popcount)(X)
#endif
#define bswap_16(U16) \
(isconstant(U16) ? ((((U16)&0xff00) >> 010) | (((U16)&0x00ff) << 010)) : ({ \
uint16_t Swapped16, Werd16 = (U16); \
@ -500,31 +495,32 @@ unsigned long hamming(unsigned long, unsigned long) pureconst;
MEM; \
})
#define __BitOp(OP, BIT, MEM) \
({ \
bool OldBit; \
if (isconstant(BIT)) { \
asm(CFLAG(OP "%z1\t%2,%1") \
: CF(OldBit), "+m"((MEM)[(BIT) / (sizeof((MEM)[0]) * CHAR_BIT)]) \
: "J"((BIT) % (sizeof((MEM)[0]) * CHAR_BIT)) \
: "cc"); \
} else if (sizeof((MEM)[0]) == 2) { \
asm(CFLAG(OP "\t%w2,%1") \
: CF(OldBit), "+m"((MEM)[0]) \
: "r"(BIT) \
: "cc"); \
} else if (sizeof((MEM)[0]) == 4) { \
asm(CFLAG(OP "\t%k2,%1") \
: CF(OldBit), "+m"((MEM)[0]) \
: "r"(BIT) \
: "cc"); \
} else if (sizeof((MEM)[0]) == 8) { \
asm(CFLAG(OP "\t%q2,%1") \
: CF(OldBit), "+m"((MEM)[0]) \
: "r"(BIT) \
: "cc"); \
} \
OldBit; \
#define __BitOp(OP, BIT, MEM) \
({ \
bool OldBit; \
if (isconstant(BIT)) { \
asm(CFLAG_ASM(OP "%z1\t%2,%1") \
: CFLAG_CONSTRAINT(OldBit), \
"+m"((MEM)[(BIT) / (sizeof((MEM)[0]) * CHAR_BIT)]) \
: "J"((BIT) % (sizeof((MEM)[0]) * CHAR_BIT)) \
: "cc"); \
} else if (sizeof((MEM)[0]) == 2) { \
asm(CFLAG_ASM(OP "\t%w2,%1") \
: CFLAG_CONSTRAINT(OldBit), "+m"((MEM)[0]) \
: "r"(BIT) \
: "cc"); \
} else if (sizeof((MEM)[0]) == 4) { \
asm(CFLAG_ASM(OP "\t%k2,%1") \
: CFLAG_CONSTRAINT(OldBit), "+m"((MEM)[0]) \
: "r"(BIT) \
: "cc"); \
} else if (sizeof((MEM)[0]) == 8) { \
asm(CFLAG_ASM(OP "\t%q2,%1") \
: CFLAG_CONSTRAINT(OldBit), "+m"((MEM)[0]) \
: "r"(BIT) \
: "cc"); \
} \
OldBit; \
})
COSMOPOLITAN_C_END_

View File

@ -17,11 +17,11 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/bits/popcnt.h"
/**
* Counts number of different bits.
*/
unsigned long hamming(unsigned long x, unsigned long y) {
return popcount(x ^ y);
return popcnt(x ^ y);
}

View File

@ -17,26 +17,20 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/bits/popcnt.h"
static noinline uint32_t popcount$swar32(uint32_t x) {
static uint32_t popcnt32(uint32_t x) {
x -= (x >> 1) & 0x55555555;
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
return (((x + (x >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
}
unsigned long(popcount)(unsigned long x) {
size_t i;
unsigned long(popcnt)(unsigned long x) {
unsigned long r;
if (X86_HAVE(POPCNT)) {
return popcount$nehalem(x);
} else {
r = 0;
for (i = 0; i < sizeof(x); i += 4) {
r |= popcount$swar32(x);
x >>= 32;
}
return r;
r = 0;
while (x) {
r += popcnt32(x);
x >>= 32;
}
return r;
}

24
libc/bits/popcnt.h 100644
View File

@ -0,0 +1,24 @@
#ifndef COSMOPOLITAN_LIBC_BITS_POPCNT_H_
#define COSMOPOLITAN_LIBC_BITS_POPCNT_H_
#include "libc/nexgen32e/x86feature.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
unsigned long popcnt(unsigned long) pureconst;
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#define popcnt(X) \
(isconstant(X) ? __builtin_popcount(X) : ({ \
unsigned long Res, Pop = (X); \
if (X86_HAVE(POPCNT)) { \
asm("popcnt\t%1,%0" : "=r"(Res) : "r"(Pop) : "cc"); \
} else { \
Res = (popcnt)(Pop); \
} \
Res; \
}))
#endif /* GNUC && !ANSI */
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_BITS_POPCNT_H_ */

View File

@ -32,8 +32,8 @@ privileged int __mprotect(void *addr, uint64_t len, int prot) {
int64_t rc;
uint32_t oldprot;
if (!IsWindows()) {
asm volatile(CFLAG("syscall")
: CF(cf), "=a"(rc)
asm volatile(CFLAG_ASM("syscall")
: CFLAG_CONSTRAINT(cf), "=a"(rc)
: "1"(__NR_mprotect), "D"(addr), "S"(len), "d"(prot)
: "rcx", "r11", "memory", "cc");
if (cf) {

View File

@ -30,7 +30,7 @@ unsigned long strtoul(const char *, char **, int) paramsnonnull((1));
long long strtoll(const char *, char **, int) paramsnonnull((1));
unsigned long long strtoull(const char *, char **, int) paramsnonnull((1));
long long strtonum(const char *, long long, long long, const char **);
intmax_t div10(intmax_t x, unsigned *rem) hidden;
intmax_t div10(intmax_t, unsigned *) hidden;
intmax_t strtoimax(const char *, char **, int) paramsnonnull((1));
uintmax_t strtoumax(const char *, char **, int) paramsnonnull((1));
intmax_t wcstoimax(const wchar_t *, wchar_t **, int);

View File

@ -37,10 +37,10 @@
* @see strtoumax
*/
intmax_t strtoimax(const char *s, char **endptr, int base) {
bool neg;
uintmax_t x;
intmax_t res;
unsigned diglet, bits;
bool neg, islong, isunsigned;
x = 0;
bits = 0;
@ -100,10 +100,23 @@ intmax_t strtoimax(const char *s, char **endptr, int base) {
}
}
if (endptr) {
*endptr = s;
if ((isunsigned = *s == 'u' || *s == 'U')) s++;
if ((islong = *s == 'l' || *s == 'L')) s++;
if (endptr) *endptr = s;
if (neg) {
res = -x;
} else {
res = x;
}
res = x;
return neg ? -res : res;
if (isunsigned) {
if (islong) {
res = (uint64_t)res;
} else {
res = (uint32_t)res;
}
}
return res;
}

View File

@ -54,23 +54,17 @@ int ftoa(int out(int, void *), void *arg, long double value, unsigned long prec,
diff = 0;
if (isnan(value)) {
buf[0] = 'N';
buf[1] = 'A';
buf[2] = 'N';
buf[0] = 'n';
buf[1] = 'a';
buf[2] = 'n';
buf[3] = '\0';
len += 3;
} else if (isinf(value) || fabsl(value) > 0x7ffffffffffffffful) {
buf[0] = 'f';
buf[1] = 'n';
buf[2] = 'i';
buf[3] = '\0';
len += 3;
} else if (isinf(value) ||
(/* TODO(jart): need this? */ fabsl(value) > 0x7fffffff)) {
buf[0] = 'Y';
buf[1] = 'T';
buf[2] = 'I';
buf[3] = 'N';
buf[4] = 'I';
buf[5] = 'F';
buf[6] = 'N';
buf[7] = 'I';
buf[8] = '\0';
len += 8;
} else {
/* set default precision to 6, if not set explicitly */

View File

@ -31,6 +31,9 @@ static void onmemchunk(void *start, void *end, size_t used_bytes, void *arg) {
(intptr_t)end - (intptr_t)start);
}
/**
* Prints memory mappings.
*/
void meminfo(FILE *f) {
memsummary(f);
(fprintf)(f, "%*s %*s %*s %*s\n", POINTER_XDIGITS, "start",

View File

@ -20,7 +20,6 @@
#define ABS(X) ((X) >= 0 ? (X) : -(X))
#define MIN(X, Y) ((Y) > (X) ? (X) : (Y))
#define MAX(X, Y) ((Y) < (X) ? (X) : (Y))
#define MOD(X, Y) ((X) - (ABS(Y)) * ((X) / ABS(Y)))
#define PASTE(A, B) __PASTE(A, B)
#define STRINGIFY(A) __STRINGIFY(A)
#define EQUIVALENT(X, Y) (isconstant((X) == (Y)) && ((X) == (Y)))

View File

@ -264,6 +264,16 @@ _init_\name:
.popsection
.endm
/ ICE Breakpoint.
/ Modern gas forgot this but objdump knows
/ @mode long,legacy,real
.macro icebp
.byte 0xF1
.endm
.macro int1
icebp
.endm
/ Sets breakpoint for software debugger.
/ @mode long,legacy,real
.macro .softicebp

View File

@ -41,14 +41,14 @@ bLoop:
vbitmask_t r1;
uint8_v v1, v2;
const uint8_v kZero = {0};
asm(ZFLAG("vmovdqu\t%5,%2\n\t" /* move because gcc problematic */
"vpcmpeqb\t%4,%2,%1\n\t" /* check for equality in p1 and p2 */
"vpcmpeqb\t%6,%2,%2\n\t" /* check for nul in p1 */
"vpandn\t%7,%1,%2\n\t" /* most complicated bitwise not ever */
"vpor\t%2,%1,%1\n\t" /* check for nul in p2 */
"pmovmskb\t%1,%3\n\t" /* turn 256 bits into 32 bits */
"bsf\t%3,%3") /* find stop byte */
: ZF(zf), "=x"(v1), "=x"(v2), "=r"(r1)
asm(ZFLAG_ASM("vmovdqu\t%5,%2\n\t" /* move because gcc problematic */
"vpcmpeqb\t%4,%2,%1\n\t" /* check for equality in p1 and p2 */
"vpcmpeqb\t%6,%2,%2\n\t" /* check for nul in p1 */
"vpandn\t%7,%1,%2\n\t" /* most complicated bitwise not ever */
"vpor\t%2,%1,%1\n\t" /* check for nul in p2 */
"pmovmskb\t%1,%3\n\t" /* turn 256 bits into 32 bits */
"bsf\t%3,%3") /* find stop byte */
: ZFLAG_CONSTRAINT(zf), "=x"(v1), "=x"(v2), "=r"(r1)
: "m"(*(const uint8_v *)(p1 + i)), "m"(*(const uint8_v *)(p2 + i)),
"x"(kZero), "m"(kVectorSize));
if (zf) goto vLoop;

View File

@ -36,8 +36,8 @@ uint64_t rdrand(void) {
for (;;) {
for (i = 0; i < 10; ++i) {
/* CF=1: Destination register valid. Quoth Intel DRNG-SIG 4.1.3 */
asm volatile(CFLAG("rdrand\t%1")
: CF(cf), "=r"(res)
asm volatile(CFLAG_ASM("rdrand\t%1")
: CFLAG_CONSTRAINT(cf), "=r"(res)
: /* no inputs */
: "cc");
if (cf) return res;

View File

@ -19,6 +19,7 @@
*/
#include "libc/assert.h"
#include "libc/bits/bits.h"
#include "libc/bits/popcnt.h"
#include "libc/bits/safemacros.h"
#include "libc/calls/calls.h"
#include "libc/runtime/buffer.h"
@ -54,9 +55,9 @@ void *balloc(struct GuardedBuffer *b, unsigned a, size_t n) {
assert(a >= 1);
assert(a <= kGuard);
assert(kGuard < kGrain);
assert(popcount(a) == 1);
assert(popcount(kGuard) == 1);
assert(popcount(kGrain) == 1);
assert(popcnt(a) == 1);
assert(popcnt(kGuard) == 1);
assert(popcnt(kGrain) == 1);
assert(n < 0x800000000000ul - kGrain - kGuard);
if (n) {

View File

@ -45,9 +45,9 @@
({ \
int KillAx; \
unsigned char Cf; \
asm volatile(CFLAG("clc\n\t" \
"syscall") \
: CF(Cf), "=a"(KillAx) \
asm volatile(CFLAG_ASM("clc\n\t" \
"syscall") \
: CFLAG_CONSTRAINT(Cf), "=a"(KillAx) \
: "1"(__NR_kill), "D"(pid), "S"(sig) \
: "rcx", "r11", "cc", "memory"); \
Cf ? -KillAx : KillAx; \

View File

@ -18,6 +18,7 @@
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/bits/popcnt.h"
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/mem/mem.h"
@ -46,7 +47,7 @@ FILE *fmemopen(void *buf, size_t size, const char *mode) {
return NULL;
}
if (size && popcount(size) != 1) {
if (size && popcnt(size) != 1) {
einval();
return NULL;
}

View File

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/bits/popcnt.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/errfuns.h"
@ -26,7 +26,7 @@
* Sets buffer on stdio stream.
*/
void setbuffer(FILE *f, char *buf, size_t size) {
if (size && popcount(size) != 1) abort();
if (size && popcnt(size) != 1) abort();
if (buf && f->buf != (unsigned char *)buf) {
free_s(&f->buf);
if (!size) size = BUFSIZ;

View File

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/bits/popcnt.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/errfuns.h"
@ -31,7 +31,7 @@
* @return 0 on success or -1 on error
*/
int setvbuf(FILE *f, char *buf, int mode, size_t size) {
if (size && popcount(size) != 1) return einval();
if (size && popcnt(size) != 1) return einval();
setbuffer(f, buf, size);
f->bufmode = mode;
return 0;

View File

@ -0,0 +1,17 @@
#ifndef COSMOPOLITAN_LIBC_TINYMATH_EMODL_H_
#define COSMOPOLITAN_LIBC_TINYMATH_EMODL_H_
#include "libc/math.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
/**
* Returns Euclidean floating-point division remainder.
*
* @return (𝑥 mod 𝑦) [0.,𝑦)
* @see fmodl()
*/
static long double emodl(long double x, long double y) {
return x - fabsl(y) * floorl(x / fabsl(y));
}
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_TINYMATH_EMODL_H_ */

View File

@ -23,10 +23,18 @@
/* todo(jart): work on this more */
TEST(strtoimax, testZero) { EXPECT_EQ(0, strtoimax("0", NULL, 0)); }
TEST(strtoimax, testDecimal) { EXPECT_EQ(-123, strtoimax("-123", NULL, 0)); }
TEST(strtoimax, testHex) { EXPECT_EQ(-255, strtoimax("-0xff", NULL, 0)); }
TEST(strtoimax, testOctal) { EXPECT_EQ(-123, strtoimax("-0173", NULL, 0)); }
TEST(strtoimax, testZero) {
EXPECT_EQ(0, strtoimax("0", NULL, 0));
}
TEST(strtoimax, testDecimal) {
EXPECT_EQ(-123, strtoimax("-123", NULL, 0));
}
TEST(strtoimax, testHex) {
EXPECT_EQ(-255, strtoimax("-0xff", NULL, 0));
}
TEST(strtoimax, testOctal) {
EXPECT_EQ(-123, strtoimax("-0173", NULL, 0));
}
TEST(strtoimax, testLimits) {
EXPECT_EQ(
@ -36,3 +44,9 @@ TEST(strtoimax, testLimits) {
((uintmax_t)0x7fffffffffffffff) << 64 | (uintmax_t)0xffffffffffffffff,
strtoimax("0x7fffffffffffffffffffffffffffffff", NULL, 0));
}
TEST(strtoimax, testZeroExtend) {
EXPECT_EQ(-1, strtoimax("-1", NULL, 0));
EXPECT_EQ(0xffffffff, strtoimax("-1u", NULL, 0));
EXPECT_EQ(0xffffffffffffffff, strtoimax("-1ul", NULL, 0));
}

View File

@ -434,7 +434,7 @@ TEST(sprintf, test_float) {
EXPECT_STREQ("a0.5 ", Format("a%-5.1f", 0.5));
EXPECT_STREQ("a0.5 end", Format("a%-5.1fend", 0.5));
/* out of range in the moment, need to be fixed by someone */
EXPECT_STREQ("INFINITY", Format("%.1f", 1E20));
EXPECT_STREQ("inf", Format("%.1f", 1E20));
}
TEST(sprintf, test_types) {

View File

@ -33,10 +33,10 @@ TEST(round, test) {
TEST(round, testCornerCases) {
EXPECT_STREQ("-0", gc(xdtoa(round(-0.0))));
EXPECT_STREQ("NAN", gc(xdtoa(round(NAN))));
EXPECT_STREQ("-NAN", gc(xdtoa(round(-NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(round(INFINITY))));
EXPECT_STREQ("-INFINITY", gc(xdtoa(round(-INFINITY))));
EXPECT_STREQ("nan", gc(xdtoa(round(NAN))));
EXPECT_STREQ("-nan", gc(xdtoa(round(-NAN))));
EXPECT_STREQ("inf", gc(xdtoa(round(INFINITY))));
EXPECT_STREQ("-inf", gc(xdtoa(round(-INFINITY))));
}
TEST(lround, test) {
@ -60,10 +60,10 @@ TEST(roundf, test) {
TEST(roundf, testCornerCases) {
EXPECT_STREQ("-0", gc(xdtoa(roundf(-0.0))));
EXPECT_STREQ("NAN", gc(xdtoa(roundf(NAN))));
EXPECT_STREQ("-NAN", gc(xdtoa(roundf(-NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(roundf(INFINITY))));
EXPECT_STREQ("-INFINITY", gc(xdtoa(roundf(-INFINITY))));
EXPECT_STREQ("nan", gc(xdtoa(roundf(NAN))));
EXPECT_STREQ("-nan", gc(xdtoa(roundf(-NAN))));
EXPECT_STREQ("inf", gc(xdtoa(roundf(INFINITY))));
EXPECT_STREQ("-inf", gc(xdtoa(roundf(-INFINITY))));
}
TEST(lroundf, test) {

View File

@ -38,10 +38,10 @@ TEST(round, test) {
TEST(round, testCornerCases) {
EXPECT_STREQ("-0", gc(xdtoa(tinymath_round(-0.0))));
EXPECT_STREQ("NAN", gc(xdtoa(tinymath_round(NAN))));
EXPECT_STREQ("-NAN", gc(xdtoa(tinymath_round(-NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(tinymath_round(INFINITY))));
EXPECT_STREQ("-INFINITY", gc(xdtoa(tinymath_round(-INFINITY))));
EXPECT_STREQ("nan", gc(xdtoa(tinymath_round(NAN))));
EXPECT_STREQ("-nan", gc(xdtoa(tinymath_round(-NAN))));
EXPECT_STREQ("inf", gc(xdtoa(tinymath_round(INFINITY))));
EXPECT_STREQ("-inf", gc(xdtoa(tinymath_round(-INFINITY))));
}
TEST(lround, test) {
@ -65,10 +65,10 @@ TEST(roundf, test) {
TEST(roundf, testCornerCases) {
EXPECT_STREQ("-0", gc(xdtoa(tinymath_roundf(-0.0))));
EXPECT_STREQ("NAN", gc(xdtoa(tinymath_roundf(NAN))));
EXPECT_STREQ("-NAN", gc(xdtoa(tinymath_roundf(-NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(tinymath_roundf(INFINITY))));
EXPECT_STREQ("-INFINITY", gc(xdtoa(tinymath_roundf(-INFINITY))));
EXPECT_STREQ("nan", gc(xdtoa(tinymath_roundf(NAN))));
EXPECT_STREQ("-nan", gc(xdtoa(tinymath_roundf(-NAN))));
EXPECT_STREQ("inf", gc(xdtoa(tinymath_roundf(INFINITY))));
EXPECT_STREQ("-inf", gc(xdtoa(tinymath_roundf(-INFINITY))));
}
TEST(lroundf, test) {
@ -103,18 +103,18 @@ TEST(roundf$k8, test) {
TEST(round$k8, testCornerCases) {
EXPECT_STREQ("-0", gc(xdtoa(tinymath_round$k8(-0.0))));
EXPECT_STREQ("NAN", gc(xdtoa(tinymath_round$k8(NAN))));
EXPECT_STREQ("-NAN", gc(xdtoa(tinymath_round$k8(-NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(tinymath_round$k8(INFINITY))));
EXPECT_STREQ("-INFINITY", gc(xdtoa(tinymath_round$k8(-INFINITY))));
EXPECT_STREQ("nan", gc(xdtoa(tinymath_round$k8(NAN))));
EXPECT_STREQ("-nan", gc(xdtoa(tinymath_round$k8(-NAN))));
EXPECT_STREQ("inf", gc(xdtoa(tinymath_round$k8(INFINITY))));
EXPECT_STREQ("-inf", gc(xdtoa(tinymath_round$k8(-INFINITY))));
}
TEST(roundf$k8, testCornerCases) {
EXPECT_STREQ("-0", gc(xdtoa(tinymath_roundf$k8(-0.0))));
EXPECT_STREQ("NAN", gc(xdtoa(tinymath_roundf$k8(NAN))));
EXPECT_STREQ("-NAN", gc(xdtoa(tinymath_roundf$k8(-NAN))));
EXPECT_STREQ("INFINITY", gc(xdtoa(tinymath_roundf$k8(INFINITY))));
EXPECT_STREQ("-INFINITY", gc(xdtoa(tinymath_roundf$k8(-INFINITY))));
EXPECT_STREQ("nan", gc(xdtoa(tinymath_roundf$k8(NAN))));
EXPECT_STREQ("-nan", gc(xdtoa(tinymath_roundf$k8(-NAN))));
EXPECT_STREQ("inf", gc(xdtoa(tinymath_roundf$k8(INFINITY))));
EXPECT_STREQ("-inf", gc(xdtoa(tinymath_roundf$k8(-INFINITY))));
}
#endif

View File

@ -83,3 +83,8 @@ TEST(strwidth, tab) {
TEST(wcwidth, block) {
EXPECT_EQ(1, wcwidth(u''));
}
TEST(strwidth, testTextDelimitingControlCodes_dontHaveSubstance) {
EXPECT_EQ(0, strwidth("\0"));
EXPECT_EQ(0, strwidth("\1"));
}

View File

@ -27,7 +27,7 @@ THIRD_PARTY_COMPILER_RT_A_CHECKS = \
$(THIRD_PARTY_COMPILER_RT_A_HDRS:%=o/$(MODE)/%.ok)
THIRD_PARTY_COMPILER_RT_A_DIRECTDEPS = \
LIBC_MATH \
LIBC_TINYMATH \
LIBC_STUBS
THIRD_PARTY_COMPILER_RT_A_DEPS := \

View File

@ -1029,5 +1029,3 @@ void *dlrealloc(void *oldmem, size_t bytes) {
}
return mem;
}
asm(".include \"third_party/dlmalloc/COPYING\"");

9340
third_party/dtoa/dtoa.c vendored

File diff suppressed because it is too large Load Diff

View File

@ -3,20 +3,15 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/* see also xdtoa() in //libc/x */
double strtod(const char *s00, char **se);
char *g_fmt(char *buf /*[32]*/, double x);
char *dtoa(double d, int mode, int ndigits, int *decpt, int *sign,
char **rve) nodiscard;
void freedtoa(char *s);
char *dtoa_r(double dd, int mode, int ndigits, int *decpt, int *sign,
char **rve, char *buf, size_t blen);
double plan9_strtod(const char *as, char **aas);
/* #if defined(TINY) || defined(TINY_STRTOD) */
/* #define strtod(X, Y) plan9_strtod(X, Y) */
/* #endif */
double strtod(const char *, char **);
double plan9_strtod(const char *, char **);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

43
third_party/xed/avx.h vendored 100644
View File

@ -0,0 +1,43 @@
#ifndef COSMOPOLITAN_THIRD_PARTY_XED_AVX_H_
#define COSMOPOLITAN_THIRD_PARTY_XED_AVX_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
union XedAvxC4Payload1 {
struct {
unsigned map : 5;
unsigned b_inv : 1;
unsigned x_inv : 1;
unsigned r_inv : 1;
unsigned pad : 24;
} s;
unsigned u32;
};
union XedAvxC4Payload2 {
struct {
unsigned pp : 2;
unsigned l : 1;
unsigned vvv210 : 3;
unsigned v3 : 1;
unsigned w : 1;
unsigned pad : 24;
} s;
unsigned u32;
};
union XedAvxC5Payload {
struct {
unsigned pp : 2;
unsigned l : 1;
unsigned vvv210 : 3;
unsigned v3 : 1;
unsigned r_inv : 1;
unsigned pad : 24;
} s;
unsigned u32;
};
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_THIRD_PARTY_XED_AVX_H_ */

44
third_party/xed/avx512.h vendored 100644
View File

@ -0,0 +1,44 @@
#ifndef COSMOPOLITAN_THIRD_PARTY_XED_AVX512_H_
#define COSMOPOLITAN_THIRD_PARTY_XED_AVX512_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
union XedAvx512Payload1 {
struct {
unsigned map : 4;
unsigned rr_inv : 1;
unsigned b_inv : 1;
unsigned x_inv : 1;
unsigned r_inv : 1;
unsigned pad : 24;
} s;
unsigned u32;
};
union XedAvx512Payload2 {
struct {
unsigned pp : 2;
unsigned ubit : 1;
unsigned vexdest210 : 3;
unsigned vexdest3 : 1;
unsigned rexw : 1;
unsigned pad : 24;
} s;
unsigned u32;
};
union XedAvx512Payload3 {
struct {
unsigned mask : 3;
unsigned vexdest4p : 1;
unsigned bcrc : 1;
unsigned llrc : 2;
unsigned z : 1;
unsigned pad : 24;
} s;
unsigned u32;
};
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_THIRD_PARTY_XED_AVX512_H_ */

167
third_party/xed/x86.h vendored
View File

@ -313,12 +313,18 @@ struct XedChipFeatures {
};
struct XedOperands {
/* data structure optimized for gcc code size */
uint8_t imm_width;
uint8_t map; /* enum XedIldMap */
uint8_t error; /* enum XedError */
uint8_t mode;
uint8_t rexw;
uint8_t osz;
uint8_t mode; /* real,legacy,long */
uint8_t modrm; /* selects address register */
uint8_t sib; /* scaled index base x86_64 */
uint8_t rexw : 1;
uint8_t rexr : 1;
uint8_t rexx : 1;
uint8_t rexb : 1;
uint8_t osz; /* operand size override prefix */
uint8_t max_bytes;
uint8_t nominal_opcode;
uint8_t out_of_bytes;
@ -326,6 +332,7 @@ struct XedOperands {
int64_t disp;
uint64_t uimm0;
enum XedChip chip;
uint8_t srm;
uint8_t amd3dnow;
uint8_t asz;
uint8_t bcrc;
@ -334,16 +341,11 @@ struct XedOperands {
uint8_t ild_f2;
uint8_t ild_f3;
uint8_t lock;
uint8_t modep5;
uint8_t modep55c;
uint8_t mode_first_prefix;
uint8_t prefix66;
uint8_t realmode;
uint8_t rex;
uint8_t rexb;
uint8_t rexr;
uint8_t rexrr;
uint8_t rexx;
uint8_t ubit;
uint8_t vexdest3;
uint8_t vexdest4;
@ -355,7 +357,6 @@ struct XedOperands {
uint8_t llrc;
uint8_t mod;
uint8_t rep;
uint8_t sibscale;
uint8_t vex_prefix;
uint8_t vl;
uint8_t hint;
@ -363,15 +364,11 @@ struct XedOperands {
uint8_t reg;
uint8_t rm;
uint8_t seg_ovd;
uint8_t sibbase;
uint8_t sibindex;
uint8_t srm;
uint8_t vexdest210;
uint8_t vexvalid;
uint8_t esrc;
uint8_t ild_seg;
uint8_t imm1_bytes;
uint8_t modrm_byte;
uint8_t nprefixes;
uint8_t nrexes;
uint8_t nseg_prefixes;
@ -384,154 +381,12 @@ struct XedOperands {
uint8_t uimm1;
};
struct XedInst {
uint8_t noperands;
uint8_t cpl;
uint8_t flag_complex;
uint8_t exceptions;
uint16_t flag_info_index;
uint16_t iform_enum;
uint16_t operand_base;
uint16_t attributes;
};
struct XedEncoderIforms {
unsigned x_MEMDISPv;
unsigned x_SIBBASE_ENCODE_SIB1;
unsigned x_VEX_MAP_ENC;
unsigned x_SIB_NT;
unsigned x_UIMM8_1;
unsigned x_SIBBASE_ENCODE;
unsigned x_VEX_ESCVL_ENC;
unsigned x_PREFIX_ENC;
unsigned x_VEXED_REX;
unsigned x_REMOVE_SEGMENT;
unsigned x_VSIB_ENC;
unsigned x_EVEX_REXB_ENC;
unsigned x_MODRM_RM_ENCODE_EA64_SIB0;
unsigned x_VEX_REXXB_ENC;
unsigned x_EVEX_REXRR_ENC;
unsigned x_AVX512_EVEX_BYTE3_ENC;
unsigned x_EVEX_REXW_VVVV_ENC;
unsigned x_VEX_REG_ENC;
unsigned x_SIMM8;
unsigned x_XOP_MAP_ENC;
unsigned x_MODRM_RM_ENCODE_EA32_SIB0;
unsigned x_UIMM8;
unsigned x_MODRM_RM_ENCODE_EA16_SIB0;
unsigned x_XOP_REXXB_ENC;
unsigned x_EVEX_MAP_ENC;
unsigned x_MEMDISP8;
unsigned x_MODRM_RM_ENCODE;
unsigned x_REX_PREFIX_ENC;
unsigned x_UIMM16;
unsigned x_VEX_TYPE_ENC;
unsigned x_EVEX_UPP_ENC;
unsigned x_VEX_REXR_ENC;
unsigned x_BRDISP32;
unsigned x_MEMDISP32;
unsigned x_MEMDISP16;
unsigned x_SIBINDEX_ENCODE;
unsigned x_SE_IMM8;
unsigned x_UIMM32;
unsigned x_SIMMz;
unsigned x_UIMMv;
unsigned x_EVEX_62_REXR_ENC;
unsigned x_DISP_NT;
unsigned x_MODRM_MOD_ENCODE;
unsigned x_MEMDISP;
unsigned x_VSIB_ENC_BASE;
unsigned x_BRDISP8;
unsigned x_BRDISPz;
unsigned x_EVEX_REXX_ENC;
unsigned x_XOP_TYPE_ENC;
};
struct XedEncoderVars {
struct XedEncoderIforms iforms;
unsigned short iform_index;
unsigned ilen;
unsigned olen;
unsigned bit_offset;
};
struct XedDecodedInst {
struct XedOperands operands;
unsigned char decoded_length;
uint8_t *bytes;
};
union XedAvxC4Payload1 {
struct {
unsigned map : 5;
unsigned b_inv : 1;
unsigned x_inv : 1;
unsigned r_inv : 1;
unsigned pad : 24;
} s;
unsigned u32;
};
union XedAvxC4Payload2 {
struct {
unsigned pp : 2;
unsigned l : 1;
unsigned vvv210 : 3;
unsigned v3 : 1;
unsigned w : 1;
unsigned pad : 24;
} s;
unsigned u32;
};
union XedAvxC5Payload {
struct {
unsigned pp : 2;
unsigned l : 1;
unsigned vvv210 : 3;
unsigned v3 : 1;
unsigned r_inv : 1;
unsigned pad : 24;
} s;
unsigned u32;
};
union XedAvx512Payload1 {
struct {
unsigned map : 4;
unsigned rr_inv : 1;
unsigned b_inv : 1;
unsigned x_inv : 1;
unsigned r_inv : 1;
unsigned pad : 24;
} s;
unsigned u32;
};
union XedAvx512Payload2 {
struct {
unsigned pp : 2;
unsigned ubit : 1;
unsigned vexdest210 : 3;
unsigned vexdest3 : 1;
unsigned rexw : 1;
unsigned pad : 24;
} s;
unsigned u32;
};
union XedAvx512Payload3 {
struct {
unsigned mask : 3;
unsigned vexdest4p : 1;
unsigned bcrc : 1;
unsigned llrc : 2;
unsigned z : 1;
unsigned pad : 24;
} s;
unsigned u32;
};
forceinline unsigned char xed_decoded_inst_get_byte(
const struct XedDecodedInst *p, long byte_index) {
return p->bytes[byte_index];
@ -575,7 +430,7 @@ forceinline struct XedDecodedInst *xed_decoded_inst_zero_set_mode(
extern const uint64_t xed_chip_features[XED_CHIP_LAST][3] hidden;
enum XedError xed_instruction_length_decode(struct XedDecodedInst *,
const unsigned char *, size_t);
const void *, size_t);
bool xed_isa_set_is_valid_for_chip(enum XedIsaSet, enum XedChip);
bool xed_test_chip_features(struct XedChipFeatures *, enum XedIsaSet);

View File

@ -21,6 +21,8 @@
#include "libc/macros.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "third_party/xed/avx.h"
#include "third_party/xed/avx512.h"
#include "third_party/xed/private.h"
#include "third_party/xed/x86.h"
@ -377,7 +379,7 @@ privileged static void xed_set_chip_modes(struct XedDecodedInst *d,
}
privileged static xed_bool_t xed3_mode_64b(struct XedDecodedInst *d) {
return d->operands.mode == 2;
return d->operands.mode == XED_MODE_LONG;
}
privileged static void xed_set_hint(char b, struct XedDecodedInst *d) {
@ -610,10 +612,10 @@ out:
d->operands.nseg_prefixes = nseg_prefixes;
d->operands.nrexes = nrexes;
if (rex) {
d->operands.rexw = (rex >> 3 & 1);
d->operands.rexr = (rex >> 2 & 1);
d->operands.rexx = (rex >> 1 & 1);
d->operands.rexb = (rex & 1);
d->operands.rexw = rex >> 3 & 1;
d->operands.rexr = rex >> 2 & 1;
d->operands.rexx = rex >> 1 & 1;
d->operands.rexb = rex & 1;
d->operands.rex = 1;
}
if (d->operands.mode_first_prefix) {
@ -773,7 +775,7 @@ privileged static void xed_evex_scanner(struct XedDecodedInst *d) {
d->operands.rexrr = ~evex1.s.rr_inv & 1;
}
d->operands.map = evex1.s.map;
d->operands.rexw = evex2.s.rexw;
d->operands.rexw = evex2.s.rexw & 1;
d->operands.vexdest3 = evex2.s.vexdest3;
d->operands.vexdest210 = evex2.s.vexdest210;
d->operands.ubit = evex2.s.ubit;
@ -894,7 +896,7 @@ privileged static void xed_vex_c4_scanner(struct XedDecodedInst *d) {
d->operands.rexr = ~c4byte1.s.r_inv & 1;
d->operands.rexx = ~c4byte1.s.x_inv & 1;
d->operands.rexb = (xed3_mode_64b(d) & ~c4byte1.s.b_inv) & 1;
d->operands.rexw = c4byte2.s.w;
d->operands.rexw = c4byte2.s.w & 1;
d->operands.vexdest3 = c4byte2.s.v3;
d->operands.vexdest210 = c4byte2.s.vvv210;
d->operands.vl = c4byte2.s.l;
@ -965,7 +967,7 @@ privileged static void xed_xop_scanner(struct XedDecodedInst *d) {
d->operands.rexr = ~xop_byte1.s.r_inv & 1;
d->operands.rexx = ~xop_byte1.s.x_inv & 1;
d->operands.rexb = (xed3_mode_64b(d) & ~xop_byte1.s.b_inv) & 1;
d->operands.rexw = xop_byte2.s.w;
d->operands.rexw = xop_byte2.s.w & 1;
d->operands.vexdest3 = xop_byte2.s.v3;
d->operands.vexdest210 = xop_byte2.s.vvv210;
d->operands.vl = xop_byte2.s.l;
@ -1041,7 +1043,7 @@ privileged static void xed_modrm_scanner(struct XedDecodedInst *d) {
length = d->decoded_length;
if (length < d->operands.max_bytes) {
b = xed_decoded_inst_get_byte(d, length);
d->operands.modrm_byte = b;
d->operands.modrm = b;
d->operands.pos_modrm = length;
d->decoded_length++;
mod = xed_modrm_mod(b);
@ -1072,9 +1074,7 @@ privileged static void xed_sib_scanner(struct XedDecodedInst *d) {
if (length < d->operands.max_bytes) {
b = xed_decoded_inst_get_byte(d, length);
d->operands.pos_sib = length;
d->operands.sibscale = xed_sib_scale(b);
d->operands.sibindex = xed_sib_index(b);
d->operands.sibbase = xed_sib_base(b);
d->operands.sib = b;
d->decoded_length++;
if (xed_sib_base(b) == 5) {
if (d->operands.mod == 0) {
@ -1134,9 +1134,9 @@ privileged static void XED_LF_BRDISP32_BRDISP_WIDTH_CONST_l2(
}
privileged static void XED_LF_DISP_BUCKET_0_l1(struct XedDecodedInst *x) {
if (x->operands.mode <= 1) {
if (x->operands.mode <= XED_MODE_LEGACY) {
XED_LF_BRDISPz_BRDISP_WIDTH_OSZ_NONTERM_EOSZ_l2(x);
} else if (x->operands.mode == 2) {
} else if (x->operands.mode == XED_MODE_LONG) {
XED_LF_BRDISP32_BRDISP_WIDTH_CONST_l2(x);
}
}
@ -1232,7 +1232,7 @@ privileged static void xed_decode_instruction_length(
* @see biggest code in gdb/clang/tensorflow binaries
*/
privileged enum XedError xed_instruction_length_decode(
struct XedDecodedInst *xedd, const uint8_t *itext, const size_t bytes) {
struct XedDecodedInst *xedd, const void *itext, size_t bytes) {
xed_set_chip_modes(xedd, xedd->operands.chip);
xedd->bytes = itext;
xedd->operands.max_bytes = MIN(bytes, XED_MAX_INSTRUCTION_BYTES);

View File

@ -154,19 +154,19 @@ xed_has_disp_regular.rodata:
.endobj xed_has_disp_regular.rodata
xed_has_sib_table.rodata:
.byte 36,0x00 # 0023 #
.byte 1,0x01 # 2424 $
.byte 7,0x00 # 252b %+
.byte 1,0x01 # 2c2c ,
.byte 7,0x00 # 2d33 3
.byte 1,0x01 # 3434 4
.byte 15,0x00 # 3543 5C
.byte 1,0x01 # 4444 D
.byte 7,0x00 # 454b EK
.byte 1,0x01 # 4c4c L
.byte 7,0x00 # 4d53 MS
.byte 1,0x01 # 5454 T
.byte 11,0x00 # 555f U_
.byte 36,FALSE # 0023 #
.byte 1,TRUE # 2424 $
.byte 7,FALSE # 252b %+
.byte 1,TRUE # 2c2c ,
.byte 7,FALSE # 2d33 3
.byte 1,TRUE # 3434 4
.byte 15,FALSE # 3543 5C
.byte 1,TRUE # 4444 D
.byte 7,FALSE # 454b EK
.byte 1,TRUE # 4c4c L
.byte 7,FALSE # 4d53 MS
.byte 1,TRUE # 5454 T
.byte 11,FALSE # 555f U_
.endobj xed_has_sib_table.rodata
xed_disp_bits_2d.rodata:

View File

@ -100,7 +100,7 @@ size_t internobj(struct Interner *t, const void *data, size_t size) {
item = data;
hash = max(1, KnuthMultiplicativeHash32(data, size));
do {
/* it is written that triangle probe halts iff i<n/2 && popcount(n)==1 */
/* it is written that triangle probe halts iff i<n/2 && popcnt(n)==1 */
i = (hash + step * (step + 1) / 2) & (it->n - 1);
if (it->p[i].hash == hash && it->p[i].index + size <= it->pool.n &&
memcmp(item, &it->pool.p[it->p[i].index], size) == 0) {

View File

@ -79,7 +79,7 @@ struct Edge {
};
struct Sources {
size_t i, n; /* phase 1: hashmap: popcount(n)==1 if n */
size_t i, n; /* phase 1: hashmap: popcnt(n)==1 if n */
struct Source *p; /* phase 2: arraylist sorted by id */
};

View File

@ -84,6 +84,7 @@ void RefactorFile(const char *path) {
char *mem, *spot = NULL, *part1, *part2;
CHECK_NE(-1, (fd = open(path, O_RDONLY)));
CHECK_NE(-1, fstat(fd, &st));
len2 = 0;
if ((len = st.st_size)) {
CHECK_NE(MAP_FAILED,
(mem = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0)));

View File

@ -145,6 +145,8 @@ int main(int argc, char *argv[]) {
SHOWOP(error);
SHOWOP(esrc);
SHOWOP(first_f2f3);
SHOWOP(modrm);
SHOWOP(sib);
SHOWOP(has_modrm);
SHOWOP(has_sib);
SHOWOP(hint);
@ -162,9 +164,6 @@ int main(int argc, char *argv[]) {
SHOWOP(mod);
SHOWOP(mode);
SHOWOP(mode_first_prefix);
SHOWOP(modep5);
SHOWOP(modep55c);
SHOWOP(modrm_byte);
SHOWOP(nominal_opcode);
SHOWOP(nprefixes);
SHOWOP(nrexes);
@ -189,9 +188,6 @@ int main(int argc, char *argv[]) {
SHOWOP(rexx);
SHOWOP(rm);
SHOWOP(seg_ovd);
SHOWOP(sibbase);
SHOWOP(sibindex);
SHOWOP(sibscale);
SHOWOP(srm);
SHOWOP(ubit);
SHOWOP(uimm0);

View File

@ -177,6 +177,7 @@ mode.\n\
#define CTRL(C) ((C) ^ 0100)
#define ALT(C) ((033 << 010) | (C))
#define ARGZ(...) ((char *const[]){__VA_ARGS__, NULL})
#define MOD(X, Y) ((X) - (ABS(Y)) * ((X) / ABS(Y)))
#define BALLOC(B, A, N, NAME) \
({ \
@ -509,8 +510,8 @@ static bool TrySpeaker(const char *prog, char *const *args) {
int rc;
int fds[3];
fds[0] = -1;
fds[1] = STDERR_FILENO;
fds[2] = STDERR_FILENO;
fds[1] = fileno(g_logfile);
fds[2] = fileno(g_logfile);
LOGF("spawning %s", prog);
if ((rc = spawnve(0, fds, prog, args, environ)) != -1) {
playpid_ = rc;
@ -541,8 +542,8 @@ static bool OpenSpeaker(void) {
if (!once) {
once = true;
i = 0;
if (sox_) tryspeakerfns_[i++] = TrySox;
if (ffplay_) tryspeakerfns_[i++] = TryFfplay;
if (sox_) tryspeakerfns_[i++] = TrySox;
}
snprintf(fifopath_, sizeof(fifopath_), "%s%s.%d.%d.wav", kTmpPath,
program_invocation_short_name, getpid(), count);