mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-22 21:32:31 +00:00
Make small fixes and oops ran clang-format on dtoa
This commit is contained in:
parent
b5b60015f5
commit
ac00be1a4e
47 changed files with 4933 additions and 5306 deletions
254
libc/bits/bits.h
254
libc/bits/bits.h
|
@ -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_
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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
Normal file
24
libc/bits/popcnt.h
Normal 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_ */
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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)))
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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; \
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
17
libc/tinymath/emodl.h
Normal file
17
libc/tinymath/emodl.h
Normal 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_ */
|
Loading…
Add table
Add a link
Reference in a new issue