AK: Don't use x86 assembly when building for non-x86 targets

This allows Lagom to be built successfully for Apple M1.

Fixes #12644.
This commit is contained in:
Gunnar Beutner 2022-02-19 10:37:05 +01:00 committed by Idan Horowitz
parent 12d75b10f3
commit 7a65bd8efb
Notes: sideshowbarker 2024-07-17 18:32:46 +09:00

View file

@ -51,6 +51,8 @@ template<FloatingPoint T>
constexpr T fmod(T x, T y) constexpr T fmod(T x, T y)
{ {
CONSTEXPR_STATE(fmod, x, y); CONSTEXPR_STATE(fmod, x, y);
#if ARCH(I386) || ARCH(X86_64)
u16 fpu_status; u16 fpu_status;
do { do {
asm( asm(
@ -60,11 +62,16 @@ constexpr T fmod(T x, T y)
: "u"(y)); : "u"(y));
} while (fpu_status & 0x400); } while (fpu_status & 0x400);
return x; return x;
#else
return __builtin_fmod(x, y);
#endif
} }
template<FloatingPoint T> template<FloatingPoint T>
constexpr T remainder(T x, T y) constexpr T remainder(T x, T y)
{ {
CONSTEXPR_STATE(remainder, x, y); CONSTEXPR_STATE(remainder, x, y);
#if ARCH(I386) || ARCH(X86_64)
u16 fpu_status; u16 fpu_status;
do { do {
asm( asm(
@ -74,6 +81,9 @@ constexpr T remainder(T x, T y)
: "u"(y)); : "u"(y));
} while (fpu_status & 0x400); } while (fpu_status & 0x400);
return x; return x;
#else
return __builtin_fmod(x, y);
#endif
} }
} }
@ -84,11 +94,16 @@ template<FloatingPoint T>
constexpr T sqrt(T x) constexpr T sqrt(T x)
{ {
CONSTEXPR_STATE(sqrt, x); CONSTEXPR_STATE(sqrt, x);
#if ARCH(I386) || ARCH(X86_64)
T res; T res;
asm("fsqrt" asm("fsqrt"
: "=t"(res) : "=t"(res)
: "0"(x)); : "0"(x));
return res; return res;
#else
return __builtin_sqrt(x);
#endif
} }
template<FloatingPoint T> template<FloatingPoint T>
@ -136,10 +151,14 @@ constexpr T fabs(T x)
{ {
if (is_constant_evaluated()) if (is_constant_evaluated())
return x < 0 ? -x : x; return x < 0 ? -x : x;
#if ARCH(I386) || ARCH(X86_64)
asm( asm(
"fabs" "fabs"
: "+t"(x)); : "+t"(x));
return x; return x;
#else
return __builtin_fabs(x);
#endif
} }
namespace Trigonometry { namespace Trigonometry {
@ -154,30 +173,42 @@ template<FloatingPoint T>
constexpr T sin(T angle) constexpr T sin(T angle)
{ {
CONSTEXPR_STATE(sin, angle); CONSTEXPR_STATE(sin, angle);
#if ARCH(I386) || ARCH(X86_64)
T ret; T ret;
asm( asm(
"fsin" "fsin"
: "=t"(ret) : "=t"(ret)
: "0"(angle)); : "0"(angle));
return ret; return ret;
#else
return __builtin_sin(angle);
#endif
} }
template<FloatingPoint T> template<FloatingPoint T>
constexpr T cos(T angle) constexpr T cos(T angle)
{ {
CONSTEXPR_STATE(cos, angle); CONSTEXPR_STATE(cos, angle);
#if ARCH(I386) || ARCH(X86_64)
T ret; T ret;
asm( asm(
"fcos" "fcos"
: "=t"(ret) : "=t"(ret)
: "0"(angle)); : "0"(angle));
return ret; return ret;
#else
return __builtin_cos(angle);
#endif
} }
template<FloatingPoint T> template<FloatingPoint T>
constexpr T tan(T angle) constexpr T tan(T angle)
{ {
CONSTEXPR_STATE(tan, angle); CONSTEXPR_STATE(tan, angle);
#if ARCH(I386) || ARCH(X86_64)
double ret, one; double ret, one;
asm( asm(
"fptan" "fptan"
@ -185,6 +216,9 @@ constexpr T tan(T angle)
: "0"(angle)); : "0"(angle));
return ret; return ret;
#else
return __builtin_tan(angle);
#endif
} }
template<FloatingPoint T> template<FloatingPoint T>
@ -192,6 +226,7 @@ constexpr T atan(T value)
{ {
CONSTEXPR_STATE(atan, value); CONSTEXPR_STATE(atan, value);
#if ARCH(I386) || ARCH(X86_64)
T ret; T ret;
asm( asm(
"fld1\n" "fld1\n"
@ -199,6 +234,9 @@ constexpr T atan(T value)
: "=t"(ret) : "=t"(ret)
: "0"(value)); : "0"(value));
return ret; return ret;
#else
return __builtin_atan(value);
#endif
} }
template<FloatingPoint T> template<FloatingPoint T>
@ -244,12 +282,16 @@ constexpr T atan2(T y, T x)
{ {
CONSTEXPR_STATE(atan2, y, x); CONSTEXPR_STATE(atan2, y, x);
#if ARCH(I386) || ARCH(X86_64)
T ret; T ret;
asm("fpatan" asm("fpatan"
: "=t"(ret) : "=t"(ret)
: "0"(x), "u"(y) : "0"(x), "u"(y)
: "st(1)"); : "st(1)");
return ret; return ret;
#else
return __builtin_atan2(y, x);
#endif
} }
} }
@ -270,6 +312,7 @@ constexpr T log(T x)
{ {
CONSTEXPR_STATE(log, x); CONSTEXPR_STATE(log, x);
#if ARCH(I386) || ARCH(X86_64)
T ret; T ret;
asm( asm(
"fldln2\n" "fldln2\n"
@ -278,6 +321,9 @@ constexpr T log(T x)
: "=t"(ret) : "=t"(ret)
: "0"(x)); : "0"(x));
return ret; return ret;
#else
return __builtin_log(x);
#endif
} }
template<FloatingPoint T> template<FloatingPoint T>
@ -285,6 +331,7 @@ constexpr T log2(T x)
{ {
CONSTEXPR_STATE(log2, x); CONSTEXPR_STATE(log2, x);
#if ARCH(I386) || ARCH(X86_64)
T ret; T ret;
asm( asm(
"fld1\n" "fld1\n"
@ -293,6 +340,9 @@ constexpr T log2(T x)
: "=t"(ret) : "=t"(ret)
: "0"(x)); : "0"(x));
return ret; return ret;
#else
return __builtin_log2(x);
#endif
} }
template<FloatingPoint T> template<FloatingPoint T>
@ -300,6 +350,7 @@ constexpr T log10(T x)
{ {
CONSTEXPR_STATE(log10, x); CONSTEXPR_STATE(log10, x);
#if ARCH(I386) || ARCH(X86_64)
T ret; T ret;
asm( asm(
"fldlg2\n" "fldlg2\n"
@ -308,6 +359,9 @@ constexpr T log10(T x)
: "=t"(ret) : "=t"(ret)
: "0"(x)); : "0"(x));
return ret; return ret;
#else
return __builtin_log10(x);
#endif
} }
template<FloatingPoint T> template<FloatingPoint T>
@ -315,6 +369,7 @@ constexpr T exp(T exponent)
{ {
CONSTEXPR_STATE(exp, exponent); CONSTEXPR_STATE(exp, exponent);
#if ARCH(I386) || ARCH(X86_64)
T res; T res;
asm("fldl2e\n" asm("fldl2e\n"
"fmulp\n" "fmulp\n"
@ -328,6 +383,9 @@ constexpr T exp(T exponent)
: "=t"(res) : "=t"(res)
: "0"(exponent)); : "0"(exponent));
return res; return res;
#else
return __builtin_exp(exponent);
#endif
} }
template<FloatingPoint T> template<FloatingPoint T>
@ -335,6 +393,7 @@ constexpr T exp2(T exponent)
{ {
CONSTEXPR_STATE(exp2, exponent); CONSTEXPR_STATE(exp2, exponent);
#if ARCH(I386) || ARCH(X86_64)
T res; T res;
asm("fld1\n" asm("fld1\n"
"fld %%st(1)\n" "fld %%st(1)\n"
@ -346,6 +405,9 @@ constexpr T exp2(T exponent)
: "=t"(res) : "=t"(res)
: "0"(exponent)); : "0"(exponent));
return res; return res;
#else
return __builtin_exp2(exponent);
#endif
} }
} }