Improve signal handling and math

- Polyfill ucontext_t on FreeBSD/OpenBSD/NetBSD
- Add tests confirming signals can edit CPU state
- Work towards supporting ZIP filesystem on bare metal
- Add more tinymath unit tests for POSIX conformance
- Add X87 and SSE status flags to crash report
- Fix some bugs in blinkenlights
- Fix llvm build breakage
This commit is contained in:
Justine Tunney 2021-02-25 18:30:17 -08:00
parent cdc54ea1fd
commit 40291c9db3
109 changed files with 2316 additions and 520 deletions

View file

@ -1,16 +0,0 @@
Cosmopolitan TinyMath
“Seymour Cray didn't care that 81.0/3.0 did not give exactly
27.0 on the CDC 6000 class machines; and he was universally
respected for making the fastest machines around.
──Linus Torvalds
Your Cosmopolitan TinyMath library provides hardware-accelerated scalar
transcendental mathematical functions that are superior to the portable
standards-compliant math library, in terms of both performance and code
size, by trading away focus on temporal concerns, like IEEE conformance
or rounding errors at the femto-scale, or reproducible results across a
broad array of niche machine languages.

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns arc cosine of 𝑥.
//

View file

@ -17,8 +17,12 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns 𝑥 with same sign as 𝑦.
//
// @param 𝑥 is double scalar in low half of %xmm0
// @param 𝑦 is double scalar in low half of %xmm1
// @return double scalar in low half of %xmm0
copysign:
.leafprologue
.profilable

View file

@ -17,8 +17,12 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns 𝑥 with same sign as 𝑦.
//
// @param 𝑦 is float scalar in low quarter of %xmm0
// @param 𝑥 is float scalar in low quarter of %xmm1
// @return float scalar in low quarter of %xmm0
copysignf:
.leafprologue
.profilable

View file

@ -18,8 +18,12 @@
*/
#include "libc/runtime/pc.internal.h"
#include "libc/macros.h"
.source __FILE__
// Returns 𝑥 with same sign as 𝑦.
//
// @param 𝑥 is an 80-bit long double passed on stack in 16-bytes
// @param 𝑦 is the power, also pushed on stack, in reverse order
// @return result on FPU stack in %st
copysignl:
push %rbp
mov %rsp,%rbp

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns 𝑒^x.
//

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns 10^x.
//

View file

@ -17,17 +17,21 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns 10^x.
//
// @param 𝑥 is an 80-bit long double passed on stack in 16-bytes
// @return result of exponentiation on FPU stack in %st
exp10l:
push %rbp
exp10l: push %rbp
mov %rsp,%rbp
.profilable
fldt 16(%rbp)
fxam # isinf(x)
fstsw %ax
mov %ah,%al
and $0x45,%ah
cmp $5,%ah
je 1f
fldl2t
fmulp %st,%st(1)
fld %st
@ -38,8 +42,13 @@ exp10l:
fld1
faddp
fscale
fstp %st(1)
0: fstp %st(1)
pop %rbp
ret
1: test $2,%al # signbit(x)
jz 0b
fstp %st
fldz
jmp 0b
.endfn exp10l,globl
.alias exp10l,pow10l

View file

@ -17,13 +17,11 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns 2^𝑥.
//
// @param 𝑥 is a double passed in the lower quadword of %xmm0
// @return result in lower quadword of %xmm0
exp2:
ezlea exp2l,ax
exp2: ezlea exp2l,ax
jmp _d2ld2
.endfn exp2,globl

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns 2^𝑥.
//
@ -27,17 +26,26 @@ exp2l: push %rbp
mov %rsp,%rbp
.profilable
fldt 16(%rbp)
fxam # isinf(x)
fstsw %ax
mov %ah,%al
and $0x45,%ah
cmp $5,%ah
je 1f
fld %st
frndint
fsubr %st,%st(1)
fxch
f2xm1
fadds .Lone(%rip)
fld1
faddp
fscale
fstp %st(1)
pop %rbp
0: pop %rbp
ret
1: test $2,%al # signbit(x)
jz 0b
fstp %st
fldz
jmp 0b
.endfn exp2l,globl
.rodata.cst4
.Lone: .float 1.0

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns 𝑒^x.
//
@ -27,6 +26,12 @@ expl: push %rbp
mov %rsp,%rbp
.profilable
fldt 16(%rbp)
fxam # isinf(x)
fstsw %ax
mov %ah,%al
and $0x45,%ah
cmp $5,%ah
je 1f
fldl2e
fmulp %st,%st(1)
fld %st
@ -38,6 +43,11 @@ expl: push %rbp
faddp
fscale
fstp %st(1)
pop %rbp
0: pop %rbp
ret
1: test $2,%al # signbit(x)
jz 0b
fstp %st
fldz
jmp 0b
.endfn expl,globl

View file

@ -17,13 +17,11 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns 𝑒^x-1.
//
// @param 𝑥 is double scalar in low half of %xmm0
// @return double scalar in low half of %xmm0
expm1:
ezlea expm1l,ax
expm1: ezlea expm1l,ax
jmp _d2ld2
.endfn expm1,globl

View file

@ -16,18 +16,23 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/runtime/pc.internal.h"
#include "libc/macros.h"
.source __FILE__
// Returns 𝑒^x-1.
//
// @param 𝑥 is an 80-bit long double passed on stack in 16-bytes
// @return result of exponentiation on FPU stack in %st
expm1l:
push %rbp
expm1l: push %rbp
mov %rsp,%rbp
.profilable
fldt 16(%rbp)
fxam # isinf(x)
fstsw %ax
mov %ah,%al
and $0x45,%ah
cmp $5,%ah
je 1f
fldl2e
fmulp %st,%st(1)
fld %st
@ -40,11 +45,15 @@ expm1l:
fxch %st(2)
fscale
fstp %st(1)
fsubs .Lone(%rip)
fld1
fsubrp
faddp %st,%st(1)
pop %rbp
0: pop %rbp
ret
1: test $2,%al # signbit(x)
jz 0b
fstp %st
fld1
3: fchs
jmp 0b
.endfn expm1l,globl
.rodata.cst4
.Lone: .float 1.0

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns absolute value of 𝑥.
//

View file

@ -1,44 +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
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.h"
.source __FILE__
fld1: fld1
ret
.endfn fld1,globl
fldl2t: fldl2t
ret
.endfn fldl2t,globl
fldlg2: fldlg2
ret
.endfn fldlg2,globl
fldl2e: fldl2e
ret
.endfn fldl2e,globl
fldln2: fldln2
ret
.endfn fldln2,globl
fldpi: fldpi
ret
.endfn fldpi,globl

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// fmod [sic] does (𝑥 rem 𝑦) w/ round()-style rounding.
//

View file

@ -18,8 +18,14 @@
*/
#include "libc/runtime/pc.internal.h"
#include "libc/macros.h"
.source __FILE__
// fmod [sic] does (𝑥 rem 𝑦) w/ round()-style rounding.
//
// @param 𝑥 is an 80-bit long double passed on stack in 16-bytes
// @param 𝑦 is the power, also pushed on stack, in reverse order
// @return remainder ∈ (-|𝑦|,|𝑦|) in %st
// @define 𝑥-truncl(𝑥/𝑦)*𝑦
// @see emod()
fmodl: push %rbp
mov %rsp,%rbp
.profilable
@ -32,4 +38,7 @@ fmodl: push %rbp
fstp %st(1)
pop %rbp
ret
1: int3
pop %rbp
ret
.endfn fmodl,globl

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns 𝑥 × 2ʸ.
//

View file

@ -16,8 +16,8 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/runtime/pc.internal.h"
#include "libc/macros.h"
.source __FILE__
// Returns 𝑥 × 2ʸ.
//

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns natural logarithm of 𝑥.
//

View file

@ -17,18 +17,16 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns log(𝟷+𝑥).
//
// @param 𝑥 is double scalar in low half of %xmm0
// @return double scalar in low half of %xmm0
log1p:
push %rbp
log1p: push %rbp
mov %rsp,%rbp
.profilable
push %rax
vmovsd %xmm0,(%rsp)
movsd %xmm0,(%rsp)
fldl (%rsp)
fld %st
fabs
@ -41,7 +39,7 @@ log1p:
fxch
fyl2xp1
fstpl (%rsp)
vmovsd (%rsp),%xmm0
movsd (%rsp),%xmm0
0: leave
ret
1: fld1
@ -50,12 +48,12 @@ log1p:
fxch
fyl2x
fstpl (%rsp)
vmovsd (%rsp),%xmm0
movsd (%rsp),%xmm0
jmp 0b
.endfn log1p,globl
.rodata.cst16
.LC16: .long 205731576
.long 2515933592
.long 16381
.LC16: .long 0x0c4336f8
.long 0x95f61998
.long 0x3ffd
.long 0

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns log(𝟷+𝑥).
//
@ -27,11 +26,11 @@ log1pf: push %rbp
mov %rsp,%rbp
.profilable
push %rax
vmovss %xmm0,-4(%rbp)
movss %xmm0,-4(%rbp)
flds -4(%rbp)
fld %st
fabs
fldt .LC16(%rip)
fldt .Lnnan(%rip)
fxch
fcomip %st(1),%st
fstp %st
@ -40,7 +39,7 @@ log1pf: push %rbp
fxch
fyl2xp1
1: fstps -4(%rbp)
vmovss -4(%rbp),%xmm0
movss -4(%rbp),%xmm0
leave
ret
2: fld1
@ -52,7 +51,7 @@ log1pf: push %rbp
.endfn log1pf,globl
.rodata.cst16
.LC16: .long 205731576
.long 2515933592
.long 16381
.Lnnan: .long 0x0c4336f8
.long 0x95f61998
.long 0x3ffd
.long 0

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Calculates log𝑥.
//

View file

@ -17,15 +17,13 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Calculates log𝑥.
//
// @param 𝑥 is an 80-bit long double passed on stack in 16-bytes
// @return result in %st
// @see ilogbl()
log2l:
push %rbp
log2l: push %rbp
mov %rsp,%rbp
.profilable
fld1

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns natural logarithm of 𝑥.
//

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns 𝑥^𝑦.
//

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns 𝑥^𝑦.
//
@ -25,6 +24,7 @@
// @param 𝑦 is the power, also pushed on stack, in reverse order
// @return result of exponentiation on FPU stack in %st
// @note Sun's fdlibm needs 2kLOC to do this for RISC lool
// @define exp2l(fmodl(y*log2l(x),1))*exp2l(y)
powl: push %rbp
mov %rsp,%rbp
.profilable

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns sine of 𝑥.
//

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns square root of 𝑥.
//

View file

@ -17,7 +17,6 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.h"
.source __FILE__
// Returns square root of 𝑥.
//