Skip to content

Commit 5cd0d2b

Browse files
committed
AK: Always use serenity's math routines, even in lagom builds
Some of the functions in AK/Math.h used to implement math routines if AK_OS_SERENITY was defined, but called __builtin_foo() in lagom builds. This was not done consistently. Many functions always implemented math functions without this check, for example: - copysign() - acos(), asin() - cosh(), sinh(), tanh(), acosh, asinh(), atanh() - exp2(), log2(), pow() Move the functions that did that check to not doing it as well. This means these functions now use the same implementation in serenity and lagom. Since lagom is for testing serenity code outside serenity, this seems like a good change. All these functions check for x86 for and use inline assembly there, so this only affects non-x86 lagom builds. It affects these functions: - fmod() - remainder() (now always a TODO(), not just in serenity) - sin(), cos(), tan() - atan(), atan2() - log(), log10() It changes but does not really affect sqrt() -- now a TODO() on non-x86 non-arm non-riscv lagom (i.e. nowhere). As Serenity's implementations of these math functions are currently lower-quality than what's usually in lagom libms, this regresses lagom behavior (performance, precision) on non-x86 for these functions. But only to the level they're at within serenity, and we'll have to improve these functions there anyways.
1 parent bed715c commit 5cd0d2b

File tree

1 file changed

+2
-45
lines changed

1 file changed

+2
-45
lines changed

AK/Math.h

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,6 @@ constexpr T fmod(T x, T y)
439439
} while (fpu_status & 0x400);
440440
return x;
441441
#else
442-
# if defined(AK_OS_SERENITY)
443442
// FIXME: This is a very naive implementation.
444443

445444
if (__builtin_isnan(x))
@@ -526,14 +525,6 @@ constexpr T fmod(T x, T y)
526525
x_bits.exponent = x_exponent;
527526
x_bits.mantissa = x_mantissa;
528527
return x_bits.to_float();
529-
# else
530-
if constexpr (IsSame<T, long double>)
531-
return __builtin_fmodl(x, y);
532-
if constexpr (IsSame<T, double>)
533-
return __builtin_fmod(x, y);
534-
if constexpr (IsSame<T, float>)
535-
return __builtin_fmodf(x, y);
536-
# endif
537528
#endif
538529
}
539530

@@ -553,16 +544,8 @@ constexpr T remainder(T x, T y)
553544
} while (fpu_status & 0x400);
554545
return x;
555546
#else
556-
# if defined(AK_OS_SERENITY)
557547
// TODO: Add implementation for this function.
558548
TODO();
559-
# endif
560-
if constexpr (IsSame<T, long double>)
561-
return __builtin_remainderl(x, y);
562-
if constexpr (IsSame<T, double>)
563-
return __builtin_remainder(x, y);
564-
if constexpr (IsSame<T, float>)
565-
return __builtin_remainderf(x, y);
566549
#endif
567550
}
568551
}
@@ -615,11 +598,8 @@ constexpr T sqrt(T x)
615598
if constexpr (IsSame<T, long double>)
616599
TODO_RISCV64();
617600
#else
618-
# if defined(AK_OS_SERENITY)
619601
// TODO: Add implementation for this function.
620602
TODO();
621-
# endif
622-
return __builtin_sqrt(x);
623603
#endif
624604
}
625605

@@ -684,7 +664,6 @@ constexpr T sin(T angle)
684664
: "0"(angle));
685665
return ret;
686666
#else
687-
# if defined(AK_OS_SERENITY)
688667
T sign = 1;
689668
if (angle < 0) {
690669
angle = -angle;
@@ -726,9 +705,6 @@ constexpr T sin(T angle)
726705
};
727706
T angle_squared = angle * angle;
728707
return sign * (angle + angle * angle_squared * f(angle_squared));
729-
# else
730-
return __builtin_sin(angle);
731-
# endif
732708
#endif
733709
}
734710

@@ -745,7 +721,6 @@ constexpr T cos(T angle)
745721
: "0"(angle));
746722
return ret;
747723
#else
748-
# if defined(AK_OS_SERENITY)
749724
if (angle < 0)
750725
angle = -angle;
751726

@@ -789,9 +764,6 @@ constexpr T cos(T angle)
789764
};
790765
T angle_squared = angle * angle;
791766
return sign * (1 + angle_squared * f(angle_squared));
792-
# else
793-
return __builtin_cos(angle);
794-
# endif
795767
#endif
796768
}
797769

@@ -828,11 +800,7 @@ constexpr T tan(T angle)
828800

829801
return ret;
830802
#else
831-
# if defined(AK_OS_SERENITY)
832803
return sin(angle) / cos(angle);
833-
# else
834-
return __builtin_tan(angle);
835-
# endif
836804
#endif
837805
}
838806

@@ -898,10 +866,7 @@ constexpr T atan(T value)
898866
: "0"(value));
899867
return ret;
900868
#else
901-
# if defined(AK_OS_SERENITY)
902869
return asin(value / sqrt(1 + value * value));
903-
# endif
904-
return __builtin_atan(value);
905870
#endif
906871
}
907872

@@ -918,7 +883,6 @@ constexpr T atan2(T y, T x)
918883
: "st(1)");
919884
return ret;
920885
#else
921-
# if defined(AK_OS_SERENITY)
922886
if (__builtin_isnan(y))
923887
return y;
924888
if (__builtin_isnan(x))
@@ -978,9 +942,6 @@ constexpr T atan2(T y, T x)
978942
return atan(y / x) - Pi<T>;
979943
// y < 0 && x > 0
980944
return atan(y / x);
981-
# else
982-
return __builtin_atan2(y, x);
983-
# endif
984945
#endif
985946
}
986947

@@ -1094,11 +1055,9 @@ constexpr T log(T x)
10941055
: "=t"(ret)
10951056
: "0"(x));
10961057
return ret;
1097-
#elif defined(AK_OS_SERENITY)
1058+
#else
10981059
// FIXME: Adjust the polynomial and formula in log2 to fit this
10991060
return log2<T>(x) / L2_E<T>;
1100-
#else
1101-
return __builtin_log(x);
11021061
#endif
11031062
}
11041063

@@ -1116,11 +1075,9 @@ constexpr T log10(T x)
11161075
: "=t"(ret)
11171076
: "0"(x));
11181077
return ret;
1119-
#elif defined(AK_OS_SERENITY)
1078+
#else
11201079
// FIXME: Adjust the polynomial and formula in log2 to fit this
11211080
return log2<T>(x) / L2_10<T>;
1122-
#else
1123-
return __builtin_log10(x);
11241081
#endif
11251082
}
11261083

0 commit comments

Comments
 (0)