Skip to content

Commit 0041ff7

Browse files
committed
AK: Extract a CALL_BUILTIN macro for Math.h
No behavior change.
1 parent c224a9b commit 0041ff7

File tree

1 file changed

+16
-36
lines changed

1 file changed

+16
-36
lines changed

AK/Math.h

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,19 @@ FloatT copysign(FloatT x, FloatT y)
7676
return ex.to_float();
7777
}
7878

79-
#define CONSTEXPR_STATE(function, args...) \
80-
if (is_constant_evaluated()) { \
81-
if (IsSame<T, long double>) \
79+
#define CALL_BUILTIN(function, args...) \
80+
do { \
81+
if constexpr (IsSame<T, long double>) \
8282
return __builtin_##function##l(args); \
83-
if (IsSame<T, double>) \
83+
if constexpr (IsSame<T, double>) \
8484
return __builtin_##function(args); \
85-
if (IsSame<T, float>) \
85+
if constexpr (IsSame<T, float>) \
8686
return __builtin_##function##f(args); \
87-
}
87+
} while (0)
88+
89+
#define CONSTEXPR_STATE(function, args...) \
90+
if (is_constant_evaluated()) \
91+
CALL_BUILTIN(function, args);
8892

8993
#define AARCH64_INSTRUCTION(instruction, arg) \
9094
if constexpr (IsSame<T, long double>) \
@@ -108,12 +112,7 @@ template<FloatingPoint T>
108112
constexpr T fabs(T x)
109113
{
110114
// Both GCC and Clang inline fabs by default, so this is just a cmath like wrapper
111-
if constexpr (IsSame<T, long double>)
112-
return __builtin_fabsl(x);
113-
if constexpr (IsSame<T, double>)
114-
return __builtin_fabs(x);
115-
if constexpr (IsSame<T, float>)
116-
return __builtin_fabsf(x);
115+
CALL_BUILTIN(fabs, x);
117116
}
118117

119118
namespace Rounding {
@@ -131,12 +130,7 @@ constexpr T ceil(T num)
131130
#if ARCH(AARCH64)
132131
AARCH64_INSTRUCTION(frintp, num);
133132
#else
134-
if constexpr (IsSame<T, long double>)
135-
return __builtin_ceill(num);
136-
if constexpr (IsSame<T, double>)
137-
return __builtin_ceil(num);
138-
if constexpr (IsSame<T, float>)
139-
return __builtin_ceilf(num);
133+
CALL_BUILTIN(ceil, num);
140134
#endif
141135
}
142136

@@ -154,12 +148,7 @@ constexpr T floor(T num)
154148
#if ARCH(AARCH64)
155149
AARCH64_INSTRUCTION(frintm, num);
156150
#else
157-
if constexpr (IsSame<T, long double>)
158-
return __builtin_floorl(num);
159-
if constexpr (IsSame<T, double>)
160-
return __builtin_floor(num);
161-
if constexpr (IsSame<T, float>)
162-
return __builtin_floorf(num);
151+
CALL_BUILTIN(floor, num);
163152
#endif
164153
}
165154

@@ -527,12 +516,7 @@ constexpr T fmod(T x, T y)
527516
x_bits.mantissa = x_mantissa;
528517
return x_bits.to_float();
529518
# 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);
519+
CALL_BUILTIN(fmod, x, y);
536520
# endif
537521
#endif
538522
}
@@ -557,12 +541,7 @@ constexpr T remainder(T x, T y)
557541
// TODO: Add implementation for this function.
558542
TODO();
559543
# 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);
544+
CALL_BUILTIN(remainder, x, y);
566545
#endif
567546
}
568547
}
@@ -1393,6 +1372,7 @@ constexpr T wrap_to_range(T a, IdentityType<T> b)
13931372
return fmod(fmod(a + b, 2 * b) + 2 * b, 2 * b) - b;
13941373
}
13951374

1375+
#undef CALL_BUILTIN
13961376
#undef CONSTEXPR_STATE
13971377
#undef AARCH64_INSTRUCTION
13981378
}

0 commit comments

Comments
 (0)