|
@@ -125,6 +125,26 @@ constexpr bool is_constant_evaluated()
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
+// These can't be exported into the global namespace as they would clash with the C standard library.
|
|
|
+
|
|
|
+#define __DEFINE_GENERIC_ABS(type, zero, intrinsic) \
|
|
|
+ constexpr type abs(type num) \
|
|
|
+ { \
|
|
|
+ if (is_constant_evaluated()) \
|
|
|
+ return num < zero ? -num : num; \
|
|
|
+ else \
|
|
|
+ return __builtin_##intrinsic(num); \
|
|
|
+ }
|
|
|
+
|
|
|
+__DEFINE_GENERIC_ABS(int, 0, abs);
|
|
|
+__DEFINE_GENERIC_ABS(long, 0l, labs);
|
|
|
+__DEFINE_GENERIC_ABS(long long, 0ll, llabs);
|
|
|
+#ifndef KERNEL
|
|
|
+__DEFINE_GENERIC_ABS(float, 0.0f, fabsf);
|
|
|
+__DEFINE_GENERIC_ABS(double, 0.0, fabs);
|
|
|
+__DEFINE_GENERIC_ABS(long double, 0.0l, fabsl);
|
|
|
+#endif
|
|
|
+
|
|
|
}
|
|
|
|
|
|
using AK::array_size;
|