Get Cosmopolitan into releasable state

A new rollup tool now exists for flattening out the headers in a way
that works better for our purposes than cpp. A lot of the API clutter
has been removed. APIs that aren't a sure thing in terms of general
recommendation are now marked internal.

There's now a smoke test for the amalgamation archive and gigantic
header file. So we can now guarantee you can use this project on the
easiest difficulty setting without the gigantic repository.

A website is being created, which is currently a work in progress:
https://justine.storage.googleapis.com/cosmopolitan/index.html
This commit is contained in:
Justine Tunney 2020-11-25 08:19:00 -08:00
parent dba7552c1e
commit ea0b5d9d1c
775 changed files with 6864 additions and 3963 deletions

View file

@ -12,7 +12,7 @@ void *bsearch(const void *, const void *, size_t, size_t,
void *bsearch_r(const void *, const void *, size_t, size_t,
int cmp(const void *, const void *, void *), void *)
paramsnonnull((1, 2, 5)) nothrow nosideeffect;
void djbsort(size_t n, int32_t[n]);
void djbsort(int32_t *, size_t);
void qsort(void *, size_t, size_t, int (*)(const void *, const void *))
paramsnonnull();
void qsort_r(void *, size_t, size_t,
@ -43,72 +43,14 @@ char16_t *concatstr16(const char16_t *, ...) nullterminated()
wchar_t *concatwcs(const wchar_t *, ...) nullterminated()
paramsnonnull() __algalloc;
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § algorithms » containers
*/
struct critbit0 {
void *root;
size_t count;
};
bool critbit0_contains(struct critbit0 *, const char *) nothrow nosideeffect
paramsnonnull();
bool critbit0_insert(struct critbit0 *, const char *) paramsnonnull();
bool critbit0_delete(struct critbit0 *, const char *) nothrow paramsnonnull();
void critbit0_clear(struct critbit0 *) nothrow paramsnonnull();
char *critbit0_get(struct critbit0 *, const char *);
intptr_t critbit0_allprefixed(struct critbit0 *, const char *,
intptr_t (*)(const char *, void *), void *)
paramsnonnull((1, 2, 3)) nothrow;
bool critbit0_emplace(struct critbit0 *, char *, size_t) paramsnonnull();
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § algorithms » comparators
*/
int cmpsb(/*const signed char[1]*/ const void *, const void *)
paramsnonnull() nothrow nocallback nosideeffect;
int cmpub(/*const unsigned char[1]*/ const void *, const void *)
paramsnonnull() nothrow nocallback nosideeffect;
int cmpsw(/*const signed short[1]*/ const void *, const void *)
paramsnonnull() nothrow nocallback nosideeffect;
int cmpuw(/*const unsigned short[1]*/ const void *, const void *)
paramsnonnull() nothrow nocallback nosideeffect;
int cmpsl(/*const signed int[1]*/ const void *, const void *)
paramsnonnull() nothrow nocallback nosideeffect;
int cmpul(/*const unsigned int[1]*/ const void *, const void *)
paramsnonnull() nothrow nocallback nosideeffect;
int cmpsq(/*const signed long[1]*/ const void *, const void *)
paramsnonnull() nothrow nocallback nosideeffect;
int cmpuq(/*const unsigned long[1]*/ const void *, const void *)
paramsnonnull() nothrow nocallback nosideeffect;
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § algorithms » generics
*/
#if __STDC_VERSION__ + 0 >= 201112
#define memmem(haystack, haystacklen, needle, needlelen) \
_Generic(*(haystack), wchar_t \
: wmemmem, char16_t \
: memmem16, default \
: memmem)(haystack, haystacklen, needle, needlelen)
#define replacestr(s, needle, replacement) \
_Generic(*(s), wchar_t \
: replacewcs, char16_t \
: replacestr16, default \
: replacestr)(s, needle, replacement)
#define concatstr(s, ...) \
_Generic(*(s), wchar_t \
: concatwcs, char16_t \
: concatstr16, default \
: concatstr)(s, __VA_ARGS__)
#endif /* C11 */
int cmpsb(const void *, const void *);
int cmpub(const void *, const void *);
int cmpsw(const void *, const void *);
int cmpuw(const void *, const void *);
int cmpsl(const void *, const void *);
int cmpul(const void *, const void *);
int cmpsq(const void *, const void *);
int cmpuq(const void *, const void *);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -28,20 +28,20 @@
#define append(ARRAYLIST, ITEM) concat((ARRAYLIST), (ITEM), 1)
#define concat(ARRAYLIST, ITEM, COUNT) \
({ \
autotype(ARRAYLIST) List = (ARRAYLIST); \
autotype(&List->p[0]) Item = (ITEM); \
size_t SizE = sizeof(*Item); \
size_t Count = (COUNT); \
size_t Idx = List->i; \
if (Idx + Count < List->n || grow(&List->p, &List->n, SizE, Count)) { \
memcpy(&List->p[Idx], Item, SizE *Count); \
atomic_store(&List->i, Idx + Count); \
} else { \
Idx = -1UL; \
} \
(ssize_t)(Idx); \
#define concat(ARRAYLIST, ITEM, COUNT) \
({ \
autotype(ARRAYLIST) List = (ARRAYLIST); \
autotype(&List->p[0]) Item = (ITEM); \
size_t SizE = sizeof(*Item); \
size_t Count = (COUNT); \
size_t Idx = List->i; \
if (Idx + Count < List->n || __grow(&List->p, &List->n, SizE, Count)) { \
memcpy(&List->p[Idx], Item, SizE *Count); \
atomic_store(&List->i, Idx + Count); \
} else { \
Idx = -1UL; \
} \
(ssize_t)(Idx); \
})
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -10,26 +10,26 @@ COSMOPOLITAN_C_START_
#define APPEND(LIST_P, LIST_I, LIST_N, ITEM) \
CONCAT(LIST_P, LIST_I, LIST_N, ITEM, 1)
#define CONCAT(LIST_P, LIST_I, LIST_N, ITEM, COUNT) \
({ \
autotype(LIST_P) ListP = (LIST_P); \
autotype(LIST_I) ListI = (LIST_I); \
autotype(LIST_N) ListN = (LIST_N); \
typeof(&(*ListP)[0]) Item = (ITEM); \
size_t SizE = sizeof(*Item); \
size_t Count = (COUNT); \
ssize_t Entry = -1; \
/* NOTE: We use `<` to guarantee one additional slot */ \
/* grow() will memset(0) extended memory, thus */ \
/* you get a nul-terminator for free sometimes */ \
/* the exception is if you list.i=0 and re-use */ \
/* so you need concat(...); list.p[list.i++]=0 */ \
if (*ListI + Count < *ListN || grow(ListP, ListN, SizE, Count)) { \
memcpy(&(*ListP)[*ListI], Item, (SizE) * (Count)); \
Entry = *ListI; \
*ListI += Count; /* happens after copy in case signal */ \
} \
Entry; \
#define CONCAT(LIST_P, LIST_I, LIST_N, ITEM, COUNT) \
({ \
autotype(LIST_P) ListP = (LIST_P); \
autotype(LIST_I) ListI = (LIST_I); \
autotype(LIST_N) ListN = (LIST_N); \
typeof(&(*ListP)[0]) Item = (ITEM); \
size_t SizE = sizeof(*Item); \
size_t Count = (COUNT); \
ssize_t Entry = -1; \
/* NOTE: We use `<` to guarantee one additional slot */ \
/* grow() will memset(0) extended memory, thus */ \
/* you get a nul-terminator for free sometimes */ \
/* the exception is if you list.i=0 and re-use */ \
/* so you need concat(...); list.p[list.i++]=0 */ \
if (*ListI + Count < *ListN || __grow(ListP, ListN, SizE, Count)) { \
memcpy(&(*ListP)[*ListI], Item, (SizE) * (Count)); \
Entry = *ListI; \
*ListI += Count; /* happens after copy in case signal */ \
} \
Entry; \
})
COSMOPOLITAN_C_END_

View file

@ -3,13 +3,8 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/**
* Floor binary search of low 32-bits of 64-bit array items.
*
* This is well-suited to NexGen-32e, requiring less than 32 bytes of
* code. It's particularly useful for frozen maps, requiring less effort
* and memory than a perfect hash table.
*/
/* TODO: DELETE */
forceinline int32_t bisectcarleft(const int32_t (*cons)[2], size_t count,
const int32_t key) {
size_t left = 0;

View file

@ -1,27 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_ALG_BISECTLEFT_H_
#define COSMOPOLITAN_LIBC_ALG_BISECTLEFT_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
forceinline size_t bisectleft(const void *key, const void *base, size_t count,
size_t sz, int cmp(const void *, const void *)) {
size_t m, l, r;
l = 0;
r = count;
while (l < r) {
m = (l + r) >> 1;
if (cmp((char *)base + m * sz, key) < 0) {
l = m + 1;
} else {
r = m;
}
}
if (l && (l == count || cmp((char *)base + l * sz, key) > 0)) {
l--;
}
return l;
}
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ALG_BISECTLEFT_H_ */

View file

@ -1,26 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_ALG_BISECTRIGHT_H_
#define COSMOPOLITAN_LIBC_ALG_BISECTRIGHT_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
forceinline size_t bisectright(const void *key, const void *base, size_t count,
size_t sz, int cmp(const void *, const void *)) {
size_t left = 0;
size_t right = count;
while (left < right) {
size_t m = (right + right) >> 1;
if (cmp((char *)base + m * sz, key) > 0) {
right = m + 1;
} else {
right = m;
}
}
if (right && (right == count || cmp((char *)base + right * sz, key) > 0)) {
right--;
}
return right;
}
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ALG_BISECTRIGHT_H_ */

View file

@ -18,7 +18,7 @@
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/bisect.h"
#include "libc/alg/bisect.internal.h"
/**
* Searches sorted array for exact item in logarithmic time.

View file

@ -18,7 +18,7 @@
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/bisect.h"
#include "libc/alg/bisect.internal.h"
/**
* Searches sorted array for exact item in logarithmic time.

27
libc/alg/critbit0.h Normal file
View file

@ -0,0 +1,27 @@
#ifndef COSMOPOLITAN_LIBC_ALG_CRITBIT0_H_
#define COSMOPOLITAN_LIBC_ALG_CRITBIT0_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § data structures » critical bit tree (for c strings)
*/
struct critbit0 {
void *root;
size_t count;
};
bool critbit0_contains(struct critbit0 *, const char *) nothrow nosideeffect
paramsnonnull();
bool critbit0_insert(struct critbit0 *, const char *) paramsnonnull();
bool critbit0_delete(struct critbit0 *, const char *) nothrow paramsnonnull();
void critbit0_clear(struct critbit0 *) nothrow paramsnonnull();
char *critbit0_get(struct critbit0 *, const char *);
intptr_t critbit0_allprefixed(struct critbit0 *, const char *,
intptr_t (*)(const char *, void *), void *)
paramsnonnull((1, 2, 3)) nothrow;
bool critbit0_emplace(struct critbit0 *, char *, size_t) paramsnonnull();
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ALG_CRITBIT0_H_ */

View file

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/critbit0.h"
#include "libc/alg/internal.h"
#include "libc/str/str.h"

View file

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/critbit0.h"
#include "libc/alg/internal.h"
#include "libc/mem/mem.h"

View file

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/critbit0.h"
#include "libc/alg/internal.h"
#include "libc/str/str.h"

View file

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/critbit0.h"
#include "libc/alg/internal.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"

View file

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/critbit0.h"
#include "libc/alg/internal.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"

View file

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/critbit0.h"
#include "libc/alg/internal.h"
#include "libc/str/str.h"

View file

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/critbit0.h"
#include "libc/alg/internal.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"

View file

@ -1,34 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_ALG_INSERTIONSORT_H_
#define COSMOPOLITAN_LIBC_ALG_INSERTIONSORT_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#define siftbackwards(C, X, V, i) \
do { \
autotype(V) V_ = (V); \
for (autotype(i) j = (i); j && C(&V_[j - 1], &V_[j]) > 0; --j) { \
X(&V_[j - 1], &V_[j]); \
} \
} while (0)
#if 0
/**
* Tiny in-place quadratic sorting algorithm.
*
* The only advantage to insertion sort is saving on code size when
* there's a strong level of certainty the array won't have more than
* sixteen items. Sometimes Insertion Sort is favored for sorting data
* that's almost sorted. SmoothSort should be a better choice (see
* qsort()) since it has that advantage and a linearithmic worst-case.
*/
#endif
#define INSERTIONSORT(C, X, A, n) \
do { \
autotype(A) A_ = (A); \
autotype(n) n_ = (n); \
for (autotype(n) i = 1; i < n_; ++i) { \
siftbackwards(C, X, A_, i); \
} \
} while (0)
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ALG_INSERTIONSORT_H_ */

View file

@ -18,8 +18,8 @@
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/arraylist2.h"
#include "libc/bits/safemacros.h"
#include "libc/alg/arraylist2.internal.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"

View file

@ -18,14 +18,15 @@
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/arraylist2.h"
#include "libc/bits/safemacros.h"
#include "libc/alg/arraylist2.internal.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"
#undef strlen
#undef replacestr
#define replacestr replacestr16
#define memmem memmem16
#define char char16_t
#define strlen strlen16
#include "libc/alg/replacestr.c"

View file

@ -3,16 +3,14 @@
#include "libc/bits/xchg.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#if 0
/**
* Reverses array.
*
* @param ARRAY is a typed array or a pointer to one
* @param ARRAY is a typed array or a pointer to one
* @param COUNT is the number of items
* @return pointer to start of array
* @see ARRAYLEN()
*/
#endif
#define reverse(ARRAY, COUNT) \
({ \
autotype(&(ARRAY)[0]) Array = (ARRAY); \

View file

@ -3,7 +3,6 @@
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#include "libc/bits/xchg.h"
#if 0
/**
* Fisher-Yates shuffle.
*
@ -12,7 +11,6 @@
* @param n is the number of items in A
* @see ARRAYLEN()
*/
#endif
#define shuffle(R, A, n) \
do { \
autotype(A) Array = (A); \

View file

@ -1,6 +1,6 @@
#ifndef COSMOPOLITAN_LIBC_BITS_AVX2INTRIN_H_
#define COSMOPOLITAN_LIBC_BITS_AVX2INTRIN_H_
#include "libc/bits/avxintrin.h"
#include "libc/bits/avxintrin.internal.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_

View file

@ -1,28 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_BITS_BIGWORD_H_
#define COSMOPOLITAN_LIBC_BITS_BIGWORD_H_
#if 0
/**
* Let BIGWORD be the the number of bytes in the largest cpu register
* available within the instruction set architecture requirements chosen
* at compile-time.
*
* In plainer terms, if you tune with flags like -mavx, you're not just
* giving the compiler permission to generate code that's incompatible
* with older computers; you're also asking Cosmopolitan to systemically
* change alignment, vectoring, buffering, ABIs, memory allocation, etc.
*/
#endif
#ifndef BIGWORD
#if __AVX512F__ + 0
#define BIGWORD 64
#elif __AVX2__ + 0
#define BIGWORD 32
#elif __SSE2__ + 0
#define BIGWORD 16
#else
#define BIGWORD __BIGGEST_ALIGNMENT__
#endif
#endif /*BIGWORD*/
#endif /* COSMOPOLITAN_LIBC_BITS_BIGWORD_H_ */

View file

@ -0,0 +1,16 @@
#ifndef COSMOPOLITAN_LIBC_BITS_BIGWORD_H_
#define COSMOPOLITAN_LIBC_BITS_BIGWORD_H_
#ifndef BIGWORD
#if __AVX512F__ + 0
#define BIGWORD 64
#elif __AVX2__ + 0
#define BIGWORD 32
#elif __SSE2__ + 0
#define BIGWORD 16
#else
#define BIGWORD __BIGGEST_ALIGNMENT__
#endif
#endif /*BIGWORD*/
#endif /* COSMOPOLITAN_LIBC_BITS_BIGWORD_H_ */

View file

@ -1,5 +1,5 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
@ -17,15 +17,8 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/macros.h"
#include "libc/bits/bits.h"
bitreverse16:
push %rbx
mov %edi,%eax
mov $kReverseBits,%ebx
xlat
xchg %al,%ah
xlat
pop %rbx
ret
.endfn bitreverse16,globl
uint16_t(bitreverse16)(uint16_t x) {
return kReverseBits[x & 0x00FF] << 8 | kReverseBits[(x & 0xFF00) >> 8];
}

View file

@ -1,5 +1,5 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
@ -17,15 +17,8 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/macros.h"
#include "libc/bits/bits.h"
bitreverse8:
.leafprologue
.profilable
push %rbx
mov %edi,%eax
mov $kReverseBits,%ebx
xlat
pop %rbx
.leafepilogue
.endfn bitreverse8,globl
uint8_t(bitreverse8)(uint8_t x) {
return kReverseBits[x];
}

View file

@ -9,42 +9,26 @@ COSMOPOLITAN_C_START_
cosmopolitan § bits
*/
extern const bool kTrue;
extern const bool kFalse;
extern const uint8_t kReverseBits[256];
uint32_t gray(uint32_t) pureconst;
uint32_t ungray(uint32_t) pureconst;
unsigned bcdadd(unsigned, unsigned) pureconst;
unsigned long bcd2i(unsigned long) pureconst;
unsigned long i2bcd(unsigned long) pureconst;
void bcxcpy(unsigned char (*)[16], unsigned long);
int ffs(int) pureconst;
int ffsl(long int) pureconst;
int ffsll(long long int) pureconst;
int fls(int) pureconst;
int flsl(long int) pureconst;
int flsll(long long int) pureconst;
uint8_t bitreverse8(uint8_t) libcesque pureconst;
uint16_t bitreverse16(uint16_t) libcesque pureconst;
uint32_t bitreverse32(uint32_t) libcesque pureconst;
uint64_t bitreverse64(uint64_t) libcesque pureconst;
unsigned long roundup2pow(unsigned long) libcesque pureconst;
unsigned long roundup2log(unsigned long) libcesque pureconst;
unsigned long rounddown2pow(unsigned long) libcesque pureconst;
unsigned long hamming(unsigned long, unsigned long) pureconst;
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § bits » no assembly required
*/
/**
* Undocumented incantations for ROR, ROL, and SAR.
*/
#define ROR(w, k) (CheckUnsigned(w) >> (k) | (w) << (sizeof(w) * 8 - (k)))
#define ROL(w, k) ((w) << (k) | CheckUnsigned(w) >> (sizeof(w) * 8 - (k)))
#define SAR(w, k) (((w) & ~(~0u >> (k))) | ((w) >> ((k) & (sizeof(w) * 8 - 1))))
#define bitreverse8(X) (kReverseBits[(uint8_t)(X)])
#define bitreverse16(X) \
((uint16_t)kReverseBits[(uint8_t)(X)] << 010 | \
@ -253,49 +237,6 @@ unsigned long hamming(unsigned long, unsigned long) pureconst;
Val; \
})
/**
* Returns true if bit is set in memory.
*
* This is a generically-typed Bitset<T> RAM. This macro is intended
* to be container-like with optimal machine instruction encoding, cf.
* machine-agnostic container abstractions. Memory accesses are words.
* Register allocation can be avoided if BIT is known. Be careful when
* casting character arrays since that should cause a page fault.
*
* @param MEM is uint𝑘_t[] where 𝑘 {16,32,64} base address
* @param BIT [-(2**(𝑘-1)),2**(𝑘-1)) is zero-based index
* @return true if bit is set, otherwise false
* @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_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 */
#define btr(MEM, BIT) __BitOp("btr", BIT, MEM) /** bit test and reset */
#define btc(MEM, BIT) __BitOp("btc", BIT, MEM) /** bit test and complement */

View file

@ -1,7 +1,7 @@
#ifndef COSMOPOLITAN_LIBC_BITS_EMMINTRIN_H_
#define COSMOPOLITAN_LIBC_BITS_EMMINTRIN_H_
#include "libc/bits/progn.h"
#include "libc/bits/xmmintrin.h"
#include "libc/bits/progn.internal.h"
#include "libc/bits/xmmintrin.internal.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
/*───────────────────────────────────────────────────────────────────────────│─╗

View file

@ -17,14 +17,8 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/bits.h"
uint32_t gray(uint32_t x) { return x ^ (x >> 1); }
uint32_t ungray(uint32_t x) {
x ^= x >> 16;
x ^= x >> 8;
x ^= x >> 4;
x ^= x >> 2;
x ^= x >> 1;
return x;
uint32_t gray(uint32_t x) {
return x ^ (x >> 1);
}

View file

@ -1,48 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 sw=8 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify │
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License. │
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of │
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software │
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/macros.h"
/ Converts integer to binary-coded decimal.
i2bcd: .leafprologue
.profilable
test %rdi,%rdi
je 2f
mov %rdi,%rsi
xor %r9d,%r9d
mov $0xcccccccccccccccd,%r8
xor %ecx,%ecx
1: mov %rsi,%rax
mul %r8
shr $3,%rdx
lea (%rdx,%rdx),%rax
lea (%rax,%rax,4),%rax
mov %rsi,%rdi
sub %rax,%rdi
shl %cl,%rdi
add %rdi,%r9
add $4,%rcx
cmp $9,%rsi
mov %rdx,%rsi
ja 1b
jmp 3f
2: xor %r9d,%r9d
3: mov %r9,%rax
.leafepilogue
.endfn i2bcd,globl

View file

@ -2,6 +2,8 @@
#define COSMOPOLITAN_LIBC_BITS_INITIALIZER_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
/* TODO: DELETE */
/**
* Teleports code fragment inside _init().
*/

View file

@ -1,7 +1,7 @@
#ifndef COSMOPOLITAN_LIBC_BITS_SHAINTRIN_H_
#define COSMOPOLITAN_LIBC_BITS_SHAINTRIN_H_
#include "libc/bits/emmintrin.h"
#include "libc/bits/xmmintrin.h"
#include "libc/bits/emmintrin.internal.h"
#include "libc/bits/xmmintrin.internal.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#define _mm_sha1rnds4_epu32(M128I_0, M128I_1, MEM) \

View file

@ -1,6 +1,6 @@
#ifndef COSMOPOLITAN_LIBC_BITS_TMMINTRIN_H_
#define COSMOPOLITAN_LIBC_BITS_TMMINTRIN_H_
#include "libc/bits/emmintrin.h"
#include "libc/bits/emmintrin.internal.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
/*───────────────────────────────────────────────────────────────────────────│─╗

View file

@ -1,14 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_BITS_TYPECHECK_H_
#define COSMOPOLITAN_LIBC_BITS_TYPECHECK_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#define TYPECHECK(T, X) \
({ \
T Lol1; \
typeof(X) Lol2; \
(void)(&Lol1 == &Lol2); \
X; \
})
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_BITS_TYPECHECK_H_ */

View file

@ -1,5 +1,5 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 sw=8 fenc=utf-8 :vi
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
@ -17,22 +17,13 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/macros.h"
#include "libc/bits/bits.h"
/ Performs addition on binary-coded decimals.
bcdadd: .leafprologue
.profilable
lea 0x6666666(%rdi),%ecx
xor %esi,%ecx
lea (%rdi,%rsi),%eax
add $0x6666666,%eax
xor %eax,%ecx
not %ecx
and $0x11111110,%ecx
mov %ecx,%edx
shr $2,%edx
shr $3,%ecx
orl %edx,%ecx
sub %ecx,%eax
.leafepilogue
.endfn bcdadd,globl
uint32_t ungray(uint32_t x) {
x ^= x >> 16;
x ^= x >> 8;
x ^= x >> 4;
x ^= x >> 2;
x ^= x >> 1;
return x;
}

View file

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
/**
* Subtracts unsigned integers w/ wraparound.

View file

@ -1,6 +1,6 @@
#ifndef COSMOPOLITAN_LIBC_BITS_WMMINTRIN_H_
#define COSMOPOLITAN_LIBC_BITS_WMMINTRIN_H_
#include "libc/bits/emmintrin.h"
#include "libc/bits/emmintrin.internal.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
#define _mm_clmulepi64_si128(X, Y, IMM) \

View file

@ -1,8 +1,8 @@
#ifndef COSMOPOLITAN_LIBC_BITS_XMMINTRIN_H_
#define COSMOPOLITAN_LIBC_BITS_XMMINTRIN_H_
#include "libc/bits/emmintrin.h"
#include "libc/bits/mmintrin.h"
#include "libc/bits/progn.h"
#include "libc/bits/emmintrin.internal.h"
#include "libc/bits/mmintrin.internal.h"
#include "libc/bits/progn.internal.h"
#include "libc/dce.h"
#define _MM_EXCEPT_MASK 0x003f

View file

@ -226,7 +226,6 @@ uint32_t getsid(int) nosideeffect;
uint32_t gettid(void) nosideeffect;
uint32_t getuid(void) nosideeffect;
uint32_t umask(int32_t);
void *getprocaddressmodule(const char *, const char *);
#define getcwd(BUF, SIZE) \
(isconstant(BUF) && (&(BUF)[0] == NULL) ? get_current_dir_name() \

View file

@ -18,7 +18,7 @@
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/struct/timespec.h"

View file

@ -17,14 +17,13 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/progn.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/progn.internal.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/conv/conv.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/nt/ntdll.h"
#include "libc/runtime/missioncritical.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/ok.h"

View file

@ -19,7 +19,7 @@
*/
#include "libc/assert.h"
#include "libc/bits/bits.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/errno.h"

View file

@ -19,6 +19,7 @@
*/
#include "libc/calls/internal.h"
#include "libc/nt/enum/filemapflags.h"
#include "libc/nt/enum/pageflags.h"
#include "libc/nt/memory.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"

View file

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/internal.h"
#include "libc/calls/struct/stat.h"
#include "libc/conv/conv.h"
@ -32,8 +32,8 @@
#include "libc/sysv/consts/s.h"
textwindows int fstat$nt(int64_t handle, struct stat *st) {
int filetype;
uint64_t actualsize;
enum NtFileType filetype;
struct NtByHandleFileInformation wst;
struct NtFileCompressionInfo fci;
if (GetFileInformationByHandle(handle, &wst)) {

View file

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/initializer.h"
#include "libc/bits/initializer.internal.h"
#include "libc/bits/pushpop.h"
#include "libc/calls/internal.h"
#include "libc/nt/runtime.h"

View file

@ -18,7 +18,7 @@
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"

View file

@ -22,21 +22,20 @@
#include "libc/calls/internal.h"
#include "libc/dce.h"
#include "libc/nt/process.h"
#include "libc/runtime/missioncritical.h"
#include "libc/runtime/runtime.h"
static int g_pid;
static void __updatepid(void) {
g_pid = __getpid();
static int __get_pid(void) {
if (!IsWindows()) {
return getpid$sysv();
} else {
return GetCurrentProcessId();
}
}
int __getpid(void) {
if (!IsWindows()) {
return GETPID();
} else {
return NtGetPid();
}
static void __update_pid(void) {
g_pid = __get_pid();
}
/**
@ -46,8 +45,8 @@ int __getpid(void) {
int getpid(void) {
static bool once;
if (!once) {
__updatepid();
atfork(__updatepid, NULL);
__update_pid();
atfork(__update_pid, NULL);
once = true;
}
return g_pid;

View file

@ -20,8 +20,9 @@
#include "libc/bits/bits.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/kntprioritycombos.h"
#include "libc/calls/kntprioritycombos.internal.h"
#include "libc/conv/conv.h"
#include "libc/nexgen32e/ffs.h"
#include "libc/nt/enum/processcreationflags.h"
#include "libc/nt/enum/threadpriority.h"
#include "libc/nt/process.h"

View file

@ -17,12 +17,12 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/calls/internal.h"
#include "libc/nt/dll.h"
#include "libc/calls/calls.h"
/**
* Returns address of function in a DLL that's already loaded.
*/
textwindows void *getprocaddressmodule(const char *module, const char *symbol) {
textwindows void *GetProcAddressModule(const char *module, const char *symbol) {
return GetProcAddress(GetModuleHandle(module), symbol);
}

View file

@ -24,6 +24,8 @@
#include "libc/nt/createfile.h"
#include "libc/nt/enum/accessmask.h"
#include "libc/nt/enum/creationdisposition.h"
#include "libc/nt/enum/fileflagandattributes.h"
#include "libc/nt/enum/filesharemode.h"
#include "libc/nt/files.h"
#include "libc/nt/runtime.h"
#include "libc/sysv/consts/at.h"

View file

@ -18,7 +18,7 @@
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/bits/progn.h"
#include "libc/bits/progn.internal.h"
#include "libc/calls/internal.h"
#include "libc/calls/struct/dirent.h"
#include "libc/dce.h"

View file

@ -18,7 +18,7 @@
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/str/str.h"

View file

@ -28,7 +28,6 @@
#include "libc/nt/process.h"
#include "libc/nt/runtime.h"
#include "libc/runtime/memtrack.h"
#include "libc/runtime/missioncritical.h"
#include "libc/runtime/runtime.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"

View file

@ -17,10 +17,10 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/calls/calls.h"
#include "libc/sysv/errfuns.h"
/**

View file

@ -17,16 +17,17 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/arraylist2.h"
#include "libc/alg/arraylist2.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/hefty/ntspawn.h"
#include "libc/conv/conv.h"
#include "libc/macros.h"
#include "libc/mem/mem.h"
#include "libc/nexgen32e/hascharacter.h"
#include "libc/nexgen32e/hascharacter.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/str/oldutf16.internal.h"
#include "libc/str/str.h"
#include "libc/str/tpdecode.h"
#include "libc/str/tpdecode.internal.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/errfuns.h"

View file

@ -17,13 +17,14 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/alg/arraylist2.h"
#include "libc/alg/arraylist2.internal.h"
#include "libc/calls/hefty/ntspawn.h"
#include "libc/conv/conv.h"
#include "libc/macros.h"
#include "libc/mem/mem.h"
#include "libc/str/oldutf16.internal.h"
#include "libc/str/str.h"
#include "libc/str/tpdecode.h"
#include "libc/str/tpdecode.internal.h"
#include "libc/sysv/errfuns.h"
/**

View file

@ -1,6 +1,6 @@
#ifndef COSMOPOLITAN_LIBC_CALLS_HEFTY_MKVARARGV_H_
#define COSMOPOLITAN_LIBC_CALLS_HEFTY_MKVARARGV_H_
#include "libc/alg/arraylist2.h"
#include "libc/alg/arraylist2.internal.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_

View file

@ -18,9 +18,9 @@
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/arraylist.h"
#include "libc/alg/arraylist.internal.h"
#include "libc/bits/bits.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/hefty/ntspawn.h"
#include "libc/calls/internal.h"
@ -28,7 +28,6 @@
#include "libc/nt/enum/processcreationflags.h"
#include "libc/nt/process.h"
#include "libc/nt/runtime.h"
#include "libc/runtime/missioncritical.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"

View file

@ -17,11 +17,11 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/calls/calls.h"
/**
* Replaces tilde in path w/ user home folder.

View file

@ -18,10 +18,10 @@
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/alg/arraylist.h"
#include "libc/alg/arraylist.internal.h"
#include "libc/calls/hefty/ntspawn.h"
#include "libc/dce.h"
#include "libc/nexgen32e/tinystrcmp.h"
#include "libc/nexgen32e/tinystrcmp.internal.h"
#include "libc/str/str.h"
static int CompareStrings(const char *l, const char *r) {

View file

@ -21,7 +21,6 @@
#include "libc/calls/hefty/mkvarargv.h"
#include "libc/calls/hefty/spawn.h"
#include "libc/mem/mem.h"
#include "libc/runtime/missioncritical.h"
#include "libc/runtime/runtime.h"
/**

View file

@ -23,6 +23,7 @@
#include "libc/calls/hefty/ntspawn.h"
#include "libc/calls/hefty/spawn.h"
#include "libc/calls/internal.h"
#include "libc/nt/enum/processcreationflags.h"
#include "libc/nt/enum/startf.h"
#include "libc/nt/files.h"
#include "libc/nt/ipc.h"

View file

@ -24,6 +24,7 @@
#include "libc/fmt/fmt.h"
#include "libc/log/log.h"
#include "libc/nt/console.h"
#include "libc/nt/enum/consolemodeflags.h"
#include "libc/str/str.h"
#include "libc/sysv/errfuns.h"

View file

@ -196,7 +196,6 @@ void *mremap$sysv(void *, u64, u64, i32, void *) hidden;
cosmopolitan § syscalls » system five » support
*/
int __getpid(void) hidden;
void __onfork(void) hidden;
bool32 __sigenter(i32, struct siginfo *, struct ucontext *) hidden;
i32 fixupnewfd$sysv(i32, i32) hidden;
@ -246,7 +245,6 @@ int symlink$nt(const char *, const char *) hidden;
int sysinfo$nt(struct sysinfo *) hidden;
int truncate$nt(const char *, u64) hidden;
int unlink$nt(const char *) hidden;
int wait4$nt(int, int *, int, struct rusage *) hidden;
i64 lseek$nt(int, i64, int) hidden;
ssize_t read$nt(struct Fd *, const struct iovec *, size_t, ssize_t) hidden;
ssize_t write$nt(struct Fd *, const struct iovec *, size_t, ssize_t) hidden;
@ -260,6 +258,7 @@ int nanosleep$nt(const struct timespec *, struct timespec *) hidden;
*/
void WinMainForked(void) hidden;
void *GetProcAddressModule(const char *, const char *) hidden;
int getsetpriority$nt(int, unsigned, int, int (*)(int));
void ntcontext2linux(struct ucontext *, const struct NtContext *) hidden;
struct NtOverlapped *offset2overlap(int64_t, struct NtOverlapped *) hidden;

View file

@ -18,8 +18,8 @@
02110-1301 USA
*/
#include "libc/calls/internal.h"
#include "libc/calls/struct/metatermios.h"
#include "libc/calls/termios-internal.h"
#include "libc/calls/struct/metatermios.internal.h"
#include "libc/calls/termios.internal.h"
#include "libc/sysv/consts/termios.h"
int ioctl$tcgets$nt(int, struct termios *);

View file

@ -18,9 +18,10 @@
02110-1301 USA
*/
#include "libc/calls/internal.h"
#include "libc/calls/struct/metatermios.h"
#include "libc/calls/termios-internal.h"
#include "libc/calls/struct/metatermios.internal.h"
#include "libc/calls/termios.internal.h"
#include "libc/nt/console.h"
#include "libc/nt/enum/consolemodeflags.h"
#include "libc/nt/enum/version.h"
#include "libc/nt/struct/teb.h"
#include "libc/sysv/consts/o.h"

View file

@ -18,8 +18,8 @@
02110-1301 USA
*/
#include "libc/calls/internal.h"
#include "libc/calls/struct/metatermios.h"
#include "libc/calls/termios-internal.h"
#include "libc/calls/struct/metatermios.internal.h"
#include "libc/calls/termios.internal.h"
#include "libc/dce.h"
#include "libc/sysv/consts/termios.h"

View file

@ -1,20 +1,20 @@
#ifndef COSMOPOLITAN_LIBC_CALLS_IOCTL_H_
#define COSMOPOLITAN_LIBC_CALLS_IOCTL_H_
#include "libc/macros.h"
#include "libc/sysv/consts/termios.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § system calls » input output control
cosmopolitan § system calls » ioctl
*/
int ioctl(int, uint64_t, void *);
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § system calls » input output control » undiamonding
cosmopolitan § system calls » ioctl » undiamonding (size optimization)
*/
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#include "libc/macros.h"
#include "libc/sysv/consts/termios.h"
#define ioctl(FD, REQUEST, MEMORY) ioctl$dispatch(FD, REQUEST, MEMORY)

View file

@ -18,13 +18,13 @@
02110-1301 USA
*/
#include "libc/alg/alg.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/conv/conv.h"
#include "libc/dce.h"
#include "libc/log/log.h"
#include "libc/nexgen32e/vendor.h"
#include "libc/nexgen32e/vendor.internal.h"
#include "libc/nt/struct/teb.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"

View file

@ -40,10 +40,15 @@
* @asyncsignalsafe
*/
int kill(int pid, int sig) {
if (pid == getpid()) return raise(sig);
int me;
if (!IsWindows()) {
return kill$sysv(pid, sig, 1);
} else {
return enosys();
me = getpid();
if (!pid || pid == me || pid == -me) {
return raise(sig);
} else {
return enosys();
}
}
}

View file

@ -18,7 +18,7 @@
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/calls/kntprioritycombos.h"
#include "libc/calls/kntprioritycombos.internal.h"
#include "libc/limits.h"
#include "libc/macros.h"
#include "libc/nt/enum/processcreationflags.h"

View file

@ -19,6 +19,7 @@
*/
#include "libc/calls/internal.h"
#include "libc/macros.h"
#include "libc/nt/enum/offerpriority.h"
#include "libc/nt/memory.h"
#include "libc/nt/runtime.h"
#include "libc/nt/struct/memoryrangeentry.h"
@ -31,7 +32,7 @@ forceinline typeof(PrefetchVirtualMemory) *GetPrefetchVirtualMemory(void) {
if (!once) {
once = true;
PrefetchVirtualMemory_ = /* win8.1+ */
getprocaddressmodule("KernelBase.dll", "PrefetchVirtualMemory");
GetProcAddressModule("KernelBase.dll", "PrefetchVirtualMemory");
}
return PrefetchVirtualMemory_;
}
@ -42,7 +43,7 @@ forceinline typeof(OfferVirtualMemory) *GetOfferVirtualMemory(void) {
if (!once) {
once = true;
OfferVirtualMemory_ = /* win8.1+ */
getprocaddressmodule("KernelBase.dll", "OfferVirtualMemory");
GetProcAddressModule("KernelBase.dll", "OfferVirtualMemory");
}
return OfferVirtualMemory_;
}

View file

@ -17,10 +17,11 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/calls/ntmagicpaths.h"
#include "libc/nexgen32e/tinystrcmp.h"
#include "libc/calls/ntmagicpaths.internal.h"
#include "libc/nexgen32e/tinystrcmp.internal.h"
#include "libc/str/oldutf16.internal.h"
#include "libc/str/str.h"
#include "libc/str/tpdecode.h"
#include "libc/str/tpdecode.internal.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/errfuns.h"

View file

@ -22,6 +22,7 @@
#include "libc/macros.h"
#include "libc/nexgen32e/nexgen32e.h"
#include "libc/sock/internal.h"
#include "libc/sock/select.internal.h"
int nanosleep$xnu(const struct timespec *req, struct timespec *rem) {
long millis;

View file

@ -17,12 +17,14 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/conv/conv.h"
#include "libc/sysv/consts/prio.h"
static int clamp(int p) { return max(-NZERO, min(NZERO - 1, p)); }
static int clamp(int p) {
return max(-NZERO, min(NZERO - 1, p));
}
/**
* Changes process priority.

View file

@ -18,8 +18,8 @@
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/bits/initializer.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/initializer.internal.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/dce.h"
#include "libc/nexgen32e/rdtsc.h"

View file

@ -20,6 +20,7 @@
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/nt/enum/accessmask.h"
#include "libc/nt/enum/securityimpersonationlevel.h"
#include "libc/nt/enum/securityinformation.h"
#include "libc/nt/errors.h"
#include "libc/nt/files.h"

View file

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/calls/ntmagicpaths.h"
#include "libc/calls/ntmagicpaths.internal.h"
const struct NtMagicPaths kNtMagicPaths = {
#define TAB(NAME, STRING) STRING,

View file

@ -19,9 +19,12 @@
*/
#include "libc/assert.h"
#include "libc/calls/internal.h"
#include "libc/calls/ntmagicpaths.h"
#include "libc/nexgen32e/tinystrcmp.h"
#include "libc/calls/ntmagicpaths.internal.h"
#include "libc/nexgen32e/tinystrcmp.internal.h"
#include "libc/nt/createfile.h"
#include "libc/nt/enum/accessmask.h"
#include "libc/nt/enum/creationdisposition.h"
#include "libc/nt/enum/fileflagandattributes.h"
#include "libc/nt/enum/filesharemode.h"
#include "libc/nt/enum/filetype.h"
#include "libc/nt/enum/fsctl.h"

View file

@ -21,12 +21,14 @@
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/conv/conv.h"
#include "libc/conv/isslash.h"
#include "libc/conv/isslash.internal.h"
#include "libc/conv/itoa.h"
#include "libc/dce.h"
#include "libc/nt/createfile.h"
#include "libc/nt/enum/accessmask.h"
#include "libc/nt/enum/creationdisposition.h"
#include "libc/nt/enum/fileflagandattributes.h"
#include "libc/nt/enum/filesharemode.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/o.h"

View file

@ -1,34 +0,0 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=8 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#ifndef COSMOPOLITAN_LIBC_COMPAT_OPENBSD_H_
#define COSMOPOLITAN_LIBC_COMPAT_OPENBSD_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § system calls » openbsd
*/
typedef unsigned char u_char;
int pledge(const char *promises, const char *execpromises);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_COMPAT_OPENBSD_H_ */

View file

@ -0,0 +1,15 @@
#ifndef COSMOPOLITAN_LIBC_COMPAT_OPENBSD_H_
#define COSMOPOLITAN_LIBC_COMPAT_OPENBSD_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § system calls » openbsd
*/
typedef unsigned char u_char;
int pledge(const char *promises, const char *execpromises);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_COMPAT_OPENBSD_H_ */

View file

@ -18,7 +18,7 @@
02110-1301 USA
*/
#include "libc/bits/bits.h"
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/struct/iovec.h"

View file

@ -17,7 +17,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/struct/iovec.h"

View file

@ -19,7 +19,23 @@
*/
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/runtime/missioncritical.h"
#include "libc/nt/console.h"
#include "libc/nt/enum/ctrlevent.h"
#include "libc/nt/runtime.h"
#include "libc/sysv/consts/sig.h"
static uint32_t GetCtrlEvent(int sig) {
switch (sig) {
case SIGINT:
return kNtCtrlCEvent;
case SIGHUP:
return kNtCtrlCloseEvent;
case SIGQUIT:
return kNtCtrlBreakEvent;
default:
ExitProcess(128 + sig);
}
}
/**
* Sends signal to this process.
@ -28,4 +44,23 @@
* @return 0 on success or -1 w/ errno
* @asyncsignalsafe
*/
int raise(int sig) { return RAISE(sig); }
int raise(int sig) {
if (sig == SIGTRAP) {
DebugBreak();
return 0;
}
if (sig == SIGFPE) {
volatile int x = 0;
x = 1 / x;
return 0;
}
if (!IsWindows()) {
return kill$sysv(getpid(), sig, 1);
} else {
if (GenerateConsoleCtrlEvent(GetCtrlEvent(sig), 0)) {
return 0;
} else {
return winerr();
}
}
}

View file

@ -18,6 +18,7 @@
02110-1301 USA
*/
#include "libc/calls/internal.h"
#include "libc/nt/enum/movefileexflags.h"
#include "libc/nt/files.h"
#include "libc/nt/runtime.h"
#include "libc/str/str.h"

View file

@ -17,11 +17,13 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/dce.h"
#include "libc/limits.h"
#include "libc/nt/enum/processaccess.h"
#include "libc/nt/enum/threadaccess.h"
#include "libc/nt/process.h"
#include "libc/nt/runtime.h"
#include "libc/nt/thread.h"
@ -64,7 +66,6 @@ static textwindows noinline int sched_setaffinity$nt(int pid,
*
* @param pid is the process or thread id (or 0 for caller)
* @param bitsetsize is byte length of bitset
* @param bitset can be manipulated using bt(), bts(), etc.
* @return 0 on success, or -1 w/ errno
*/
int sched_setaffinity(int pid, uint64_t bitsetsize, const void *bitset) {

View file

@ -17,9 +17,9 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "libc/bits/safemacros.h"
#include "libc/bits/safemacros.internal.h"
#include "libc/calls/internal.h"
#include "libc/calls/kntprioritycombos.h"
#include "libc/calls/kntprioritycombos.internal.h"
#include "libc/nt/process.h"
#include "libc/nt/runtime.h"
#include "libc/nt/thread.h"

View file

@ -20,10 +20,10 @@
#include "libc/bits/bits.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/struct/sigaction-freebsd.h"
#include "libc/calls/struct/sigaction-linux.h"
#include "libc/calls/struct/sigaction-openbsd.h"
#include "libc/calls/struct/sigaction-xnu.h"
#include "libc/calls/struct/sigaction-freebsd.internal.h"
#include "libc/calls/struct/sigaction-linux.internal.h"
#include "libc/calls/struct/sigaction-openbsd.internal.h"
#include "libc/calls/struct/sigaction-xnu.internal.h"
#include "libc/calls/struct/sigaction.h"
#include "libc/calls/typedef/sigaction_f.h"
#include "libc/calls/ucontext.h"
@ -63,7 +63,7 @@ union metasigaction {
static void sigaction$cosmo2native(union metasigaction *sa) {
if (!sa) return;
switch (hostos) {
switch (__hostos) {
case LINUX:
SWITCHEROO(sa->cosmo, sa->linux, sa_handler, sa_flags, sa_restorer,
sa_mask);
@ -87,7 +87,7 @@ static void sigaction$cosmo2native(union metasigaction *sa) {
static void sigaction$native2cosmo(union metasigaction *sa) {
if (!sa) return;
switch (hostos) {
switch (__hostos) {
case LINUX:
SWITCHEROO(sa->linux, sa->cosmo, sa_handler, sa_flags, sa_restorer,
sa_mask);

View file

@ -18,13 +18,20 @@
02110-1301 USA
*/
#include "libc/calls/sigbits.h"
#include "libc/calls/calls.h"
#include "libc/sysv/errfuns.h"
/**
* Adds sig to set.
* Adds signal to set.
*
* @return true, false, or -1 w/ errno
* @error EINVAL
* @asyncsignalsafe
*/
int(sigaddset)(sigset_t *set, int sig) { return sigaddset(set, sig); }
int sigaddset(sigset_t *set, int sig) {
unsigned i = sig - 1;
if (i < sizeof(set->__bits) * 8) {
set->__bits[i >> 6] |= 1ull << (i & 63);
return 0;
} else {
return einval();
}
}

View file

@ -1,52 +1,14 @@
#ifndef COSMOPOLITAN_LIBC_CALLS_SIGBITS_H_
#define COSMOPOLITAN_LIBC_CALLS_SIGBITS_H_
#include "libc/bits/bits.h"
#include "libc/bits/progn.h"
#include "libc/str/str.h"
#include "libc/calls/struct/sigset.h"
#include "libc/sysv/errfuns.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct sigset;
int sigaddset(struct sigset *, int) paramsnonnull();
int sigdelset(struct sigset *, int) paramsnonnull();
int sigemptyset(struct sigset *) paramsnonnull();
int sigfillset(struct sigset *) paramsnonnull();
int sigismember(const struct sigset *, int) paramsnonnull() nosideeffect;
#define sigemptyset(SET) \
({ \
memset((SET), 0, sizeof(struct sigset)); \
0; \
})
#define sigfillset(SET) \
({ \
memset((SET), -1, sizeof(struct sigset)); \
0; \
})
#define sigaddset(SET, SIG) \
({ \
int Sig = (SIG); \
__IsValidSignal(Sig) ? PROGN(bts((SET)->sig, Sig - 1), 0) : einval(); \
})
#define sigdelset(SET, SIG) \
({ \
int Sig = (SIG); \
__IsValidSignal(Sig) ? PROGN(btr((SET)->sig, Sig - 1), 0) : einval(); \
})
#define sigismember(SET, SIG) \
({ \
int Sig = (SIG); \
__IsValidSignal(Sig) ? bt((SET)->sig, Sig - 1) : einval(); \
})
#define __IsValidSignal(SIG) (1 <= (SIG) && (SIG) <= NSIG)
int sigaddset(sigset_t *, int) paramsnonnull();
int sigdelset(sigset_t *, int) paramsnonnull();
int sigemptyset(sigset_t *) paramsnonnull();
int sigfillset(sigset_t *) paramsnonnull();
int sigismember(const sigset_t *, int) paramsnonnull() nosideeffect;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

Some files were not shown because too many files have changed in this diff Show more