mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-22 21:32:31 +00:00
Introduce native support for MacOS ARM64
There's a new program named ape/ape-m1.c which will be used to build an embeddable binary that can load ape and elf executables. The support is mostly working so far, but still chasing down ABI issues.
This commit is contained in:
parent
b852650c08
commit
1422e96b4e
757 changed files with 2988 additions and 1321 deletions
|
@ -30,3 +30,7 @@ double copysign(double x, double y) {
|
|||
ux.i |= uy.i & 1ULL << 63;
|
||||
return ux.f;
|
||||
}
|
||||
|
||||
#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
|
||||
__strong_reference(copysign, copysignl);
|
||||
#endif
|
||||
|
|
|
@ -18,19 +18,16 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/math.h"
|
||||
#include "libc/tinymath/ldshape.internal.h"
|
||||
#if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024)
|
||||
|
||||
/**
|
||||
* Returns 𝑥 with same sign as 𝑦.
|
||||
*/
|
||||
long double copysignl(long double x, long double y) {
|
||||
#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
|
||||
return copysign(x, y);
|
||||
#elif (LDBL_MANT_DIG == 64 || LDBL_MANT_DIG == 113) && LDBL_MAX_EXP == 16384
|
||||
union ldshape ux = {x}, uy = {y};
|
||||
ux.i.se &= 0x7fff;
|
||||
ux.i.se |= uy.i.se & 0x8000;
|
||||
return ux.f;
|
||||
#else
|
||||
#error "architecture unsupported"
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* long double is long */
|
||||
|
|
|
@ -92,40 +92,41 @@ static void mul(uint64_t *hi, uint64_t *lo, uint64_t x, uint64_t y)
|
|||
*/
|
||||
double fma(double x, double y, double z)
|
||||
{
|
||||
#if defined(__x86_64__) && defined(__FMA__)
|
||||
#if defined(__x86_64__) && defined(__FMA__) && defined(__FAST_MATH__)
|
||||
|
||||
// Intel Haswell+ (c. 2013)
|
||||
// AMD Piledriver+ (c. 2011)
|
||||
asm("vfmadd132sd\t%1,%2,%0" : "+x"(x) : "x"(y), "x"(z));
|
||||
return x;
|
||||
|
||||
#elif defined(__x86_64__) && defined(__FMA4__)
|
||||
#elif defined(__x86_64__) && defined(__FMA4__) && defined(__FAST_MATH__)
|
||||
|
||||
// AMD Bulldozer+ (c. 2011)
|
||||
asm("vfmaddsd\t%3,%2,%1,%0" : "=x"(x) : "x"(x), "x"(y), "x"(z));
|
||||
return x;
|
||||
|
||||
#elif defined(__aarch64__)
|
||||
#elif defined(__aarch64__) && defined(__FAST_MATH__)
|
||||
|
||||
asm("fmadd\t%d0,%d1,%d2,%d3" : "=w"(x) : "w"(x), "w"(y), "w"(z));
|
||||
return x;
|
||||
|
||||
#elif defined(__powerpc64__)
|
||||
#elif defined(__powerpc64__) && defined(__FAST_MATH__)
|
||||
|
||||
asm("fmadd\t%0,%1,%2,%3" : "=d"(x) : "d"(x), "d"(y), "d"(z));
|
||||
return x;
|
||||
|
||||
#elif defined(__riscv) && __riscv_flen >= 64
|
||||
#elif defined(__riscv) && __riscv_flen >= 64 && defined(__FAST_MATH__)
|
||||
|
||||
asm("fmadd.d\t%0,%1,%2,%3" : "=f"(x) : "f"(x), "f"(y), "f"(z));
|
||||
return x;
|
||||
|
||||
#elif defined(__s390x__)
|
||||
#elif defined(__s390x__) && defined(__FAST_MATH__)
|
||||
|
||||
asm("madbr\t%0,\t%1,\t%2" : "+f"(z) : "f"(x), "f"(y));
|
||||
return z;
|
||||
|
||||
#else
|
||||
// #pragma STDC FENV_ACCESS ON
|
||||
|
||||
/* normalize so top 10bits and last bit are 0 */
|
||||
struct num nx, ny, nz;
|
||||
|
|
|
@ -17,15 +17,19 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/math.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
/**
|
||||
* Adds doubles in array.
|
||||
*/
|
||||
double fsum(const double *p, size_t n) {
|
||||
size_t i;
|
||||
double s;
|
||||
if (n > 8) return fsum(p, n / 2) + fsum(p + n / 2, n - n / 2);
|
||||
for (s = i = 0; i < n; ++i) s += p[i];
|
||||
return s;
|
||||
double err, sum, t, y;
|
||||
sum = err = 0;
|
||||
for (i = 0; i < n; ++i) {
|
||||
y = p[i] - err;
|
||||
t = sum + y;
|
||||
err = (t - sum) - y;
|
||||
sum = t;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/math.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
/**
|
||||
* Adds floats in array.
|
||||
|
|
|
@ -61,3 +61,7 @@ int ilogb(double x)
|
|||
}
|
||||
return e - 0x3ff;
|
||||
}
|
||||
|
||||
#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
|
||||
__strong_reference(ilogb, ilogbl);
|
||||
#endif
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
#include "libc/math.h"
|
||||
#include "libc/tinymath/internal.h"
|
||||
#include "libc/tinymath/ldshape.internal.h"
|
||||
#if !(LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024)
|
||||
|
||||
asm(".ident\t\"\\n\\n\
|
||||
Musl libc (MIT License)\\n\
|
||||
|
@ -39,11 +40,10 @@ asm(".include \"libc/disclaimer.inc\"");
|
|||
/**
|
||||
* Returns log₂𝑥 exponent part of double.
|
||||
*/
|
||||
int ilogbl(long double x) {
|
||||
#if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
|
||||
return ilogb(x);
|
||||
#elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
|
||||
// #pragma STDC FENV_ACCESS ON
|
||||
int ilogbl(long double x)
|
||||
{
|
||||
#if LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
|
||||
// #pragma STDC FENV_ACCESS ON
|
||||
union ldshape u = {x};
|
||||
uint64_t m = u.i.m;
|
||||
int e = u.i.se & 0x7fff;
|
||||
|
@ -63,7 +63,7 @@ int ilogbl(long double x) {
|
|||
}
|
||||
return e - 0x3fff;
|
||||
#elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
|
||||
// #pragma STDC FENV_ACCESS ON
|
||||
// #pragma STDC FENV_ACCESS ON
|
||||
union ldshape u = {x};
|
||||
int e = u.i.se & 0x7fff;
|
||||
|
||||
|
@ -82,8 +82,7 @@ int ilogbl(long double x) {
|
|||
return u.f ? FP_ILOGBNAN : INT_MAX;
|
||||
}
|
||||
return e - 0x3fff;
|
||||
#else
|
||||
#error "architecture unsupported"
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif /* long double is long */
|
||||
|
|
|
@ -67,7 +67,7 @@ as a double.
|
|||
#define EPS LDBL_EPSILON
|
||||
#endif
|
||||
static dontinline long lrint_slow(double x) {
|
||||
// #pragma STDC FENV_ACCESS ON
|
||||
// #pragma STDC FENV_ACCESS ON
|
||||
int e;
|
||||
e = fetestexcept(FE_INEXACT);
|
||||
x = rint(x);
|
||||
|
|
|
@ -38,6 +38,9 @@ Copyright 2005-2014 Rich Felker, et. al.\"");
|
|||
asm(".include \"libc/disclaimer.inc\"");
|
||||
// clang-format off
|
||||
|
||||
/**
|
||||
* Rounds to nearest integer.
|
||||
*/
|
||||
long lrintl(long double x)
|
||||
{
|
||||
#ifdef FE_INEXACT
|
||||
|
@ -48,7 +51,7 @@ Note that if LONG_MAX == 0x7fffffffffffffff && LDBL_MANT_DIG == 64
|
|||
then x == 2**63 - 0.5 is the only input that overflows and
|
||||
raises inexact (with tonearest or upward rounding mode)
|
||||
*/
|
||||
// #pragma STDC FENV_ACCESS ON
|
||||
// #pragma STDC FENV_ACCESS ON
|
||||
int e;
|
||||
|
||||
e = fetestexcept(FE_INEXACT);
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/tinymath/magicu.h"
|
||||
#include "libc/assert.h"
|
||||
|
||||
/**
|
||||
* Precomputes magic numbers for unsigned division by constant.
|
||||
|
@ -27,7 +28,7 @@
|
|||
* assert(77 / 7 == __magicu_div(77, __magicu_get(7)));
|
||||
*
|
||||
* @param d is intended divisor, which must not be zero
|
||||
* @return magic divisor
|
||||
* @return magic divisor (never zero)
|
||||
*/
|
||||
struct magicu __magicu_get(uint32_t d) {
|
||||
// From Hacker's Delight by Henry S. Warren Jr., 9780321842688
|
||||
|
@ -35,6 +36,7 @@ struct magicu __magicu_get(uint32_t d) {
|
|||
int a, p;
|
||||
struct magicu magu;
|
||||
uint32_t p32, q, r, delta;
|
||||
_npassert(d); // Can't divide by zero.
|
||||
p32 = 0; // Avoid compiler warning.
|
||||
a = 0; // Initialize "add" indicator.
|
||||
p = 31; // Initialize p.
|
||||
|
@ -58,8 +60,9 @@ struct magicu __magicu_get(uint32_t d) {
|
|||
}
|
||||
delta = d - 1 - r;
|
||||
} while (p < 64 && p32 < delta);
|
||||
magu.M = q + 1; // Magic number and
|
||||
magu.s = p - 32; // Shift amount to return
|
||||
if (a) magu.s |= 64; // Sets "add" indicator
|
||||
magu.M = q + 1; // Magic number and
|
||||
magu.s = p - 32; // Shift amount to return
|
||||
if (a) magu.s |= 64; // Sets "add" indicator
|
||||
_npassert(magu.M || magu.s); // Never returns zero.
|
||||
return magu;
|
||||
}
|
||||
|
|
|
@ -21,6 +21,15 @@ forceinline uint32_t __magicu_div(uint32_t x, struct magicu d) {
|
|||
return ((((uint64_t)x * d.M) >> 32) + ((d.s & 64) ? x : 0)) >> (d.s & 63);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if 𝑑 contains a valid initialized divisor.
|
||||
*/
|
||||
static inline bool __magicu_valid(struct magicu d) {
|
||||
if (!d.M && !d.s) return false; /* uninitialized */
|
||||
if (d.s & ~(64 | 63)) return false; /* corrupted */
|
||||
return true;
|
||||
}
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_LIBC_TINYMATH_MAGICU_H_ */
|
||||
|
|
|
@ -2,88 +2,78 @@
|
|||
│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│
|
||||
╚──────────────────────────────────────────────────────────────────────────────╝
|
||||
│ │
|
||||
│ FreeBSD lib/msun/src/s_tanhf.c │
|
||||
│ Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. │
|
||||
│ Musl Libc │
|
||||
│ Copyright © 2005-2014 Rich Felker, et al. │
|
||||
│ │
|
||||
│ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. │
|
||||
│ Permission is hereby granted, free of charge, to any person obtaining │
|
||||
│ a copy of this software and associated documentation files (the │
|
||||
│ "Software"), to deal in the Software without restriction, including │
|
||||
│ without limitation the rights to use, copy, modify, merge, publish, │
|
||||
│ distribute, sublicense, and/or sell copies of the Software, and to │
|
||||
│ permit persons to whom the Software is furnished to do so, subject to │
|
||||
│ the following conditions: │
|
||||
│ │
|
||||
│ Developed at SunPro, a Sun Microsystems, Inc. business. │
|
||||
│ Permission to use, copy, modify, and distribute this │
|
||||
│ software is freely granted, provided that this notice │
|
||||
│ is preserved. │
|
||||
│ The above copyright notice and this permission notice shall be │
|
||||
│ included in all copies or substantial portions of the Software. │
|
||||
│ │
|
||||
│ Copyright (c) 1992-2023 The FreeBSD Project. │
|
||||
│ │
|
||||
│ Redistribution and use in source and binary forms, with or without │
|
||||
│ modification, are permitted provided that the following conditions │
|
||||
│ are met: │
|
||||
│ 1. Redistributions of source code must retain the above copyright │
|
||||
│ notice, this list of conditions and the following disclaimer. │
|
||||
│ 2. Redistributions in binary form must reproduce the above copyright │
|
||||
│ notice, this list of conditions and the following disclaimer in the │
|
||||
│ documentation and/or other materials provided with the distribution. │
|
||||
│ │
|
||||
│ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND │
|
||||
│ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE │
|
||||
│ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE │
|
||||
│ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE │
|
||||
│ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL │
|
||||
│ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS │
|
||||
│ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) │
|
||||
│ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT │
|
||||
│ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY │
|
||||
│ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF │
|
||||
│ SUCH DAMAGE. │
|
||||
│ 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 OR COPYRIGHT HOLDERS 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/math.h"
|
||||
#include "libc/tinymath/freebsd.internal.h"
|
||||
#include "libc/tinymath/internal.h"
|
||||
|
||||
asm(".ident\t\"\\n\\n\
|
||||
FreeBSD libm (BSD-2 License)\\n\
|
||||
Copyright (c) 2005-2011, Bruce D. Evans, Steven G. Kargl, David Schultz.\"");
|
||||
asm(".ident\t\"\\n\\n\
|
||||
fdlibm (fdlibm license)\\n\
|
||||
Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\"");
|
||||
Musl libc (MIT License)\\n\
|
||||
Copyright 2005-2014 Rich Felker, et. al.\"");
|
||||
asm(".include \"libc/disclaimer.inc\"");
|
||||
// clang-format off
|
||||
|
||||
static const volatile float tiny = 1.0e-30;
|
||||
static const float one=1.0, two=2.0, huge = 1.0e30;
|
||||
|
||||
/**
|
||||
* Returns hyperbolic tangent of 𝑥.
|
||||
*
|
||||
* @define `tanhf(x)=(expf(x)-expf(-x))/(expf(x)+expf(-x))`
|
||||
* @define `tanhf(x)=(expf(2.f*x)-1.f)/(expf(2.f*x)-1.f+2.f)`
|
||||
*/
|
||||
float
|
||||
tanhf(float x)
|
||||
float tanhf(float x)
|
||||
{
|
||||
float t,z;
|
||||
int32_t jx,ix;
|
||||
union {float f; uint32_t i;} u = {.f = x};
|
||||
uint32_t w;
|
||||
int sign;
|
||||
float t;
|
||||
|
||||
GET_FLOAT_WORD(jx,x);
|
||||
ix = jx&0x7fffffff;
|
||||
/* x = |x| */
|
||||
sign = u.i >> 31;
|
||||
u.i &= 0x7fffffff;
|
||||
x = u.f;
|
||||
w = u.i;
|
||||
|
||||
/* x is INF or NaN */
|
||||
if(ix>=0x7f800000) {
|
||||
if (jx>=0) return one/x+one; /* tanh(+-inf)=+-1 */
|
||||
else return one/x-one; /* tanh(NaN) = NaN */
|
||||
}
|
||||
|
||||
/* |x| < 9 */
|
||||
if (ix < 0x41100000) { /* |x|<9 */
|
||||
if (ix<0x39800000) { /* |x|<2**-12 */
|
||||
if(huge+x>one) return x; /* tanh(tiny) = tiny with inexact */
|
||||
}
|
||||
if (ix>=0x3f800000) { /* |x|>=1 */
|
||||
t = expm1f(two*fabsf(x));
|
||||
z = one - two/(t+two);
|
||||
} else {
|
||||
t = expm1f(-two*fabsf(x));
|
||||
z= -t/(t+two);
|
||||
}
|
||||
/* |x| >= 9, return +-1 */
|
||||
if (w > 0x3f0c9f54) {
|
||||
/* |x| > log(3)/2 ~= 0.5493 or nan */
|
||||
if (w > 0x41200000) {
|
||||
/* |x| > 10 */
|
||||
t = 1 + 0/x;
|
||||
} else {
|
||||
t = expm1f(2*x);
|
||||
t = 1 - 2/(t+2);
|
||||
}
|
||||
} else if (w > 0x3e82c578) {
|
||||
/* |x| > log(5/3)/2 ~= 0.2554 */
|
||||
t = expm1f(2*x);
|
||||
t = t/(t+2);
|
||||
} else if (w >= 0x00800000) {
|
||||
/* |x| >= 0x1p-126 */
|
||||
t = expm1f(-2*x);
|
||||
t = -t/(t+2);
|
||||
} else {
|
||||
z = one - tiny; /* raise inexact flag */
|
||||
/* |x| is subnormal */
|
||||
FORCE_EVAL(x*x);
|
||||
t = x;
|
||||
}
|
||||
return (jx>=0)? z: -z;
|
||||
return sign ? -t : t;
|
||||
}
|
||||
|
|
|
@ -42,13 +42,6 @@ $(LIBC_TINYMATH_A).pkg: \
|
|||
$(LIBC_TINYMATH_A_OBJS) \
|
||||
$(foreach x,$(LIBC_TINYMATH_A_DIRECTDEPS),$($(x)_A).pkg)
|
||||
|
||||
o/$(MODE)/libc/tinymath/cpow.o \
|
||||
o/$(MODE)/libc/tinymath/cpowf.o \
|
||||
o/$(MODE)/libc/tinymath/cpowl.o \
|
||||
o/$(MODE)/libc/tinymath/powfin.o: private \
|
||||
OVERRIDE_CFLAGS += \
|
||||
-ffast-math
|
||||
|
||||
o/$(MODE)/libc/tinymath/lround.o \
|
||||
o/$(MODE)/libc/tinymath/lroundf.o \
|
||||
o/$(MODE)/libc/tinymath/lroundl.o: private \
|
||||
|
@ -60,6 +53,18 @@ o/$(MODE)/libc/tinymath/loglq.o: private \
|
|||
OVERRIDE_CFLAGS += \
|
||||
-ffunction-sections
|
||||
|
||||
$(LIBC_TINYMATH_A_OBJS): private \
|
||||
OVERRIDE_CFLAGS += \
|
||||
-fsigned-zeros \
|
||||
-ftrapping-math \
|
||||
-frounding-math \
|
||||
-fsignaling-nans \
|
||||
-fno-reciprocal-math \
|
||||
-fno-associative-math \
|
||||
-fno-finite-math-only \
|
||||
-fno-cx-limited-range \
|
||||
-ffp-int-builtin-inexact
|
||||
|
||||
LIBC_TINYMATH_LIBS = $(foreach x,$(LIBC_TINYMATH_ARTIFACTS),$($(x)))
|
||||
LIBC_TINYMATH_HDRS = $(foreach x,$(LIBC_TINYMATH_ARTIFACTS),$($(x)_HDRS))
|
||||
LIBC_TINYMATH_SRCS = $(foreach x,$(LIBC_TINYMATH_ARTIFACTS),$($(x)_SRCS))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue