libc: implement common abs for various integer sizes (#23893)
* libc: implement common `abs` for various integer sizes * libc: move imaxabs to inttypes.zig and don't use cInclude * libc: delete `fabs` c implementations because already implemented in compiler_rt * libc: export functions depending on the target libc Previously all the functions that were exported were handled equally, though some may exist and some not inside the same file. Moving the checks inside the file allows handling different functions differently * remove empty ifs in inttypes Co-authored-by: Alex Rønne Petersen <alex@alexrp.com> * remove empty ifs in stdlib Co-authored-by: Alex Rønne Petersen <alex@alexrp.com> * libc: use `@abs` for the absolute value calculation --------- Co-authored-by: Alex Rønne Petersen <alex@alexrp.com>
This commit is contained in:
18
lib/libc/mingw/math/fabsf.c
vendored
18
lib/libc/mingw/math/fabsf.c
vendored
@@ -1,18 +0,0 @@
|
||||
/**
|
||||
* This file has no copyright assigned and is placed in the Public Domain.
|
||||
* This file is part of the mingw-w64 runtime package.
|
||||
* No warranty is given; refer to the file DISCLAIMER.PD within this package.
|
||||
*/
|
||||
float fabsf (float x);
|
||||
|
||||
float
|
||||
fabsf (float x)
|
||||
{
|
||||
#if defined(__x86_64__) || defined(_AMD64_) || defined(__arm__) || defined(_ARM_) || defined(__aarch64__) || defined(_ARM64_)
|
||||
return __builtin_fabsf (x);
|
||||
#elif defined(__i386__) || defined(_X86_)
|
||||
float res = 0.0F;
|
||||
asm volatile ("fabs;" : "=t" (res) : "0" (x));
|
||||
return res;
|
||||
#endif /* defined(__x86_64__) || defined(_AMD64_) || defined(__arm__) || defined(_ARM_) || defined(__aarch64__) || defined(_ARM64_) */
|
||||
}
|
||||
18
lib/libc/mingw/math/fabsl.c
vendored
18
lib/libc/mingw/math/fabsl.c
vendored
@@ -1,18 +0,0 @@
|
||||
/**
|
||||
* This file has no copyright assigned and is placed in the Public Domain.
|
||||
* This file is part of the mingw-w64 runtime package.
|
||||
* No warranty is given; refer to the file DISCLAIMER.PD within this package.
|
||||
*/
|
||||
long double fabsl (long double x);
|
||||
|
||||
long double
|
||||
fabsl (long double x)
|
||||
{
|
||||
#if defined(__x86_64__) || defined(_AMD64_) || defined(__i386__) || defined(_X86_)
|
||||
long double res = 0.0L;
|
||||
asm volatile ("fabs;" : "=t" (res) : "0" (x));
|
||||
return res;
|
||||
#elif defined(__arm__) || defined(_ARM_) || defined(__aarch64__) || defined(_ARM64_)
|
||||
return __builtin_fabsl (x);
|
||||
#endif /* defined(__x86_64__) || defined(_AMD64_) || defined(__i386__) || defined(_X86_) */
|
||||
}
|
||||
7
lib/libc/musl/src/math/aarch64/fabs.c
vendored
7
lib/libc/musl/src/math/aarch64/fabs.c
vendored
@@ -1,7 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
double fabs(double x)
|
||||
{
|
||||
__asm__ ("fabs %d0, %d1" : "=w"(x) : "w"(x));
|
||||
return x;
|
||||
}
|
||||
7
lib/libc/musl/src/math/aarch64/fabsf.c
vendored
7
lib/libc/musl/src/math/aarch64/fabsf.c
vendored
@@ -1,7 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
float fabsf(float x)
|
||||
{
|
||||
__asm__ ("fabs %s0, %s1" : "=w"(x) : "w"(x));
|
||||
return x;
|
||||
}
|
||||
15
lib/libc/musl/src/math/arm/fabs.c
vendored
15
lib/libc/musl/src/math/arm/fabs.c
vendored
@@ -1,15 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
#if __ARM_PCS_VFP && __ARM_FP&8
|
||||
|
||||
double fabs(double x)
|
||||
{
|
||||
__asm__ ("vabs.f64 %P0, %P1" : "=w"(x) : "w"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include "../fabs.c"
|
||||
|
||||
#endif
|
||||
15
lib/libc/musl/src/math/arm/fabsf.c
vendored
15
lib/libc/musl/src/math/arm/fabsf.c
vendored
@@ -1,15 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
#if __ARM_PCS_VFP && !BROKEN_VFP_ASM
|
||||
|
||||
float fabsf(float x)
|
||||
{
|
||||
__asm__ ("vabs.f32 %0, %1" : "=t"(x) : "t"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include "../fabsf.c"
|
||||
|
||||
#endif
|
||||
9
lib/libc/musl/src/math/fabs.c
vendored
9
lib/libc/musl/src/math/fabs.c
vendored
@@ -1,9 +0,0 @@
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
|
||||
double fabs(double x)
|
||||
{
|
||||
union {double f; uint64_t i;} u = {x};
|
||||
u.i &= -1ULL/2;
|
||||
return u.f;
|
||||
}
|
||||
9
lib/libc/musl/src/math/fabsf.c
vendored
9
lib/libc/musl/src/math/fabsf.c
vendored
@@ -1,9 +0,0 @@
|
||||
#include <math.h>
|
||||
#include <stdint.h>
|
||||
|
||||
float fabsf(float x)
|
||||
{
|
||||
union {float f; uint32_t i;} u = {x};
|
||||
u.i &= 0x7fffffff;
|
||||
return u.f;
|
||||
}
|
||||
15
lib/libc/musl/src/math/fabsl.c
vendored
15
lib/libc/musl/src/math/fabsl.c
vendored
@@ -1,15 +0,0 @@
|
||||
#include "libm.h"
|
||||
#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
|
||||
long double fabsl(long double x)
|
||||
{
|
||||
return fabs(x);
|
||||
}
|
||||
#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
|
||||
long double fabsl(long double x)
|
||||
{
|
||||
union ldshape u = {x};
|
||||
|
||||
u.i.se &= 0x7fff;
|
||||
return u.f;
|
||||
}
|
||||
#endif
|
||||
7
lib/libc/musl/src/math/i386/fabs.c
vendored
7
lib/libc/musl/src/math/i386/fabs.c
vendored
@@ -1,7 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
double fabs(double x)
|
||||
{
|
||||
__asm__ ("fabs" : "+t"(x));
|
||||
return x;
|
||||
}
|
||||
7
lib/libc/musl/src/math/i386/fabsf.c
vendored
7
lib/libc/musl/src/math/i386/fabsf.c
vendored
@@ -1,7 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
float fabsf(float x)
|
||||
{
|
||||
__asm__ ("fabs" : "+t"(x));
|
||||
return x;
|
||||
}
|
||||
7
lib/libc/musl/src/math/i386/fabsl.c
vendored
7
lib/libc/musl/src/math/i386/fabsl.c
vendored
@@ -1,7 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
long double fabsl(long double x)
|
||||
{
|
||||
__asm__ ("fabs" : "+t"(x));
|
||||
return x;
|
||||
}
|
||||
16
lib/libc/musl/src/math/mips/fabs.c
vendored
16
lib/libc/musl/src/math/mips/fabs.c
vendored
@@ -1,16 +0,0 @@
|
||||
#if !defined(__mips_soft_float) && defined(__mips_abs2008)
|
||||
|
||||
#include <math.h>
|
||||
|
||||
double fabs(double x)
|
||||
{
|
||||
double r;
|
||||
__asm__("abs.d %0,%1" : "=f"(r) : "f"(x));
|
||||
return r;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include "../fabs.c"
|
||||
|
||||
#endif
|
||||
16
lib/libc/musl/src/math/mips/fabsf.c
vendored
16
lib/libc/musl/src/math/mips/fabsf.c
vendored
@@ -1,16 +0,0 @@
|
||||
#if !defined(__mips_soft_float) && defined(__mips_abs2008)
|
||||
|
||||
#include <math.h>
|
||||
|
||||
float fabsf(float x)
|
||||
{
|
||||
float r;
|
||||
__asm__("abs.s %0,%1" : "=f"(r) : "f"(x));
|
||||
return r;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include "../fabsf.c"
|
||||
|
||||
#endif
|
||||
15
lib/libc/musl/src/math/powerpc/fabs.c
vendored
15
lib/libc/musl/src/math/powerpc/fabs.c
vendored
@@ -1,15 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
#if defined(_SOFT_FLOAT) || defined(__NO_FPRS__) || defined(BROKEN_PPC_D_ASM)
|
||||
|
||||
#include "../fabs.c"
|
||||
|
||||
#else
|
||||
|
||||
double fabs(double x)
|
||||
{
|
||||
__asm__ ("fabs %0, %1" : "=d"(x) : "d"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
#endif
|
||||
15
lib/libc/musl/src/math/powerpc/fabsf.c
vendored
15
lib/libc/musl/src/math/powerpc/fabsf.c
vendored
@@ -1,15 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
#if defined(_SOFT_FLOAT) || defined(__NO_FPRS__)
|
||||
|
||||
#include "../fabsf.c"
|
||||
|
||||
#else
|
||||
|
||||
float fabsf(float x)
|
||||
{
|
||||
__asm__ ("fabs %0, %1" : "=f"(x) : "f"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
#endif
|
||||
7
lib/libc/musl/src/math/powerpc64/fabs.c
vendored
7
lib/libc/musl/src/math/powerpc64/fabs.c
vendored
@@ -1,7 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
double fabs(double x)
|
||||
{
|
||||
__asm__ ("fabs %0, %1" : "=d"(x) : "d"(x));
|
||||
return x;
|
||||
}
|
||||
7
lib/libc/musl/src/math/powerpc64/fabsf.c
vendored
7
lib/libc/musl/src/math/powerpc64/fabsf.c
vendored
@@ -1,7 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
float fabsf(float x)
|
||||
{
|
||||
__asm__ ("fabs %0, %1" : "=f"(x) : "f"(x));
|
||||
return x;
|
||||
}
|
||||
15
lib/libc/musl/src/math/riscv32/fabs.c
vendored
15
lib/libc/musl/src/math/riscv32/fabs.c
vendored
@@ -1,15 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
#if __riscv_flen >= 64
|
||||
|
||||
double fabs(double x)
|
||||
{
|
||||
__asm__ ("fabs.d %0, %1" : "=f"(x) : "f"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include "../fabs.c"
|
||||
|
||||
#endif
|
||||
15
lib/libc/musl/src/math/riscv32/fabsf.c
vendored
15
lib/libc/musl/src/math/riscv32/fabsf.c
vendored
@@ -1,15 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
#if __riscv_flen >= 32
|
||||
|
||||
float fabsf(float x)
|
||||
{
|
||||
__asm__ ("fabs.s %0, %1" : "=f"(x) : "f"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include "../fabsf.c"
|
||||
|
||||
#endif
|
||||
15
lib/libc/musl/src/math/riscv64/fabs.c
vendored
15
lib/libc/musl/src/math/riscv64/fabs.c
vendored
@@ -1,15 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
#if __riscv_flen >= 64
|
||||
|
||||
double fabs(double x)
|
||||
{
|
||||
__asm__ ("fabs.d %0, %1" : "=f"(x) : "f"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include "../fabs.c"
|
||||
|
||||
#endif
|
||||
15
lib/libc/musl/src/math/riscv64/fabsf.c
vendored
15
lib/libc/musl/src/math/riscv64/fabsf.c
vendored
@@ -1,15 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
#if __riscv_flen >= 32
|
||||
|
||||
float fabsf(float x)
|
||||
{
|
||||
__asm__ ("fabs.s %0, %1" : "=f"(x) : "f"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include "../fabsf.c"
|
||||
|
||||
#endif
|
||||
15
lib/libc/musl/src/math/s390x/fabs.c
vendored
15
lib/libc/musl/src/math/s390x/fabs.c
vendored
@@ -1,15 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
#if defined(__HTM__) || __ARCH__ >= 9
|
||||
|
||||
double fabs(double x)
|
||||
{
|
||||
__asm__ ("lpdbr %0, %1" : "=f"(x) : "f"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include "../fabs.c"
|
||||
|
||||
#endif
|
||||
15
lib/libc/musl/src/math/s390x/fabsf.c
vendored
15
lib/libc/musl/src/math/s390x/fabsf.c
vendored
@@ -1,15 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
#if defined(__HTM__) || __ARCH__ >= 9
|
||||
|
||||
float fabsf(float x)
|
||||
{
|
||||
__asm__ ("lpebr %0, %1" : "=f"(x) : "f"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include "../fabsf.c"
|
||||
|
||||
#endif
|
||||
15
lib/libc/musl/src/math/s390x/fabsl.c
vendored
15
lib/libc/musl/src/math/s390x/fabsl.c
vendored
@@ -1,15 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
#if defined(__HTM__) || __ARCH__ >= 9
|
||||
|
||||
long double fabsl(long double x)
|
||||
{
|
||||
__asm__ ("lpxbr %0, %1" : "=f"(x) : "f"(x));
|
||||
return x;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include "../fabsl.c"
|
||||
|
||||
#endif
|
||||
9
lib/libc/musl/src/math/x32/fabs.s
vendored
9
lib/libc/musl/src/math/x32/fabs.s
vendored
@@ -1,9 +0,0 @@
|
||||
.global fabs
|
||||
.type fabs,@function
|
||||
fabs:
|
||||
xor %eax,%eax
|
||||
dec %rax
|
||||
shr %rax
|
||||
movq %rax,%xmm1
|
||||
andpd %xmm1,%xmm0
|
||||
ret
|
||||
7
lib/libc/musl/src/math/x32/fabsf.s
vendored
7
lib/libc/musl/src/math/x32/fabsf.s
vendored
@@ -1,7 +0,0 @@
|
||||
.global fabsf
|
||||
.type fabsf,@function
|
||||
fabsf:
|
||||
mov $0x7fffffff,%eax
|
||||
movq %rax,%xmm1
|
||||
andps %xmm1,%xmm0
|
||||
ret
|
||||
6
lib/libc/musl/src/math/x32/fabsl.s
vendored
6
lib/libc/musl/src/math/x32/fabsl.s
vendored
@@ -1,6 +0,0 @@
|
||||
.global fabsl
|
||||
.type fabsl,@function
|
||||
fabsl:
|
||||
fldt 8(%esp)
|
||||
fabs
|
||||
ret
|
||||
10
lib/libc/musl/src/math/x86_64/fabs.c
vendored
10
lib/libc/musl/src/math/x86_64/fabs.c
vendored
@@ -1,10 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
double fabs(double x)
|
||||
{
|
||||
double t;
|
||||
__asm__ ("pcmpeqd %0, %0" : "=x"(t)); // t = ~0
|
||||
__asm__ ("psrlq $1, %0" : "+x"(t)); // t >>= 1
|
||||
__asm__ ("andps %1, %0" : "+x"(x) : "x"(t)); // x &= t
|
||||
return x;
|
||||
}
|
||||
10
lib/libc/musl/src/math/x86_64/fabsf.c
vendored
10
lib/libc/musl/src/math/x86_64/fabsf.c
vendored
@@ -1,10 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
float fabsf(float x)
|
||||
{
|
||||
float t;
|
||||
__asm__ ("pcmpeqd %0, %0" : "=x"(t)); // t = ~0
|
||||
__asm__ ("psrld $1, %0" : "+x"(t)); // t >>= 1
|
||||
__asm__ ("andps %1, %0" : "+x"(x) : "x"(t)); // x &= t
|
||||
return x;
|
||||
}
|
||||
7
lib/libc/musl/src/math/x86_64/fabsl.c
vendored
7
lib/libc/musl/src/math/x86_64/fabsl.c
vendored
@@ -1,7 +0,0 @@
|
||||
#include <math.h>
|
||||
|
||||
long double fabsl(long double x)
|
||||
{
|
||||
__asm__ ("fabs" : "+t"(x));
|
||||
return x;
|
||||
}
|
||||
6
lib/libc/musl/src/stdlib/abs.c
vendored
6
lib/libc/musl/src/stdlib/abs.c
vendored
@@ -1,6 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
int abs(int a)
|
||||
{
|
||||
return a>0 ? a : -a;
|
||||
}
|
||||
6
lib/libc/musl/src/stdlib/imaxabs.c
vendored
6
lib/libc/musl/src/stdlib/imaxabs.c
vendored
@@ -1,6 +0,0 @@
|
||||
#include <inttypes.h>
|
||||
|
||||
intmax_t imaxabs(intmax_t a)
|
||||
{
|
||||
return a>0 ? a : -a;
|
||||
}
|
||||
6
lib/libc/musl/src/stdlib/labs.c
vendored
6
lib/libc/musl/src/stdlib/labs.c
vendored
@@ -1,6 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
long labs(long a)
|
||||
{
|
||||
return a>0 ? a : -a;
|
||||
}
|
||||
6
lib/libc/musl/src/stdlib/llabs.c
vendored
6
lib/libc/musl/src/stdlib/llabs.c
vendored
@@ -1,6 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
long long llabs(long long a)
|
||||
{
|
||||
return a>0 ? a : -a;
|
||||
}
|
||||
Reference in New Issue
Block a user