Port a lot more code to AARCH64

- Introduce epoll_pwait()
- Rewrite -ftrapv and ffs() libraries in C code
- Use more FreeBSD code in math function library
- Get significantly more tests passing on qemu-aarch64
- Fix many Musl long double functions that were broken on AARCH64
This commit is contained in:
Justine Tunney 2023-05-14 09:32:15 -07:00
parent 91791e9f38
commit 550b52abf6
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
158 changed files with 6018 additions and 3499 deletions

View file

@ -17,11 +17,33 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/macros.internal.h"
/**
* Returns absolute value of x.
* Returns absolute value of 𝑥.
*
* This function is a footgun since your argument may be narrrowed.
* Consider using labs(), llabs(), or better yet a macro like this:
*
* #define ABS(X) ((X) >= 0 ? (X) : -(X))
*
* Note that passing `x` as `INT_MIN` is undefined behavior, which
* depends on whether or not your c library as well as the objects
* that call it were built using the `-fwrapv` or `-ftrapv` flags.
*/
int abs(int x) {
return ABS(x);
return x < 0 ? -x : x;
}
/**
* Returns absolute value of 𝑥.
*/
long labs(long x) {
return x < 0 ? -x : x;
}
/**
* Returns absolute value of 𝑥.
*/
long long llabs(long long x) {
return x < 0 ? -x : x;
}

View file

@ -1,39 +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 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.internal.h"
.privileged
.alignfunc
// Returns 𝑥+𝑦, aborting on overflow.
//
// @param edi is int32 𝑥
// @param esi is int32 𝑦
// @return eax is 𝑥+𝑦
// @see -ftrapv
__addvsi3:
mov %edi,%eax
add %esi,%eax
jo 1f
ret
1: push %rbp
mov %rsp,%rbp
call __on_arithmetic_overflow
pop %rbp
ret
.endfn __addvsi3,globl

View file

@ -1,41 +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 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.internal.h"
.privileged
.alignfunc
// Returns 𝑥+𝑦, aborting on overflow.
//
// @param rdi:rsi is int128 𝑥
// @param rdx:rcx is int128 𝑦
// @return rdx:rax is 𝑥+𝑦
// @see -ftrapv
__addvti3:
mov %rdi,%rax
add %rdx,%rax
mov %rsi,%rdx
adc %rcx,%rdx
jo 1f
ret
1: push %rbp
mov %rsp,%rbp
call __on_arithmetic_overflow
pop %rbp
ret
.endfn __addvti3,globl

View file

@ -1,7 +1,7 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Copyright 2023 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
@ -16,15 +16,25 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
.privileged
#include "libc/intrin/intrin.h"
// Arithmetic overflow handler.
// @see -ftrapv
__on_arithmetic_overflow:
push %rbp
mov %rsp,%rbp
int3
0: ud2
jmp 0b
.endfn __on_arithmetic_overflow,weak
/**
* Finds lowest set bit in word.
*/
int ffs(int x) {
return __builtin_ffs(x);
}
/**
* Finds lowest set bit in word.
*/
long ffsl(long x) {
return __builtin_ffsl(x);
}
/**
* Finds lowest set bit in word.
*/
long long ffsll(long long x) {
return __builtin_ffsll(x);
}

186
libc/intrin/ftrapv.c Normal file
View file

@ -0,0 +1,186 @@
/*-*- 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 2023 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/intrin.h"
#include "libc/limits.h"
#include "libc/runtime/internal.h"
/**
* Returns -𝑥, aborting on overflow.
*
* @see __on_arithmetic_overflow()
* @see -ftrapv to enable
*/
int __negvsi2(int x) {
if (x == INT_MIN) {
__on_arithmetic_overflow();
}
return -x;
}
/**
* Returns -𝑥 on overflow.
*
* @see __on_arithmetic_overflow()
* @see -ftrapv to enable
*/
long __negvdi2(long x) {
if (x == LONG_MIN) {
__on_arithmetic_overflow();
}
return -x;
}
/**
* Returns -𝑥, aborting on overflow.
*
* @see __on_arithmetic_overflow()
* @see -ftrapv to enable
*/
int128_t __negvti2(int128_t x) {
if (x == INT128_MIN) {
__on_arithmetic_overflow();
}
return -x;
}
/**
* Returns 𝑥+𝑦, aborting on overflow.
*
* @see __on_arithmetic_overflow()
* @see -ftrapv to enable
*/
int __addvsi3(int x, int y) {
int z;
if (__builtin_add_overflow(x, y, &z)) {
__on_arithmetic_overflow();
}
return z;
}
/**
* Returns 𝑥+𝑦, aborting on overflow.
*
* @see __on_arithmetic_overflow()
* @see -ftrapv to enable
*/
long __addvdi3(long x, long y) {
long z;
if (__builtin_add_overflow(x, y, &z)) {
__on_arithmetic_overflow();
}
return z;
}
/**
* Returns 𝑥+𝑦, aborting on overflow.
*
* @see __on_arithmetic_overflow()
* @see -ftrapv to enable
*/
int128_t __addvti3(int128_t x, int128_t y) {
int128_t z;
if (__builtin_add_overflow(x, y, &z)) {
__on_arithmetic_overflow();
}
return z;
}
/**
* Returns 𝑥-𝑦, aborting on overflow.
*
* @see __on_arithmetic_overflow()
* @see -ftrapv to enable
*/
int __subvsi3(int x, int y) {
int z;
if (__builtin_sub_overflow(x, y, &z)) {
__on_arithmetic_overflow();
}
return z;
}
/**
* Returns 𝑥-𝑦, aborting on overflow.
*
* @see __on_arithmetic_overflow()
* @see -ftrapv to enable
*/
long __subvdi3(long x, long y) {
long z;
if (__builtin_sub_overflow(x, y, &z)) {
__on_arithmetic_overflow();
}
return z;
}
/**
* Returns 𝑥-𝑦, aborting on overflow.
*
* @see __on_arithmetic_overflow()
* @see -ftrapv to enable
*/
int128_t __subvti3(int128_t x, int128_t y) {
int128_t z;
if (__builtin_sub_overflow(x, y, &z)) {
__on_arithmetic_overflow();
}
return z;
}
/**
* Returns 𝑥*𝑦, aborting on overflow.
*
* @see __on_arithmetic_overflow()
* @see -ftrapv to enable
*/
int __mulvsi3(int x, int y) {
int z;
if (__builtin_mul_overflow(x, y, &z)) {
__on_arithmetic_overflow();
}
return z;
}
/**
* Returns 𝑥*𝑦, aborting on overflow.
*
* @see __on_arithmetic_overflow()
* @see -ftrapv to enable
*/
long __mulvdi3(long x, long y) {
long z;
if (__builtin_mul_overflow(x, y, &z)) {
__on_arithmetic_overflow();
}
return z;
}
/**
* Returns 𝑥*𝑦, aborting on overflow.
*
* @see __on_arithmetic_overflow()
* @see -ftrapv to enable
*/
int128_t __mulvti3(int128_t x, int128_t y) {
int128_t z;
if (__builtin_mul_overflow(x, y, &z)) {
__on_arithmetic_overflow();
}
return z;
}

View file

@ -126,6 +126,14 @@ o/$(MODE)/libc/intrin/restorewintty.o: private \
OVERRIDE_CFLAGS += \
-fno-sanitize=all
# we can't use -ftrapv because:
# this file implements it
o/$(MODE)/libc/intrin/ftrapv.o: private \
OVERRIDE_CFLAGS += \
-ffunction-sections \
-ffreestanding \
-fwrapv
# we can't use asan because:
# sys_mmap() calls these which sets up shadow memory
o/$(MODE)/libc/intrin/describeflags.o \

View file

@ -1,39 +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 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.internal.h"
.privileged
.alignfunc
// Returns 𝑥*𝑦, aborting on overflow.
//
// @param rdi is int64 𝑥
// @param rdi is int64 𝑦
// @return rax is 𝑥*𝑦
// @see -ftrapv
__mulvdi3:
mov %rdi,%rax
imul %rsi
jc 1f
ret
1: push %rbp
mov %rsp,%rbp
call __on_arithmetic_overflow
pop %rbp
ret
.endfn __mulvdi3,globl

View file

@ -1,39 +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 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.internal.h"
.privileged
.alignfunc
// Returns 𝑥*𝑦, aborting on overflow.
//
// @param edi is int32 𝑥
// @param esi is int32 𝑦
// @return eax is 𝑥*𝑦
// @see -ftrapv
__mulvsi3:
mov %edi,%eax
imul %esi
jc 1f
ret
1: push %rbp
mov %rsp,%rbp
call __on_arithmetic_overflow
pop %rbp
ret
.endfn __mulvsi3,globl

View file

@ -1,134 +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 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.internal.h"
.privileged
.alignfunc
// Returns 𝑥*𝑦, aborting on overflow.
//
// @param rdi:rsi is int128 𝑥
// @param rdx:rcx is int128 𝑦
// @return rdx:rax is 𝑥*𝑦
// @see -ftrapv
__mulvti3:
push %rbp
mov %rsp,%rbp
push %rbx
push %rbx
push %r12
push %r13
push %r14
push %r15
mov %rdx,%r10
mov %rdi,%rdx
xor %r11d,%r11d
mov %r10,%rax
sar $63,%rdx
sar $63,%rax
cmp %rsi,%rdx
jne 4f
cmp %rcx,%rax
jne 5f
mov %rdi,%rax
imul %r10
mov %rax,%r14
mov %rdx,%r8
jmp 2f
5: mov %r10,%r12
mov %rcx,%r13
mov %rcx,%r8
mov %rdi,%rbx
jmp 6f
4: cmp %rcx,%rax
jne 7f
mov %rdi,%r12
mov %rsi,%r13
mov %rsi,%r8
mov %r10,%rbx
6: mov %rdi,%rax
mul %r10
mov %rax,%r14
mov %rbx,%rax
mov %rdx,%r15
mul %r8
test %r8,%r8
jns 8f
xor %r8d,%r8d
sub %r8,%rax
sbb %rbx,%rdx
8: test %rbx,%rbx
jns 9f
sub %r12,%rax
sbb %r13,%rdx
9: mov %r15,%r8
xor %r9d,%r9d
add %rax,%r8
adc %rdx,%r9
mov %r8,%rdx
sar $63,%rdx
cmp %r9,%rdx
je 2f
imul %r10,%rsi
mov %rdi,%rax
imul %rdi,%rcx
mul %r10
lea (%rsi,%rcx),%r8
add %rdx,%r8
mov %rax,%r14
jmp 3f
7: mov %rsi,%r8
mov %rcx,%rdx
mov %rdi,%rax
imul %rdi,%rdx
imul %r10,%r8
add %rdx,%r8
mul %r10
mov %rax,%r14
lea 1(%rsi),%rax
add %rdx,%r8
cmp $1,%rax
ja 3f
lea 1(%rcx),%rax
cmp $1,%rax
ja 3f
cmp %rcx,%rsi
jne 11f
cmp %r14,%r11
mov %r11,%rax
sbb %r8,%rax
jl 2f
jmp 3f
11: test %r8,%r8
js 2f
3: mov $1,%r11d
2: test %r11,%r11
je 1f
mov %r8,-8(%rbp)
call __on_arithmetic_overflow
mov -8(%rbp),%r8
1: mov %r14,%rax
mov %r8,%rdx
pop %r15
pop %r14
pop %r13
pop %r12
pop %rbx
leave
ret
.endfn __mulvti3,globl

View file

@ -1,38 +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 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.internal.h"
.privileged
.alignfunc
// Returns -𝑥, aborting on overflow (two's complement bane).
//
// @param rdi is int64 𝑥
// @return rax is -𝑥
// @see -ftrapv
__negvdi2:
mov %rdi,%rax
neg %rax
jo 1f
ret
1: push %rbp
mov %rsp,%rbp
call __on_arithmetic_overflow
pop %rbp
ret
.endfn __negvdi2,globl

View file

@ -1,38 +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 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.internal.h"
.privileged
.alignfunc
// Returns -𝑥, aborting on overflow (two's complement bane).
//
// @param edi is int32 𝑥
// @return eax is -𝑥
// @see -ftrapv
__negvsi2:
mov %edi,%eax
neg %eax
jo 1f
ret
1: push %rbp
mov %rsp,%rbp
call __on_arithmetic_overflow
pop %rbp
ret
.endfn __negvsi2,globl

View file

@ -1,42 +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 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.internal.h"
.privileged
.alignfunc
// Returns -𝑥, aborting on overflow.
//
// @param rdi:rsi is int128 𝑥
// @return rdx:rax is -𝑥
// @see -ftrapv
__negvti2:
mov %rdi,%rax
mov %rsi,%rdx
neg %rax
not %rdx
cmc
adc $0,%rdx
jo 1f
ret
1: push %rbp
mov %rsp,%rbp
call __on_arithmetic_overflow
pop %rbp
ret
.endfn __negvti2,globl

View file

@ -1,7 +1,7 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
Copyright 2023 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
@ -16,24 +16,21 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
.privileged
.alignfunc
#include "libc/intrin/intrin.h"
#include "libc/intrin/kprintf.h"
// Returns 𝑥+𝑦, aborting on overflow.
//
// @param rdi is int64 𝑥
// @param rsi is int64 𝑦
// @return rax is 𝑥+𝑦
// @see -ftrapv
__addvdi3:
mov %rdi,%rax
add %rsi,%rax
jo 1f
ret
1: push %rbp
mov %rsp,%rbp
call __on_arithmetic_overflow
pop %rbp
ret
.endfn __addvdi3,globl
/**
* Arithmetic overflow handler.
*
* This function is provided weakly, so that programs which depend on
* this library may define it themselves. This default implementation
* will print a message to standard error and raise SIGTRAP. A custom
* implementation may return from this function, in which case the op
* shall have `-fwrapv` i.e. signed two's complement behavior.
*
* @see -ftrapv
*/
__attribute__((__weak__)) void __on_arithmetic_overflow(void) {
kprintf("error: -ftrapv caught arithmetic overflow\n");
__builtin_trap();
}

View file

@ -1,39 +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 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.internal.h"
.privileged
.alignfunc
// Returns 𝑥-𝑦, aborting on overflow.
//
// @param rdi is int64 𝑥
// @param rsi is int64 𝑦
// @return rax is 𝑥-𝑦
// @see -ftrapv
__subvdi3:
mov %rdi,%rax
sub %rsi,%rax
jo 1f
ret
1: push %rbp
mov %rsp,%rbp
call __on_arithmetic_overflow
pop %rbp
ret
.endfn __subvdi3,globl

View file

@ -1,38 +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 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.internal.h"
.privileged
.alignfunc
// Returns 𝑥-𝑦, aborting on overflow.
//
// @param edi is int32 𝑥
// @param esi is int32 𝑦
// @see -ftrapv
__subvsi3:
mov %edi,%eax
sub %esi,%eax
jo 1f
ret
1: push %rbp
mov %rsp,%rbp
call __on_arithmetic_overflow
pop %rbp
ret
.endfn __subvsi3,globl

View file

@ -1,41 +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 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.internal.h"
.privileged
.alignfunc
// Returns 𝑥-𝑦, aborting on overflow.
//
// @param rdi:rsi is int128 𝑥
// @param rdx:rcx is int128 𝑦
// @return rdx:rax is 𝑥-𝑦
// @see -ftrapv
__subvti3:
mov %rdi,%rax
sub %rdx,%rax
mov %rsi,%rdx
sbb %rcx,%rdx
jo 1f
ret
1: push %rbp
mov %rsp,%rbp
call __on_arithmetic_overflow
pop %rbp
ret
.endfn __subvti3,globl