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