Create ELF aliases for identical symbols

This change greatly reduces the number of modules that need to be
compiled. The only issue right now is that sometimes when viewing
symbol table entries, the aliased symbol is chosen.
This commit is contained in:
Justine Tunney 2023-06-06 03:30:37 -07:00
parent e1b83399bd
commit b8a6a989c0
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
191 changed files with 414 additions and 2190 deletions

View file

@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
strategy: strategy:
matrix: matrix:
mode: ["", tiny, rel, aarch64] mode: ["", tiny, rel]
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v3

View file

@ -1744,14 +1744,14 @@ static char *sstrdup(const char *p) {
return memcpy(stalloc(len), p, len); return memcpy(stalloc(len), p, len);
} }
int xwrite(int, const void *, uint64_t); int __xwrite(int, const void *, uint64_t);
static void flushout(struct output *dest) { static void flushout(struct output *dest) {
unsigned len; unsigned len;
len = dest->nextc - dest->buf; len = dest->nextc - dest->buf;
if (!len || dest->fd < 0) return; if (!len || dest->fd < 0) return;
dest->nextc = dest->buf; dest->nextc = dest->buf;
if ((xwrite(dest->fd, dest->buf, len))) dest->flags |= OUTPUT_ERR; if ((__xwrite(dest->fd, dest->buf, len))) dest->flags |= OUTPUT_ERR;
} }
static void flushall(void) { static void flushall(void) {
@ -1817,7 +1817,7 @@ static void outmem(const char *p, unsigned len, struct output *dest) {
} }
nleft = dest->end - dest->nextc; nleft = dest->end - dest->nextc;
if (nleft > len) goto buffered; if (nleft > len) goto buffered;
if ((xwrite(dest->fd, p, len))) { if ((__xwrite(dest->fd, p, len))) {
dest->flags |= OUTPUT_ERR; dest->flags |= OUTPUT_ERR;
} }
} }
@ -9179,7 +9179,7 @@ static int64_t openhere(union node *redir) {
} }
len = strlen(p); len = strlen(p);
if (len <= PIPESIZE) { if (len <= PIPESIZE) {
xwrite(pip[1], p, len); __xwrite(pip[1], p, len);
goto out; goto out;
} }
if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) { if (forkshell((struct job *)NULL, (union node *)NULL, FORK_NOJOB) == 0) {
@ -9188,7 +9188,7 @@ static int64_t openhere(union node *redir) {
signal(SIGQUIT, SIG_IGN); signal(SIGQUIT, SIG_IGN);
signal(SIGHUP, SIG_IGN); signal(SIGHUP, SIG_IGN);
signal(SIGPIPE, SIG_DFL); signal(SIGPIPE, SIG_DFL);
xwrite(pip[1], p, len); __xwrite(pip[1], p, len);
_exit(0); _exit(0);
} }
out: out:

View file

@ -65,3 +65,5 @@ int fadvise(int fd, uint64_t offset, uint64_t len, int advice) {
STRACE("fadvise(%d, %'lu, %'lu, %d) → %d% m", fd, offset, len, advice, rc); STRACE("fadvise(%d, %'lu, %'lu, %d) → %d% m", fd, offset, len, advice, rc);
return rc; return rc;
} }
__strong_reference(fadvise, fadvise64);

View file

@ -1,23 +0,0 @@
/*-*- 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
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
fadvise64:
jmp fadvise
.endfn fadvise64,globl

View file

@ -53,3 +53,5 @@ int fstat(int fd, struct stat *st) {
STRACE("fstat(%d, [%s]) → %d% m", fd, DescribeStat(rc, st), rc); STRACE("fstat(%d, [%s]) → %d% m", fd, DescribeStat(rc, st), rc);
return rc; return rc;
} }
__strong_reference(fstat, fstat64);

View file

@ -1,23 +0,0 @@
/*-*- 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
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
fstat64:
jmp fstat
.endfn fstat64,globl

View file

@ -75,3 +75,5 @@ int fstatat(int dirfd, const char *path, struct stat *st, int flags) {
DescribeStat(rc, st), __strace_fstatat_flags(alloca(12), flags), rc); DescribeStat(rc, st), __strace_fstatat_flags(alloca(12), flags), rc);
return rc; return rc;
} }
__strong_reference(fstatat, fstatat64);

View file

@ -1,31 +0,0 @@
/*-*- 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
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/macros.internal.h"
fstatat64:
jmp fstatat
.endfn fstatat64,globl

View file

@ -87,3 +87,5 @@ int ftruncate(int fd, int64_t length) {
STRACE("ftruncate(%d, %'ld) → %d% m", fd, length, rc); STRACE("ftruncate(%d, %'ld) → %d% m", fd, length, rc);
return rc; return rc;
} }
__strong_reference(ftruncate, ftruncate64);

View file

@ -1,31 +0,0 @@
/*-*- 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
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/macros.internal.h"
ftruncate64:
jmp ftruncate
.endfn ftruncate64,globl

View file

@ -90,3 +90,5 @@ int64_t lseek(int fd, int64_t offset, int whence) {
rc); rc);
return rc; return rc;
} }
__strong_reference(lseek, lseek64);

View file

@ -1,23 +0,0 @@
/*-*- 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
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
lseek64:
jmp lseek
.endfn lseek64,globl

View file

@ -26,3 +26,5 @@
int lstat(const char *pathname, struct stat *st) { int lstat(const char *pathname, struct stat *st) {
return fstatat(AT_FDCWD, pathname, st, AT_SYMLINK_NOFOLLOW); return fstatat(AT_FDCWD, pathname, st, AT_SYMLINK_NOFOLLOW);
} }
__strong_reference(lstat, lstat64);

View file

@ -1,31 +0,0 @@
/*-*- 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
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/macros.internal.h"
lstat64:
jmp lstat
.endfn lstat64,globl

View file

@ -43,3 +43,5 @@ int open(const char *file, int flags, ...) {
va_end(va); va_end(va);
return openat(AT_FDCWD, file, flags, mode); return openat(AT_FDCWD, file, flags, mode);
} }
__strong_reference(open, open64);

View file

@ -1,22 +0,0 @@
/*-*- 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
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
open64: jmp open
.endfn open64,globl

View file

@ -83,3 +83,5 @@ ssize_t pread(int fd, void *buf, size_t size, int64_t offset) {
MAX(0, MIN(40, rc)), buf, rc > 40 ? "..." : "", size, offset, rc); MAX(0, MIN(40, rc)), buf, rc > 40 ? "..." : "", size, offset, rc);
return rc; return rc;
} }
__strong_reference(pread, pread64);

View file

@ -1,23 +0,0 @@
/*-*- 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
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
pread64:
jmp pread
.endfn pread64,globl

View file

@ -80,3 +80,5 @@ ssize_t pwrite(int fd, const void *buf, size_t size, int64_t offset) {
MAX(0, MIN(40, rc)), buf, rc > 40 ? "..." : "", size, offset, rc); MAX(0, MIN(40, rc)), buf, rc > 40 ? "..." : "", size, offset, rc);
return rc; return rc;
} }
__strong_reference(pwrite, pwrite64);

View file

@ -1,23 +0,0 @@
/*-*- 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
Copyright 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
pwrite64:
jmp pwrite
.endfn pwrite64,globl

View file

@ -38,3 +38,5 @@
int stat(const char *path, struct stat *st) { int stat(const char *path, struct stat *st) {
return fstatat(AT_FDCWD, path, st, 0); return fstatat(AT_FDCWD, path, st, 0);
} }
__strong_reference(stat, stat64);

View file

@ -1,30 +0,0 @@
/*-*- 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
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include "libc/macros.internal.h"
stat64: jmp stat
.endfn stat64,globl

View file

@ -7,6 +7,7 @@
#include "libc/runtime/ezmap.internal.h" #include "libc/runtime/ezmap.internal.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0) #if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_ COSMOPOLITAN_C_START_
#ifdef COSMO
/*───────────────────────────────────────────────────────────────────────────│─╗ /*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § executable & linkable format cosmopolitan § executable & linkable format
@ -29,6 +30,7 @@ const char *GetElfSectionName(const Elf64_Ehdr *, size_t, Elf64_Shdr *);
Elf64_Sym *GetElfDynSymbolTable(const Elf64_Ehdr *, size_t, Elf64_Xword *); Elf64_Sym *GetElfDynSymbolTable(const Elf64_Ehdr *, size_t, Elf64_Xword *);
char *GetElfDynStringTable(const Elf64_Ehdr *, size_t); char *GetElfDynStringTable(const Elf64_Ehdr *, size_t);
#endif /* COSMO */
COSMOPOLITAN_C_END_ COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ELF_H_ */ #endif /* COSMOPOLITAN_LIBC_ELF_H_ */

View file

@ -48,3 +48,5 @@ long atol(const char *s) {
} }
return x; return x;
} }
__weak_reference(atol, atoll);

View file

@ -1,31 +0,0 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/limits.h"
/**
* Decodes decimal number from ASCII string.
*
* @param s is a non-null nul-terminated string
* @return the decoded signed saturated integer
*/
long long atoll(const char *s) {
_Static_assert(LONG_MAX == LONG_LONG_MAX, "need atoll impl");
return atol(s);
}

View file

@ -18,6 +18,19 @@
*/ */
#include "libc/fmt/conv.h" #include "libc/fmt/conv.h"
/**
* Divides integers yielding numerator and denominator.
*/
div_t(div)(int num, int den) { div_t(div)(int num, int den) {
return div(num, den); div_t retval;
retval.quot = num / den;
retval.rem = num % den;
#if __STDC_VERSION__ + 0 < 199901L
// satisfy quot*denominator+rem == numerator
if (n > 0 && retval.rem < 0) {
retval.quot += 1;
retval.rem -= d;
}
#endif
return retval;
} }

View file

@ -1,24 +0,0 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/macros.internal.h"
int128_t i128abs(int128_t x) {
return ABS(x);
}

View file

@ -1,24 +0,0 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/macros.internal.h"
intmax_t imaxabs(intmax_t x) {
return ABS(x);
}

View file

@ -1,32 +0,0 @@
/*-*- 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 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
imaxdiv_t imaxdiv(intmax_t n, intmax_t d) {
imaxdiv_t retval;
retval.quot = n / d;
retval.rem = n % d;
// satisfy quot*denominator+rem == numerator
if (n > 0 && retval.rem < 0) {
retval.quot += 1;
retval.rem -= d;
}
return (retval);
}

View file

@ -2,6 +2,26 @@
#define COSMOPOLITAN_LIBC_FMT_ITOA_H_ #define COSMOPOLITAN_LIBC_FMT_ITOA_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0) #if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_ COSMOPOLITAN_C_START_
#ifdef COSMO
#define LengthInt64 __LengthInt64
#define LengthUint64 __LengthUint64
#define LengthInt64Thousands __LengthInt64Thousands
#define LengthUint64Thousands __LengthUint64Thousands
#define FormatInt32 __FormatInt32
#define FormatUint32 __FormatUint32
#define FormatInt64 __FormatInt64
#define FormatUint64 __FormatUint64
#define FormatInt64Thousands __FormatInt64Thousands
#define FormatUint64Thousands __FormatUint64Thousands
#define FormatOctal32 __FormatOctal32
#define FormatOctal64 __FormatOctal64
#define FormatBinary64 __FormatBinary64
#define FormatHex64 __FormatHex64
#define FormatFlex64 __FormatFlex64
#define uint64toarray_radix16 __uint64toarray_radix16
#define uint64toarray_fixed16 __uint64toarray_fixed16
#define uint64toarray_radix8 __uint64toarray_radix8
unsigned LengthInt64(int64_t) pureconst; unsigned LengthInt64(int64_t) pureconst;
unsigned LengthUint64(uint64_t) pureconst; unsigned LengthUint64(uint64_t) pureconst;
@ -27,6 +47,7 @@ size_t int128toarray_radix10(int128_t, char *);
size_t uint128toarray_radix10(uint128_t, char *); size_t uint128toarray_radix10(uint128_t, char *);
#endif #endif
#endif /* COSMO */
COSMOPOLITAN_C_END_ COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_FMT_ITOA_H_ */ #endif /* COSMOPOLITAN_LIBC_FMT_ITOA_H_ */

View file

@ -18,6 +18,22 @@
*/ */
#include "libc/fmt/conv.h" #include "libc/fmt/conv.h"
/**
* Divides integers yielding numerator and denominator.
*/
ldiv_t(ldiv)(long num, long den) { ldiv_t(ldiv)(long num, long den) {
return ldiv(num, den); ldiv_t retval;
retval.quot = num / den;
retval.rem = num % den;
#if __STDC_VERSION__ + 0 < 199901L
// satisfy quot*denominator+rem == numerator
if (n > 0 && retval.rem < 0) {
retval.quot += 1;
retval.rem -= d;
}
#endif
return retval;
} }
__weak_reference(ldiv, lldiv);
__weak_reference(ldiv, imaxdiv);

View file

@ -2,6 +2,17 @@
#define COSMOPOLITAN_LIBC_FMT_LEB128_H_ #define COSMOPOLITAN_LIBC_FMT_LEB128_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0) #if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_ COSMOPOLITAN_C_START_
#ifdef COSMO
#define sleb64 __sleb64
#define zleb64 __zleb64
#define uleb64 __uleb64
#define unzleb64 __unzleb64
#define unuleb64 __unuleb64
#define sleb128 __sleb128
#define zleb128 __zleb128
#define uleb128 __uleb128
#define unsleb128 __unsleb128
char *sleb64(char *, int64_t); char *sleb64(char *, int64_t);
char *zleb64(char[hasatleast 10], int64_t); char *zleb64(char[hasatleast 10], int64_t);
@ -16,6 +27,7 @@ char *uleb128(char *, uint128_t);
int unsleb128(const void *, size_t, int128_t *); int unsleb128(const void *, size_t, int128_t *);
#endif /* __STRICT_ANSI__ */ #endif /* __STRICT_ANSI__ */
#endif /* COSMO */
COSMOPOLITAN_C_END_ COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_FMT_LEB128_H_ */ #endif /* COSMOPOLITAN_LIBC_FMT_LEB128_H_ */

View file

@ -1,23 +0,0 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
lldiv_t(lldiv)(long long num, long long den) {
return lldiv(num, den);
}

View file

@ -1,61 +0,0 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/errno.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/strtol.internal.h"
#include "libc/limits.h"
#include "libc/str/str.h"
#include "libc/str/tab.internal.h"
/**
* Decodes intmax_t from ASCII string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char
* following the last one this function processed, which is usually
* the NUL byte, or in the case of invalid strings, would point to
* the first invalid character
* @param base can be anywhere between [2,36] or 0 to auto-detect based
* on the the prefixes 0 (octal), 0x (hexadecimal), 0b (binary), or
* decimal (base 10) by default
* @return decoded saturated integer
* @see strtoumax()
*/
intmax_t strtoimax(const char *s, char **endptr, int base) {
char t = 0;
int d, c = *s;
intmax_t x = 0;
CONSUME_SPACES(s, c);
GET_SIGN(s, c, d);
GET_RADIX(s, c, base);
if ((c = kBase36[c & 255]) && --c < base) {
if (!((t |= 1) & 2)) {
do {
if (__builtin_mul_overflow(x, base, &x) ||
__builtin_add_overflow(x, c * d, &x)) {
x = d > 0 ? INTMAX_MAX : INTMAX_MIN;
errno = ERANGE;
t |= 2;
}
} while ((c = kBase36[*++s & 255]) && --c < base);
}
}
if (t && endptr) *endptr = s;
return x;
}

View file

@ -70,3 +70,7 @@ long strtol(const char *s, char **endptr, int base) {
if (t && endptr) *endptr = s; if (t && endptr) *endptr = s;
return x; return x;
} }
__weak_reference(strtol, strtoimax);
__weak_reference(strtol, strtoll);
__weak_reference(strtol, strtoll_l);

View file

@ -1,38 +0,0 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/limits.h"
/**
* Decodes signed integer from ASCII string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char
* following the last one this function processed, which is usually
* the NUL byte, or in the case of invalid strings, would point to
* the first invalid character
* @param base can be anywhere between [2,36] or 0 to auto-detect based
* on the the prefixes 0 (octal), 0x (hexadecimal), 0b (binary), or
* decimal (base 10) by default
* @return decoded signed saturated integer
*/
long long strtoll(const char *s, char **endptr, int base) {
_Static_assert(LONG_MAX == LONG_LONG_MAX, "need strtoll impl");
return strtol(s, endptr, base);
}

View file

@ -1,24 +0,0 @@
/*-*- 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 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/str/locale.h"
long long strtoll_l(const char *nptr, char **endptr, int base, locale_t l) {
return strtoll(nptr, endptr, base);
}

View file

@ -58,3 +58,7 @@ unsigned long strtoul(const char *s, char **endptr, int base) {
if (t && endptr) *endptr = s; if (t && endptr) *endptr = s;
return d > 0 ? x : -x; return d > 0 ? x : -x;
} }
__weak_reference(strtoul, strtoumax);
__weak_reference(strtoul, strtoull);
__weak_reference(strtoul, strtoull_l);

View file

@ -1,38 +0,0 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/limits.h"
/**
* Decodes unsigned integer from ASCII string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char
* following the last one this function processed, which is usually
* the NUL byte, or in the case of invalid strings, would point to
* the first invalid character
* @param base can be anywhere between [2,36] or 0 to auto-detect based
* on the the prefixes 0 (octal), 0x (hexadecimal), 0b (binary), or
* decimal (base 10) by default
* @return decoded integer mod 2 negated if leading `-`
*/
unsigned long long strtoull(const char *s, char **endptr, int base) {
_Static_assert(LONG_MAX == LONG_LONG_MAX, "need strtoull impl");
return strtoul(s, endptr, base);
}

View file

@ -1,25 +0,0 @@
/*-*- 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 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/str/locale.h"
unsigned long long strtoull_l(const char *nptr, char **endptr, int base,
locale_t l) {
return strtoull(nptr, endptr, base);
}

View file

@ -1,54 +0,0 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/fmt/strtol.internal.h"
#include "libc/str/str.h"
#include "libc/str/tab.internal.h"
/**
* Decodes uintmax_t from ASCII string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char
* following the last one this function processed, which is usually
* the NUL byte, or in the case of invalid strings, would point to
* the first invalid character
* @param base can be anywhere between [2,36] or 0 to auto-detect based
* on the the prefixes 0 (octal), 0x (hexadecimal), 0b (binary), or
* decimal (base 10) by default
* @return decoded integer mod 2¹² negated if leading `-`
* @see strtoimax()
*/
uintmax_t strtoumax(const char *s, char **endptr, int base) {
char t = 0;
int d, c = *s;
uintmax_t x = 0;
CONSUME_SPACES(s, c);
GET_SIGN(s, c, d);
GET_RADIX(s, c, base);
if ((c = kBase36[c & 255]) && --c < base) {
t |= 1;
do {
x *= base;
x += c;
} while ((c = kBase36[*++s & 255]) && --c < base);
}
if (t && endptr) *endptr = s;
return d > 0 ? x : -x;
}

View file

@ -1,25 +0,0 @@
/*-*- 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 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/str/locale.h"
#include "libc/str/str.h"
#include "libc/thread/tls.h"
int wcscoll(const wchar_t *p, const wchar_t *q) {
return wcscoll_l(p, q, (locale_t)__get_tls()->tib_locale);
}

View file

@ -1,61 +0,0 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/errno.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/strtol.internal.h"
#include "libc/limits.h"
#include "libc/str/str.h"
#include "libc/str/tab.internal.h"
/**
* Decodes intmax_t from wide string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char
* following the last one this function processed, which is usually
* the NUL byte, or in the case of invalid strings, would point to
* the first invalid character
* @param base can be anywhere between [2,36] or 0 to auto-detect based
* on the the prefixes 0 (octal), 0x (hexadecimal), 0b (binary), or
* decimal (base 10) by default
* @return decoded saturated integer
* @see strtoumax()
*/
intmax_t wcstoimax(const wchar_t *s, wchar_t **endptr, int base) {
char t = 0;
intmax_t x = 0;
int d, c = *s;
CONSUME_SPACES(s, c);
GET_SIGN(s, c, d);
GET_RADIX(s, c, base);
if ((c = kBase36[c & 255]) && --c < base) {
if (!((t |= 1) & 2)) {
do {
if (__builtin_mul_overflow(x, base, &x) ||
__builtin_add_overflow(x, c * d, &x)) {
x = d > 0 ? INTMAX_MAX : INTMAX_MIN;
errno = ERANGE;
t |= 2;
}
} while ((c = kBase36[*++s & 255]) && --c < base);
}
}
if (t && endptr) *endptr = s;
return x;
}

View file

@ -58,3 +58,7 @@ long wcstol(const wchar_t *s, wchar_t **endptr, int base) {
if (t && endptr) *endptr = s; if (t && endptr) *endptr = s;
return x; return x;
} }
__weak_reference(wcstol, wcstoimax);
__weak_reference(wcstol, wcstoll);
__weak_reference(wcstol, wcstoll_l);

View file

@ -1,60 +0,0 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/errno.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/strtol.internal.h"
#include "libc/limits.h"
#include "libc/str/str.h"
#include "libc/str/tab.internal.h"
/**
* Decodes signed long long integer from wide string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char
* following the last one this function processed, which is usually
* the NUL byte, or in the case of invalid strings, would point to
* the first invalid character
* @param base can be anywhere between [2,36] or 0 to auto-detect based
* on the the prefixes 0 (octal), 0x (hexadecimal), 0b (binary), or
* decimal (base 10) by default
* @return the decoded signed saturated number
*/
long long wcstoll(const wchar_t *s, wchar_t **endptr, int base) {
char t = 0;
int d, c = *s;
long long x = 0;
CONSUME_SPACES(s, c);
GET_SIGN(s, c, d);
GET_RADIX(s, c, base);
if ((c = kBase36[c & 255]) && --c < base) {
if (!((t |= 1) & 2)) {
do {
if (__builtin_mul_overflow(x, base, &x) ||
__builtin_add_overflow(x, c * d, &x)) {
x = d > 0 ? LONG_LONG_MAX : LONG_LONG_MIN;
errno = ERANGE;
t |= 2;
}
} while ((c = kBase36[*++s & 255]) && --c < base);
}
}
if (t && endptr) *endptr = s;
return x;
}

View file

@ -1,25 +0,0 @@
/*-*- 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 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/str/locale.h"
long long wcstoll_l(const wchar_t *nptr, wchar_t **endptr, int base,
locale_t l) {
return wcstoll(nptr, endptr, base);
}

View file

@ -57,3 +57,7 @@ unsigned long wcstoul(const wchar_t *s, wchar_t **endptr, int base) {
if (t && endptr) *endptr = s; if (t && endptr) *endptr = s;
return d > 0 ? x : -x; return d > 0 ? x : -x;
} }
__weak_reference(wcstoul, wcstoumax);
__weak_reference(wcstoul, wcstoull);
__weak_reference(wcstoul, wcstoull_l);

View file

@ -1,54 +0,0 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/errno.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/strtol.internal.h"
#include "libc/str/str.h"
#include "libc/str/tab.internal.h"
/**
* Decodes unsigned long long integer from wide string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char
* following the last one this function processed, which is usually
* the NUL byte, or in the case of invalid strings, would point to
* the first invalid character
* @param base can be anywhere between [2,36] or 0 to auto-detect based
* on the the prefixes 0 (octal), 0x (hexadecimal), 0b (binary), or
* decimal (base 10) by default
* @return decoded integer mod 2 negated if leading `-`
*/
unsigned long long wcstoull(const wchar_t *s, wchar_t **endptr, int base) {
char t = 0;
int d, c = *s;
unsigned long long x = 0;
CONSUME_SPACES(s, c);
GET_SIGN(s, c, d);
GET_RADIX(s, c, base);
if ((c = kBase36[c & 255]) && --c < base) {
t |= 1;
do {
x *= base;
x += c;
} while ((c = kBase36[*++s & 255]) && --c < base);
}
if (t && endptr) *endptr = s;
return d > 0 ? x : -x;
}

View file

@ -1,25 +0,0 @@
/*-*- 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 2022 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/str/locale.h"
unsigned long long wcstoull_l(const wchar_t *nptr, wchar_t **endptr, int base,
locale_t l) {
return wcstoull(nptr, endptr, base);
}

View file

@ -1,54 +0,0 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/fmt/strtol.internal.h"
#include "libc/str/str.h"
#include "libc/str/tab.internal.h"
/**
* Decodes uintmax_t from wide string.
*
* @param s is a non-null nul-terminated string
* @param endptr if non-null will always receive a pointer to the char
* following the last one this function processed, which is usually
* the NUL byte, or in the case of invalid strings, would point to
* the first invalid character
* @param base can be anywhere between [2,36] or 0 to auto-detect based
* on the the prefixes 0 (octal), 0x (hexadecimal), 0b (binary), or
* decimal (base 10) by default
* @return decoded integer mod 2¹² negated if leading `-`
* @see strtoimax()
*/
uintmax_t wcstoumax(const wchar_t *s, wchar_t **endptr, int base) {
char t = 0;
int d, c = *s;
uintmax_t x = 0;
CONSUME_SPACES(s, c);
GET_SIGN(s, c, d);
GET_RADIX(s, c, base);
if ((c = kBase36[c & 255]) && --c < base) {
t |= 1;
do {
x *= base;
x += c;
} while ((c = kBase36[*++s & 255]) && --c < base);
}
if (t && endptr) *endptr = s;
return d > 0 ? x : -x;
}

View file

@ -859,9 +859,10 @@ void abort(void) wontreturn;
#define __strong_reference(sym, aliassym) \ #define __strong_reference(sym, aliassym) \
extern typeof(sym) aliassym __attribute__((__alias__(#sym))) extern typeof(sym) aliassym __attribute__((__alias__(#sym)))
#define __weak_reference(sym, alias) \ #define __weak_reference(sym, alias) \
asm(".weak\t" #alias "\n\t" \ asm(".weak\t" #alias "\n\t" \
".equ\t" #alias ", " #sym) ".equ\t" #alias ", " #sym "\n\t" \
".type\t" #alias ",@notype")
#define __funline \ #define __funline \
extern __inline \ extern __inline \

View file

@ -28,6 +28,7 @@
#include "libc/intrin/aarch64/asmdefs.internal.h" #include "libc/intrin/aarch64/asmdefs.internal.h"
#define __strchr_aarch64 strchr #define __strchr_aarch64 strchr
#define __index_aarch64 index
.ident "\n\n\ .ident "\n\n\
Optimized Routines (MIT License)\n\ Optimized Routines (MIT License)\n\
@ -77,6 +78,7 @@ Copyright 2022 ARM Limited\n"
/* Locals and temporaries. */ /* Locals and temporaries. */
ENTRY_ALIAS (__strchr_aarch64, __index_aarch64)
ENTRY (__strchr_aarch64) ENTRY (__strchr_aarch64)
PTR_ARG (0) PTR_ARG (0)
/* Magic constant 0xc0300c03 to allow us to identify which lane /* Magic constant 0xc0300c03 to allow us to identify which lane

View file

@ -28,6 +28,7 @@
#include "libc/intrin/aarch64/asmdefs.internal.h" #include "libc/intrin/aarch64/asmdefs.internal.h"
#define __strrchr_aarch64 strrchr #define __strrchr_aarch64 strrchr
#define __rindex_aarch64 rindex
.ident "\n\n\ .ident "\n\n\
Optimized Routines (MIT License)\n\ Optimized Routines (MIT License)\n\
@ -81,6 +82,7 @@ Copyright 2022 ARM Limited\n"
in the original string a count_trailing_zeros() operation will in the original string a count_trailing_zeros() operation will
identify exactly which byte is causing the termination, and why. */ identify exactly which byte is causing the termination, and why. */
ENTRY_ALIAS (__strrchr_aarch64, __rindex_aarch64)
ENTRY (__strrchr_aarch64) ENTRY (__strrchr_aarch64)
PTR_ARG (0) PTR_ARG (0)
/* Magic constant 0x40100401 to allow us to identify which lane /* Magic constant 0x40100401 to allow us to identify which lane

View file

@ -41,9 +41,5 @@ long labs(long x) {
return x < 0 ? -x : x; return x < 0 ? -x : x;
} }
/** __weak_reference(labs, llabs);
* Returns absolute value of 𝑥. __weak_reference(labs, imaxabs);
*/
long long llabs(long long x) {
return x < 0 ? -x : x;
}

View file

@ -2,6 +2,7 @@
#define COSMOPOLITAN_LIBC_BITS_H_ #define COSMOPOLITAN_LIBC_BITS_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0) #if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_ COSMOPOLITAN_C_START_
#ifdef COSMO
#define CheckUnsigned(x) ((x) / !((typeof(x))(-1) < 0)) #define CheckUnsigned(x) ((x) / !((typeof(x))(-1) < 0))
@ -11,8 +12,6 @@ COSMOPOLITAN_C_START_
extern const uint8_t kReverseBits[256]; extern const uint8_t kReverseBits[256];
uint32_t gray(uint32_t) pureconst;
uint32_t ungray(uint32_t) pureconst;
int _bitreverse8(int) libcesque pureconst; int _bitreverse8(int) libcesque pureconst;
int _bitreverse16(int) libcesque pureconst; int _bitreverse16(int) libcesque pureconst;
uint32_t _bitreverse32(uint32_t) uint32_t _bitreverse32(uint32_t)
@ -22,7 +21,6 @@ libcesque pureconst;
unsigned long _roundup2pow(unsigned long) libcesque pureconst; unsigned long _roundup2pow(unsigned long) libcesque pureconst;
unsigned long _roundup2log(unsigned long) libcesque pureconst; unsigned long _roundup2log(unsigned long) libcesque pureconst;
unsigned long _rounddown2pow(unsigned long) libcesque pureconst; unsigned long _rounddown2pow(unsigned long) libcesque pureconst;
unsigned long _hamming(unsigned long, unsigned long) pureconst;
unsigned _bextra(const unsigned *, size_t, char); unsigned _bextra(const unsigned *, size_t, char);
/*───────────────────────────────────────────────────────────────────────────│─╗ /*───────────────────────────────────────────────────────────────────────────│─╗
@ -157,6 +155,7 @@ unsigned _bextra(const unsigned *, size_t, char);
}) })
#endif /* __GNUC__ && !__STRICT_ANSI__ */ #endif /* __GNUC__ && !__STRICT_ANSI__ */
#endif /* COSMO */
COSMOPOLITAN_C_END_ COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_BITS_H_ */ #endif /* COSMOPOLITAN_LIBC_BITS_H_ */

View file

@ -60,23 +60,4 @@ int(_bsfl)(long x) {
return _bsfl(x); return _bsfl(x);
} }
/** __weak_reference(_bsfl, _bsfll);
* Returns position of first bit set.
*
* ctz(𝑥) 31^clz(𝑥) clz(𝑥)
* uint32 𝑥 _bsf(𝑥) tzcnt(𝑥) ffs(𝑥) _bsr(𝑥) lzcnt(𝑥)
* 0x00000000 wut 32 0 wut 32
* 0x00000001 0 0 1 0 31
* 0x80000001 0 0 1 31 0
* 0x80000000 31 31 32 31 0
* 0x00000010 4 4 5 4 27
* 0x08000010 4 4 5 27 4
* 0x08000000 27 27 28 27 4
* 0xffffffff 0 0 1 31 0
*
* @param 𝑥 is a 64-bit integer
* @return number in range 0..63 or undefined if 𝑥 is 0
*/
int(_bsfll)(long long x) {
return _bsfll(x);
}

View file

@ -60,23 +60,4 @@ int(_bsrl)(long x) {
return _bsrl(x); return _bsrl(x);
} }
/** __weak_reference(_bsrl, _bsrll);
* Returns binary logarithm of 𝑥.
*
* ctz(𝑥) 31^clz(𝑥) clz(𝑥)
* uint32 𝑥 _bsf(𝑥) tzcnt(𝑥) ffs(𝑥) _bsr(𝑥) lzcnt(𝑥)
* 0x00000000 wut 32 0 wut 32
* 0x00000001 0 0 1 0 31
* 0x80000001 0 0 1 31 0
* 0x80000000 31 31 32 31 0
* 0x00000010 4 4 5 4 27
* 0x08000010 4 4 5 27 4
* 0x08000000 27 27 28 27 4
* 0xffffffff 0 0 1 31 0
*
* @param x is a 64-bit integer
* @return number in range 0..63 or undefined if 𝑥 is 0
*/
int(_bsrll)(long long x) {
return _bsrll(x);
}

View file

@ -16,16 +16,32 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/str/str.h" #include "libc/intrin/bswap.h"
#include "libc/intrin/newbie.h"
#include "libc/sock/sock.h"
/** uint16_t(bswap_16)(uint16_t x) {
* Sets memory to zero w/ accompanying non-optimizing macro. return (0x00ff & x) << 010 | (0xff00 & x) >> 010;
* }
* This is intended for security-conscious applications.
* __strong_reference(bswap_16, ntohs);
* @param p points to bytes that'll be cleared __strong_reference(bswap_16, htons);
* @param n is number of bytes to clear
*/ uint32_t(bswap_32)(uint32_t x) {
void explicit_bzero(void *p, size_t n) { return (0x000000ffu & x) << 030 | (0x0000ff00u & x) << 010 |
bzero(p, n); (0x00ff0000u & x) >> 010 | (0xff000000u & x) >> 030;
}
__strong_reference(bswap_32, ntohl);
__strong_reference(bswap_32, htonl);
uint64_t(bswap_64)(uint64_t x) {
return (0x00000000000000ffull & x) << 070 |
(0x000000000000ff00ull & x) << 050 |
(0x0000000000ff0000ull & x) << 030 |
(0x00000000ff000000ull & x) << 010 |
(0x000000ff00000000ull & x) >> 010 |
(0x0000ff0000000000ull & x) >> 030 |
(0x00ff000000000000ull & x) >> 050 |
(0xff00000000000000ull & x) >> 070;
} }

View file

@ -5,7 +5,7 @@ COSMOPOLITAN_C_START_
uint16_t bswap_16(uint16_t) pureconst; uint16_t bswap_16(uint16_t) pureconst;
uint32_t bswap_32(uint32_t) pureconst; uint32_t bswap_32(uint32_t) pureconst;
uint32_t bswap_64(uint32_t) pureconst; uint64_t bswap_64(uint64_t) pureconst;
#if defined(__GNUC__) && !defined(__STRICT_ANSI__) #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#define bswap_16(x) __builtin_bswap16(x) #define bswap_16(x) __builtin_bswap16(x)

View file

@ -165,3 +165,5 @@ void bzero(void *p, size_t n) {
bzero128(b, n); bzero128(b, n);
} }
} }
__weak_reference(bzero, explicit_bzero);

View file

@ -91,3 +91,5 @@ wontreturn void _Exit(int exitcode) {
unreachable; unreachable;
#endif #endif
} }
__strong_reference(_Exit, _exit);

View file

@ -39,7 +39,9 @@
// - `FE_INEXACT` // - `FE_INEXACT`
// - `FE_ALL_EXCEPT` (all of the above) // - `FE_ALL_EXCEPT` (all of the above)
// @return 0 on success, or nonzero on error // @return 0 on success, or nonzero on error
.ftrace1
feclearexcept: feclearexcept:
.ftrace2
#ifdef __x86_64__ #ifdef __x86_64__
// maintain exceptions in the sse mxcsr, clear x87 exceptions // maintain exceptions in the sse mxcsr, clear x87 exceptions
mov %edi,%ecx mov %edi,%ecx
@ -79,6 +81,7 @@ feclearexcept:
// volatile double x = 0, y = 1 / x; // volatile double x = 0, y = 1 / x;
// assert(fetestexcept(FE_ALL_EXCEPT) == FE_DIVBYZERO); // assert(fetestexcept(FE_ALL_EXCEPT) == FE_DIVBYZERO);
// //
.ftrace1
// @param excepts may bitwise-or the following: // @param excepts may bitwise-or the following:
// - `FE_INVALID` // - `FE_INVALID`
// - `FE_DIVBYZERO` // - `FE_DIVBYZERO`
@ -88,7 +91,9 @@ feclearexcept:
// - `FE_ALL_EXCEPT` (all of the above) // - `FE_ALL_EXCEPT` (all of the above)
// @return mask of which exception status codes are currently set, // @return mask of which exception status codes are currently set,
// or zero if there aren't any floating point exceptions // or zero if there aren't any floating point exceptions
.ftrace1
fetestexcept: fetestexcept:
.ftrace2
#ifdef __x86_64__ #ifdef __x86_64__
and $0x3f,%edi and $0x3f,%edi
push %rax push %rax
@ -106,7 +111,9 @@ fetestexcept:
#endif #endif
.endfn fetestexcept,globl .endfn fetestexcept,globl
.ftrace1
feraiseexcept: feraiseexcept:
.ftrace2
#ifdef __x86_64__ #ifdef __x86_64__
and $0x3f,%edi and $0x3f,%edi
stmxcsr -8(%rsp) stmxcsr -8(%rsp)
@ -124,7 +131,9 @@ feraiseexcept:
#endif #endif
.endfn feraiseexcept,globl .endfn feraiseexcept,globl
.ftrace1
__fesetround: __fesetround:
.ftrace2
#ifdef __x86_64__ #ifdef __x86_64__
push %rax push %rax
xor %eax,%eax xor %eax,%eax
@ -150,7 +159,9 @@ __fesetround:
#endif #endif
.endfn __fesetround,globl,hidden .endfn __fesetround,globl,hidden
.ftrace1
fegetround: fegetround:
.ftrace2
#ifdef __x86_64__ #ifdef __x86_64__
push %rax push %rax
stmxcsr (%rsp) stmxcsr (%rsp)
@ -165,7 +176,9 @@ fegetround:
#endif #endif
.endfn fegetround,globl .endfn fegetround,globl
.ftrace1
fegetenv: fegetenv:
.ftrace2
#ifdef __x86_64__ #ifdef __x86_64__
xor %eax,%eax xor %eax,%eax
fnstenv (%rdi) fnstenv (%rdi)
@ -180,7 +193,9 @@ fegetenv:
#endif #endif
.endfn fegetenv,globl .endfn fegetenv,globl
.ftrace1
fesetenv: fesetenv:
.ftrace2
#ifdef __x86_64__ #ifdef __x86_64__
xor %eax,%eax xor %eax,%eax
inc %rdi inc %rdi

View file

@ -31,9 +31,4 @@ long ffsl(long x) {
return __builtin_ffsl(x); return __builtin_ffsl(x);
} }
/** __weak_reference(ffsl, ffsll);
* Finds lowest set bit in word.
*/
long long ffsll(long long x) {
return __builtin_ffsll(x);
}

View file

@ -1,28 +0,0 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/bits.h"
/**
* Returns gray code for x.
* @see https://en.wikipedia.org/wiki/Gray_code
* @see ungray()
*/
uint32_t gray(uint32_t x) {
return x ^ (x >> 1);
}

View file

@ -1,27 +0,0 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/popcnt.h"
/**
* Counts number of different bits.
* @see https://en.wikipedia.org/wiki/Hamming_code
*/
unsigned long _hamming(unsigned long x, unsigned long y) {
return popcnt(x ^ y);
}

View file

@ -2,10 +2,14 @@
#define COSMOPOLITAN_LIBC_BITS_HILBERT_H_ #define COSMOPOLITAN_LIBC_BITS_HILBERT_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0) #if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_ COSMOPOLITAN_C_START_
#ifdef COSMO
#define hilbert __hilbert
#define unhilbert __unhilbert
long hilbert(long, long, long) pureconst; long hilbert(long, long, long) pureconst;
axdx_t unhilbert(long, long) pureconst; axdx_t unhilbert(long, long) pureconst;
#endif /* COSMO */
COSMOPOLITAN_C_END_ COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_BITS_HILBERT_H_ */ #endif /* COSMOPOLITAN_LIBC_BITS_HILBERT_H_ */

View file

@ -1,8 +1,22 @@
#ifndef COSMOPOLITAN_LIBC_INTRIN_KPRINTF_H_ #ifndef COSMOPOLITAN_LIBC_INTRIN_KPRINTF_H_
#define COSMOPOLITAN_LIBC_INTRIN_KPRINTF_H_ #define COSMOPOLITAN_LIBC_INTRIN_KPRINTF_H_
#ifdef COSMO
#define kprintf __kprintf
#define ksnprintf __ksnprintf
#define kvprintf __kvprintf
#define kvsnprintf __kvsnprintf
#define kisdangerous __kisdangerous
#if !(__ASSEMBLER__ + __LINKER__ + 0) #if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_ COSMOPOLITAN_C_START_
void kprintf(const char *, ...);
size_t ksnprintf(char *, size_t, const char *, ...);
void kvprintf(const char *, va_list);
size_t kvsnprintf(char *, size_t, const char *, va_list);
bool kisdangerous(const void *);
void kprintf(const char *, ...); void kprintf(const char *, ...);
size_t ksnprintf(char *, size_t, const char *, ...); size_t ksnprintf(char *, size_t, const char *, ...);
void kvprintf(const char *, va_list); void kvprintf(const char *, va_list);
@ -12,4 +26,5 @@ void _klog(const char *, size_t);
COSMOPOLITAN_C_END_ COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMO */
#endif /* COSMOPOLITAN_LIBC_INTRIN_KPRINTF_H_ */ #endif /* COSMOPOLITAN_LIBC_INTRIN_KPRINTF_H_ */

View file

@ -1,6 +1,7 @@
#ifndef COSMOPOLITAN_LIBC_BITS_LIKELY_H_ #ifndef COSMOPOLITAN_LIBC_BITS_LIKELY_H_
#define COSMOPOLITAN_LIBC_BITS_LIKELY_H_ #define COSMOPOLITAN_LIBC_BITS_LIKELY_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0) #if !(__ASSEMBLER__ + __LINKER__ + 0)
#ifdef COSMO
#define LIKELY(x) __builtin_expect(!!(x), 1) #define LIKELY(x) __builtin_expect(!!(x), 1)
#define UNLIKELY(x) __builtin_expect(!!(x), 0) #define UNLIKELY(x) __builtin_expect(!!(x), 0)
@ -17,5 +18,6 @@
#define VERY_UNLIKELY(x) UNLIKELY(x) #define VERY_UNLIKELY(x) UNLIKELY(x)
#endif #endif
#endif /* COSMO */
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_BITS_LIKELY_H_ */ #endif /* COSMOPOLITAN_LIBC_BITS_LIKELY_H_ */

View file

@ -1,22 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_BITS_LOCKXADD_H_
#define COSMOPOLITAN_LIBC_BITS_LOCKXADD_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
intptr_t _lockxadd(void *, intptr_t, size_t);
#if defined(__GNUC__) && !defined(__STRICT_ANSI__) && defined(__x86__)
#define _lockxadd(PTR, VAL) \
({ \
typeof(*(PTR)) Res; \
typeof(Res) Val = (VAL); \
asm volatile("lock xadd\t%0,%1" : "=r"(Res), "+m"(*(PTR)) : "0"(Val)); \
Res; /* contains *PTR before addition cf. InterlockedAdd() */ \
})
#else
#define _lockxadd(MEM, VAL) _lockxadd(MEM, (intptr_t)(VAL), sizeof(*(MEM)))
#endif /* GNUC && !ANSI && x86 */
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_BITS_LOCKXADD_H_ */

View file

@ -342,6 +342,6 @@ void *memmove(void *dst, const void *src, size_t n) {
} }
} }
__strong_reference(memmove, memcpy); __weak_reference(memmove, memcpy);
#endif /* __aarch64__ */ #endif /* __aarch64__ */

View file

@ -23,7 +23,7 @@
* @see https://en.wikipedia.org/wiki/Z-order_curve * @see https://en.wikipedia.org/wiki/Z-order_curve
* @see unmorton() * @see unmorton()
*/ */
unsigned long(morton)(unsigned long y, unsigned long x) { unsigned long morton(unsigned long y, unsigned long x) {
x = (x | x << 020) & 0x0000FFFF0000FFFF; x = (x | x << 020) & 0x0000FFFF0000FFFF;
x = (x | x << 010) & 0x00FF00FF00FF00FF; x = (x | x << 010) & 0x00FF00FF00FF00FF;
x = (x | x << 004) & 0x0F0F0F0F0F0F0F0F; x = (x | x << 004) & 0x0F0F0F0F0F0F0F0F;

View file

@ -1,24 +1,16 @@
#ifndef COSMOPOLITAN_LIBC_BITS_MORTON_H_ #ifndef COSMOPOLITAN_LIBC_BITS_MORTON_H_
#define COSMOPOLITAN_LIBC_BITS_MORTON_H_ #define COSMOPOLITAN_LIBC_BITS_MORTON_H_
#include "libc/intrin/pdep.h"
#include "libc/intrin/pext.h"
#include "libc/nexgen32e/x86feature.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0) #if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_ COSMOPOLITAN_C_START_
#ifdef COSMO
#define morton __morton
#define unmorton __unmorton
unsigned long morton(unsigned long, unsigned long) libcesque; unsigned long morton(unsigned long, unsigned long) libcesque;
axdx_t unmorton(unsigned long) libcesque; axdx_t unmorton(unsigned long) libcesque;
#ifndef __STRICT_ANSI__ #endif /* COSMO */
#define morton(Y, X) \
(X86_NEED(BMI2) ? pdep(X, 0x5555555555555555) | pdep(Y, 0xAAAAAAAAAAAAAAAA) \
: morton(Y, X))
#define unmorton(I) \
(X86_NEED(BMI2) \
? (axdx_t){pext(I, 0xAAAAAAAAAAAAAAAA), pext(I, 0x5555555555555555)} \
: unmorton(I))
#endif
COSMOPOLITAN_C_END_ COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_BITS_MORTON_H_ */ #endif /* COSMOPOLITAN_LIBC_BITS_MORTON_H_ */

View file

@ -2,11 +2,6 @@
#define COSMOPOLITAN_LIBC_BITS_NEWBIE_H_ #define COSMOPOLITAN_LIBC_BITS_NEWBIE_H_
#include "libc/intrin/bswap.h" #include "libc/intrin/bswap.h"
/*
* Macros for newbies.
* https://justine.lol/endian.html
*/
#define BYTE_ORDER __BYTE_ORDER__ #define BYTE_ORDER __BYTE_ORDER__
#define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__ #define LITTLE_ENDIAN __ORDER_LITTLE_ENDIAN__
#define BIG_ENDIAN __ORDER_BIG_ENDIAN__ #define BIG_ENDIAN __ORDER_BIG_ENDIAN__

View file

@ -2,6 +2,7 @@
#define COSMOPOLITAN_LIBC_BITS_PUSHPOP_H_ #define COSMOPOLITAN_LIBC_BITS_PUSHPOP_H_
#include "libc/macros.internal.h" #include "libc/macros.internal.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0) #if !(__ASSEMBLER__ + __LINKER__ + 0)
#ifdef COSMO
#if !defined(__GNUC__) || defined(__STRICT_ANSI__) || !defined(__x86_64__) #if !defined(__GNUC__) || defined(__STRICT_ANSI__) || !defined(__x86_64__)
#define pushpop(x) (x) #define pushpop(x) (x)
@ -51,5 +52,6 @@
}) })
#endif #endif
#endif /* COSMO */
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_BITS_PUSHPOP_H_ */ #endif /* COSMOPOLITAN_LIBC_BITS_PUSHPOP_H_ */

View file

@ -1,6 +1,7 @@
#ifndef COSMOPOLITAN_LIBC_INTRIN_REPMOVSB_H_ #ifndef COSMOPOLITAN_LIBC_INTRIN_REPMOVSB_H_
#define COSMOPOLITAN_LIBC_INTRIN_REPMOVSB_H_ #define COSMOPOLITAN_LIBC_INTRIN_REPMOVSB_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0) #if !(__ASSEMBLER__ + __LINKER__ + 0)
#ifdef COSMO
forceinline void repmovsb(void **dest, const void **src, size_t cx) { forceinline void repmovsb(void **dest, const void **src, size_t cx) {
char *di = (char *)*dest; char *di = (char *)*dest;
@ -22,5 +23,6 @@ forceinline void repmovsb(void **dest, const void **src, size_t cx) {
}) })
#endif #endif
#endif /* COSMO */
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_INTRIN_REPMOVSB_H_ */ #endif /* COSMOPOLITAN_LIBC_INTRIN_REPMOVSB_H_ */

View file

@ -1,6 +1,7 @@
#ifndef COSMOPOLITAN_LIBC_INTRIN_REPSTOSB_H_ #ifndef COSMOPOLITAN_LIBC_INTRIN_REPSTOSB_H_
#define COSMOPOLITAN_LIBC_INTRIN_REPSTOSB_H_ #define COSMOPOLITAN_LIBC_INTRIN_REPSTOSB_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0) #if !(__ASSEMBLER__ + __LINKER__ + 0)
#ifdef COSMO
forceinline void *repstosb(void *dest, unsigned char al, size_t cx) { forceinline void *repstosb(void *dest, unsigned char al, size_t cx) {
unsigned char *di = (unsigned char *)dest; unsigned char *di = (unsigned char *)dest;
@ -21,5 +22,6 @@ forceinline void *repstosb(void *dest, unsigned char al, size_t cx) {
}) })
#endif #endif
#endif /* COSMO */
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_INTRIN_REPSTOSB_H_ */ #endif /* COSMOPOLITAN_LIBC_INTRIN_REPSTOSB_H_ */

View file

@ -2,6 +2,7 @@
#define COSMOPOLITAN_LIBC_BITS_SEGMENTATION_H_ #define COSMOPOLITAN_LIBC_BITS_SEGMENTATION_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0) #if !(__ASSEMBLER__ + __LINKER__ + 0)
#if defined(__GNUC__) && !defined(__STRICT_ANSI__) #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#ifdef COSMO
/** /**
* Reads scalar from memory, offset by segment. * Reads scalar from memory, offset by segment.
@ -20,6 +21,7 @@
Pk; \ Pk; \
}) })
#endif /* COSMO */
#endif /* __GNUC__ && !__STRICT_ANSI__ */ #endif /* __GNUC__ && !__STRICT_ANSI__ */
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_BITS_SEGMENTATION_H_ */ #endif /* COSMOPOLITAN_LIBC_BITS_SEGMENTATION_H_ */

View file

@ -117,4 +117,6 @@ char *strchr(const char *s, int c) {
#endif #endif
} }
__strong_reference(strchr, index);
#endif /* __aarch64__ */ #endif /* __aarch64__ */

View file

@ -31,4 +31,6 @@ char *strrchr(const char *s, int c) {
return memrchr(s, c, strlen(s)); return memrchr(s, c, strlen(s));
} }
__strong_reference(strrchr, rindex);
#endif /* __aarch64__ */ #endif /* __aarch64__ */

View file

@ -1,65 +0,0 @@
/*-*- 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
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
.privileged
// Invokes SYSCALL for libfatal forceinline asm() routines.
//
// @param rax is ordinal
// @param rdi is arg1
// @param rsi is arg2
// @param rdx is arg3
// @param rcx is arg4
// @param r8 is arg5
// @param r9 is arg6
// @param rsp may contain more args
// @return rdx:rax where rax holds -errno on error
// @clob rax,rdx,memory,cc
__syscall__:
mov %rcx,.Lrcx(%rip)
mov %rdi,.Lrdi(%rip)
mov %rsi,.Lrsi(%rip)
mov %r8,.Lr8(%rip)
mov %r9,.Lr9(%rip)
mov %r10,.Lr10(%rip)
mov %r11,.Lr11(%rip)
mov %rcx,%r10
clc
syscall
jnc 1f
neg %rax
1: mov .Lrcx(%rip),%rcx
mov .Lrdi(%rip),%rdi
mov .Lrsi(%rip),%rsi
mov .Lr8(%rip),%r8
mov .Lr9(%rip),%r9
mov .Lr10(%rip),%r10
mov .Lr11(%rip),%r11
ret
.endfn __syscall__,globl,hidden
.bss
.balign 8
.Lrcx: .quad 0 // clobbered by syscall
.Lrdi: .quad 0 // just in case
.Lrsi: .quad 0 // just in case
.Lr8: .quad 0 // freebsd bug?
.Lr9: .quad 0 // just in case
.Lr10: .quad 0 // just in case
.Lr11: .quad 0 // clobbered by syscall

View file

@ -1,33 +0,0 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/bits.h"
/**
* Decodes gray code.
* @see https://en.wikipedia.org/wiki/Gray_code
* @see gray()
*/
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

@ -36,6 +36,6 @@ static unsigned long GetOddBits(unsigned long x) {
* @see en.wikipedia.org/wiki/Z-order_curve * @see en.wikipedia.org/wiki/Z-order_curve
* @see morton() * @see morton()
*/ */
axdx_t(unmorton)(unsigned long i) { axdx_t unmorton(unsigned long i) {
return (axdx_t){GetOddBits(i >> 1), GetOddBits(i)}; return (axdx_t){GetOddBits(i >> 1), GetOddBits(i)};
} }

View file

@ -1,16 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_BITS_XADD_H_
#define COSMOPOLITAN_LIBC_BITS_XADD_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define _xadd(p, v) \
({ \
typeof(*(p)) Res; \
autotype(Res) Val = (v); \
asm volatile("xadd\t%0,%1" : "=r"(Res), "+m"(*(p)) : "0"(Val)); \
Res + Val; \
})
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_BITS_XADD_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=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2021 Justine Alexandra Roberts Tunney
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/runtime/runtime.h"
/**
* Terminates process, ignoring destructors and atexit() handlers.
*
* When running on bare metal, this function will reboot your computer
* by hosing the interrupt descriptors and triple faulting the system.
*
* @param exitcode is masked with 255
* @asyncsignalsafe
* @vforksafe
* @noreturn
*/
wontreturn void _exit(int exitcode) {
_Exit(exitcode);
}

View file

@ -9,10 +9,10 @@ COSMOPOLITAN_C_START_
#define INET_ADDRSTRLEN 22 #define INET_ADDRSTRLEN 22
#define IFHWADDRLEN 6 #define IFHWADDRLEN 6
uint16_t htons(uint16_t); uint16_t htons(uint16_t) pureconst;
uint16_t ntohs(uint16_t); uint16_t ntohs(uint16_t) pureconst;
uint32_t htonl(uint32_t); uint32_t htonl(uint32_t) pureconst;
uint32_t ntohl(uint32_t); uint32_t ntohl(uint32_t) pureconst;
#if defined(__GNUC__) && !defined(__STRICT_ANSI__) #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#define htons(x) __builtin_bswap16(x) #define htons(x) __builtin_bswap16(x)

View file

@ -1,9 +1,20 @@
#ifndef COSMOPOLITAN_LIBC_STDIO_APPEND_H_ #ifndef COSMOPOLITAN_LIBC_STDIO_APPEND_H_
#define COSMOPOLITAN_LIBC_STDIO_APPEND_H_ #define COSMOPOLITAN_LIBC_STDIO_APPEND_H_
#include "libc/fmt/pflink.h" #include "libc/fmt/pflink.h"
#ifdef COSMO
#define APPEND_COOKIE 21578 #define APPEND_COOKIE 21578
#define appendz __appendz
#define appendr __appendr
#define appendd __appendd
#define appendw __appendw
#define appends __appends
#define appendf __appendf
#define vappendf __vappendf
#define kappendf __kappendf
#define kvappendf __kvappendf
#if !(__ASSEMBLER__ + __LINKER__ + 0) #if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_ COSMOPOLITAN_C_START_
@ -22,11 +33,7 @@ ssize_t vappendf(char **, const char *, va_list);
ssize_t kappendf(char **, const char *, ...); ssize_t kappendf(char **, const char *, ...);
ssize_t kvappendf(char **, const char *, va_list); ssize_t kvappendf(char **, const char *, va_list);
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
#define appendf(BUF, FMT, ...) appendf(BUF, PFLINK(FMT), ##__VA_ARGS__)
#define vappendf(BUF, FMT, VA) vappendf(BUF, PFLINK(FMT), VA)
#endif
COSMOPOLITAN_C_END_ COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMO */
#endif /* COSMOPOLITAN_LIBC_STDIO_APPEND_H_ */ #endif /* COSMOPOLITAN_LIBC_STDIO_APPEND_H_ */

View file

@ -34,3 +34,5 @@ int fgetc(FILE *f) {
funlockfile(f); funlockfile(f);
return rc; return rc;
} }
__strong_reference(fgetc, getc);

View file

@ -34,3 +34,5 @@ int fgetc_unlocked(FILE *f) {
return b[0]; return b[0];
} }
} }
__strong_reference(fgetc_unlocked, getc_unlocked);

View file

@ -34,3 +34,5 @@ wint_t fgetwc(FILE *f) {
funlockfile(f); funlockfile(f);
return wc; return wc;
} }
__strong_reference(fgetwc, getwc);

View file

@ -49,3 +49,5 @@ wint_t fgetwc_unlocked(FILE *f) {
} }
return x; return x;
} }
__strong_reference(fgetwc_unlocked, getwc_unlocked);

View file

@ -33,3 +33,5 @@ int fputc(int c, FILE *f) {
funlockfile(f); funlockfile(f);
return rc; return rc;
} }
__strong_reference(fputc, putc);

View file

@ -36,3 +36,5 @@ int fputc_unlocked(int c, FILE *f) {
return b; return b;
} }
} }
__strong_reference(fputc_unlocked, putc_unlocked);

View file

@ -34,3 +34,5 @@ wint_t fputwc(wchar_t wc, FILE *f) {
funlockfile(f); funlockfile(f);
return rc; return rc;
} }
__strong_reference(fputwc, putwc);

View file

@ -40,3 +40,5 @@ wint_t fputwc_unlocked(wchar_t wc, FILE *f) {
return -1; return -1;
} }
} }
__strong_reference(fputwc_unlocked, putwc_unlocked);

View file

@ -1,36 +0,0 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/stdio/stdio.h"
/**
* Repositions open file stream.
*
* This function flushes the buffer (unless it's currently in the EOF
* state) and then calls lseek() on the underlying file. If the stream
* is in the EOF state, this function can be used to restore it without
* needing to reopen the file.
*
* @param f is a non-null stream handle
* @param offset is the byte delta
* @param whence can be SEET_SET, SEEK_CUR, or SEEK_END
* @returns 0 on success or -1 on error
*/
int fseek(FILE *f, long offset, int whence) {
return fseeko(f, offset, whence);
}

View file

@ -44,3 +44,5 @@ int fseeko(FILE *f, int64_t offset, int whence) {
funlockfile(f); funlockfile(f);
return rc; return rc;
} }
__strong_reference(fseeko, fseek);

View file

@ -1,29 +0,0 @@
/*-*- 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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/stdio/stdio.h"
/**
* Returns current position of stream.
*
* @param stream is a non-null stream handle
* @returns current byte offset from beginning, or -1 w/ errno
*/
long ftell(FILE *f) {
return ftello(f);
}

View file

@ -55,3 +55,5 @@ int64_t ftello(FILE *f) {
funlockfile(f); funlockfile(f);
return rc; return rc;
} }
__strong_reference(ftello, ftell);

View file

@ -1,28 +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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/stdio/stdio.h"
/**
* Reads byte from stream.
* @return byte in range 0..255, or -1 w/ errno
* @threadsafe
*/
int(getc)(FILE *f) {
return fgetc(f);
}

View file

@ -1,27 +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
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/stdio/stdio.h"
/**
* Reads byte from stream.
* @return byte in range 0..255, or -1 w/ errno
*/
int(getc_unlocked)(FILE *f) {
return fgetc_unlocked(f);
}

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