Skip to content

Commit 8af91bc

Browse files
committed
Make pow() conform to standard definition
1 parent 754974f commit 8af91bc

File tree

15 files changed

+253
-85
lines changed

15 files changed

+253
-85
lines changed

libc/log/die.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/bits/bits.h"
20-
#include "libc/bits/weaken.h"
2120
#include "libc/dce.h"
2221
#include "libc/log/backtrace.internal.h"
2322
#include "libc/log/log.h"
24-
#include "libc/runtime/runtime.h"
25-
#include "libc/stdio/stdio.h"
2623

2724
/**
2825
* Aborts process after printing a backtrace.
@@ -32,13 +29,10 @@
3229
relegated wontreturn void __die(void) {
3330
static bool once;
3431
if (cmpxchg(&once, false, true)) {
35-
if (weaken(fflush)) {
36-
weaken(fflush)(NULL);
37-
}
3832
if (!IsTiny()) {
3933
if (IsDebuggerPresent(false)) DebugBreak();
4034
ShowBacktrace(2, NULL);
4135
}
4236
}
43-
_exit(77);
37+
exit(77);
4438
}

libc/log/onkill.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "libc/calls/calls.h"
2121
#include "libc/calls/sigbits.h"
2222
#include "libc/calls/struct/sigaction.h"
23+
#include "libc/calls/struct/siginfo.h"
24+
#include "libc/calls/ucontext.h"
2325
#include "libc/dce.h"
2426
#include "libc/log/log.h"
2527
#include "libc/macros.internal.h"
@@ -34,9 +36,6 @@
3436
* @see also libc/oncrash.c
3537
*/
3638

37-
struct ucontext;
38-
struct siginfo;
39-
4039
static const int sigs[] = {
4140
SIGHUP, /* hangup aka ctrl_close_event */
4241
SIGINT, /* ctrl+c aka ^C aka ETX aka \003 aka ♥ */

libc/nt/master.sh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7698,7 +7698,7 @@ imp '_memccpy' _memccpy ntdll 2198
76987698
imp '_memicmp' _memicmp ntdll 2199
76997699
imp '_onexit' _onexit KernelBase 1846
77007700
imp '_purecall' _purecall KernelBase 1847
7701-
imp 'sys__setjmp_nt' _setjmp ntdll 2200
7701+
imp 'sys__setjmp_nt' _setjmp ntdll 2200
77027702
imp '_setjmpex' _setjmpex ntdll 2201
77037703
imp '_time64' _time64 KernelBase 1848
77047704
imp '_wmakepath_s' _wmakepath_s ntdll 2245
@@ -7707,7 +7707,7 @@ imp '_wtoi' _wtoi ntdll 2247
77077707
imp '_wtoi64' _wtoi64 ntdll 2248
77087708
imp '_wtol' _wtol ntdll 2249
77097709
imp 'sys_abs_nt' abs ntdll 2250
7710-
imp '__sys_accept_nt' accept ws2_32 1
7710+
imp '__sys_accept_nt' accept ws2_32 1
77117711
imp 'sys_atan_nt' atan ntdll 2251
77127712
imp 'sys_atan2_nt' atan2 ntdll 2252
77137713
imp 'sys_atexit_nt' atexit KernelBase 1849
@@ -7718,12 +7718,12 @@ imp 'bDeleteLDC' bDeleteLDC gdi32 1949
77187718
imp 'bInitSystemAndFontsDirectories' bInitSystemAndFontsDirectoriesW gdi32 1950
77197719
imp 'bMakePathName' bMakePathNameW gdi32 1951
77207720
imp '__sys_bind_nt' bind ws2_32 2 3
7721-
imp 'sys_bsearch_nt' bsearch ntdll 2255
7721+
imp 'sys_bsearch_nt' bsearch ntdll 2255
77227722
imp 'bsearch_s' bsearch_s ntdll 2256
77237723
imp 'cGetTTFFromFOT' cGetTTFFromFOT gdi32 1952
77247724
imp 'sys_ceil_nt' ceil ntdll 2257
77257725
imp '__sys_closesocket_nt' closesocket ws2_32 3 1
7726-
imp '__sys_connect_nt' connect ws2_32 4
7726+
imp '__sys_connect_nt' connect ws2_32 4
77277727
imp 'sys_cos_nt' cos ntdll 2258
77287728
imp 'dwLBSubclass' dwLBSubclass comdlg32 128
77297729
imp 'dwOKSubclass' dwOKSubclass comdlg32 129
@@ -7745,7 +7745,7 @@ imp 'sys_gethostname_nt' gethostname ws2_32 57
77457745
imp 'sys_getnameinfo_nt' getnameinfo ws2_32 192
77467746
imp '__sys_getpeername_nt' getpeername ws2_32 5 3
77477747
imp 'sys_getprotobyname_nt' getprotobyname ws2_32 53
7748-
imp 'sys_getprotobynumber_nt' getprotobynumber ws2_32 54
7748+
imp 'sys_getprotobynumber_nt' getprotobynumber ws2_32 54
77497749
imp 'sys_getservbyname_nt' getservbyname ws2_32 55
77507750
imp 'sys_getservbyport_nt' getservbyport ws2_32 56
77517751
imp '__sys_getsockname_nt' getsockname ws2_32 6 3
@@ -7758,24 +7758,24 @@ imp '__sys_ioctlsocket_nt' ioctlsocket ws2_32 10 3
77587758
imp 'iswascii' iswascii ntdll 2273
77597759
imp 'keybd_event' keybd_event user32 2580
77607760
imp 'sys_labs_nt' labs ntdll 2282
7761-
imp '__sys_listen_nt' listen ws2_32 13 2
7761+
imp '__sys_listen_nt' listen ws2_32 13 2
77627762
imp 'sys_log_nt' log ntdll 2283
7763-
imp 'sys_longjmp_nt' longjmp ntdll 2284
7763+
imp 'sys_longjmp_nt' longjmp ntdll 2284
77647764
imp 'mouse_event' mouse_event user32 2583
77657765
imp 'pGdiDevCaps' pGdiDevCaps gdi32 1961
77667766
imp 'pGdiSharedHandleTable' pGdiSharedHandleTable gdi32 1962
77677767
imp 'pGdiSharedMemory' pGdiSharedMemory gdi32 1963
77687768
imp 'pldcGet' pldcGet gdi32 1964
77697769
imp 'sys_recv_nt' recv ws2_32 16
7770-
imp '__sys_recvfrom_nt' recvfrom ws2_32 17
7771-
imp '__sys_select_nt' select ws2_32 18 5
7770+
imp '__sys_recvfrom_nt' recvfrom ws2_32 17
7771+
imp '__sys_select_nt' select ws2_32 18 5
77727772
imp 'semDxTrimNotification' semDxTrimNotification gdi32 1965
77737773
imp 'sys_send_nt' send ws2_32 19
7774-
imp '__sys_sendto_nt' sendto ws2_32 20
7774+
imp '__sys_sendto_nt' sendto ws2_32 20
77757775
imp '__sys_setsockopt_nt' setsockopt ws2_32 21 5
7776-
imp '__sys_shutdown_nt' shutdown ws2_32 22 2
7776+
imp '__sys_shutdown_nt' shutdown ws2_32 22 2
77777777
imp 'sys_sin_nt' sin ntdll 2296
7778-
imp '__sys_socket_nt' socket ws2_32 23
7778+
imp '__sys_socket_nt' socket ws2_32 23
77797779
imp 'timeBeginPeriod' timeBeginPeriod kernel32 1609
77807780
imp 'timeEndPeriod' timeEndPeriod kernel32 1610
77817781
imp 'timeGetDevCaps' timeGetDevCaps kernel32 1611

libc/tinymath/copysign.S

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ copysign:
3636
.rodata.cst16
3737
.Lnan: .long 0xffffffff
3838
.long 0x7fffffff
39-
.long 0
40-
.long 0
41-
.Lneg0: .long 0
42-
.long -2147483648
43-
.long 0
44-
.long 0
39+
.long 0x00000000
40+
.long 0x00000000
41+
.Lneg0: .long 0x00000000
42+
.long 0x80000000
43+
.long 0x00000000
44+
.long 0x00000000

libc/tinymath/cos.S

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/macros.internal.h"
20-
.source __FILE__
2120

2221
// Returns cosine of 𝑥.
2322
//

libc/tinymath/exp10.S

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
// @param 𝑥 is double scalar in low half of %xmm0
2424
// @return double scalar in low half of %xmm0
2525
// @see pow(), exp()
26-
exp10:
27-
ezlea exp10l,ax
26+
exp10: ezlea exp10l,ax
2827
jmp _d2ld2
2928
.endfn exp10,globl
3029
.alias exp10,pow10

libc/tinymath/fabsl.S

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/macros.internal.h"
20-
.source __FILE__
2120

2221
// Returns absolute value of 𝑥.
2322
//

libc/tinymath/powl.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2021 Justine Alexandra Roberts Tunney │
5+
│ │
6+
│ Permission to use, copy, modify, and/or distribute this software for │
7+
│ any purpose with or without fee is hereby granted, provided that the │
8+
│ above copyright notice and this permission notice appear in all copies. │
9+
│ │
10+
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
11+
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
12+
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
13+
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
14+
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
15+
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
16+
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
17+
│ PERFORMANCE OF THIS SOFTWARE. │
18+
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/math.h"
20+
21+
/**
22+
* Returns 𝑥^𝑦.
23+
*/
24+
long double powl(long double x, long double y) {
25+
long double t, u;
26+
if (!isunordered(x, y)) {
27+
if (!isinf(y)) {
28+
if (!isinf(x)) {
29+
if (x) {
30+
asm("fyl2x" : "=t"(u) : "0"(fabsl(x)), "u"(y) : "st(1)");
31+
asm("fprem" : "=t"(t) : "0"(u), "u"(1.L));
32+
asm("f2xm1" : "=t"(t) : "0"(t));
33+
asm("fscale" : "=t"(t) : "0"(t + 1), "u"(u));
34+
return copysignl(t, x);
35+
} else if (y > 0) {
36+
return 0;
37+
} else if (!y) {
38+
return 1;
39+
} else if (y == truncl(y) && ((int64_t)y & 1)) {
40+
return copysignl(INFINITY, x);
41+
} else {
42+
return INFINITY;
43+
}
44+
} else if (signbit(x)) {
45+
if (!y) return 1;
46+
x = y < 0 ? 0 : INFINITY;
47+
if (y == truncl(y) && ((int64_t)y & 1)) x = -x;
48+
return x;
49+
} else if (y < 0) {
50+
return 0;
51+
} else if (y > 0) {
52+
return INFINITY;
53+
} else {
54+
return 1;
55+
}
56+
} else {
57+
x = fabsl(x);
58+
if (x < 1) return signbit(y) ? INFINITY : 0;
59+
if (x > 1) return signbit(y) ? 0 : INFINITY;
60+
return 1;
61+
}
62+
} else if (!y || x == 1) {
63+
return 1;
64+
} else {
65+
return NAN;
66+
}
67+
}
Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
2-
│vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi│
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
33
╞══════════════════════════════════════════════════════════════════════════════╡
4-
│ Copyright 2020 Justine Alexandra Roberts Tunney │
4+
│ Copyright 2021 Justine Alexandra Roberts Tunney │
55
│ │
66
│ Permission to use, copy, modify, and/or distribute this software for │
77
│ any purpose with or without fee is hereby granted, provided that the │
@@ -16,37 +16,8 @@
1616
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
19-
#include "libc/runtime/pc.internal.h"
20-
#include "libc/macros.internal.h"
19+
#include "libc/math.h"
2120

22-
// Returns 𝑥^𝑦.
23-
//
24-
// @param 𝑥 is an 80-bit long double passed on stack in 16-bytes
25-
// @param 𝑦 is the power, also pushed on stack, in reverse order
26-
// @return result of exponentiation on FPU stack in %st
27-
// @note Sun's fdlibm needs 2kLOC to do this for RISC lool
28-
// @define z=y*log2(fabs(x)),copysign(trunc(exp2(fmod(z,1)))*exp2(z),x)
29-
powl: push %rbp
30-
mov %rsp,%rbp
31-
.profilable
32-
fldt 32(%rbp)
33-
fldt 16(%rbp)
34-
fxam
35-
fstsw
36-
fabs
37-
fyl2x
38-
fld1
39-
fld %st(1)
40-
fprem
41-
f2xm1
42-
faddp
43-
fscale
44-
fxch
45-
fstp %st
46-
test $FPU_C1>>8,%ah
47-
jz 1f
48-
fchs
49-
1: pop %rbp
50-
ret
51-
.endfn powl,globl
52-
.alias powl,__powl_finite
21+
long double __powl_finite(long double x, long double y) {
22+
return powl(x, y);
23+
}

libc/tinymath/truncl.S

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,23 @@
1717
│ PERFORMANCE OF THIS SOFTWARE. │
1818
╚─────────────────────────────────────────────────────────────────────────────*/
1919
#include "libc/macros.internal.h"
20-
.source __FILE__
2120

22-
truncl: .profilable
23-
sub $24,%rsp
24-
fldt 32(%rsp)
25-
fnstcw 14(%rsp)
26-
movzwl 14(%rsp),%eax
21+
// Rounds to integer, toward zero.
22+
//
23+
// @param 𝑥 is long double passed on stack
24+
// @return long double in %st
25+
truncl: pushq %rbp
26+
mov %rsp,%rbp
27+
.profilable
28+
sub $16,%rsp
29+
fnstcw -2(%rbp)
30+
fldt 16(%rbp)
31+
movzwl -2(%rbp),%eax
2732
or $0b1100,%ah # round to zero
28-
mov %ax,12(%rsp)
29-
fldcw 12(%rsp)
33+
mov %ax,-4(%rbp)
34+
fldcw -4(%rbp)
3035
frndint
31-
fldcw 14(%rsp)
32-
add $24,%rsp
36+
fldcw -2(%rbp)
37+
leave
3338
ret
3439
.endfn truncl,globl

0 commit comments

Comments
 (0)