| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #ifndef LIBCPP_BITS_BUILTINS
|
|---|
| 8 | #define LIBCPP_BITS_BUILTINS
|
|---|
| 9 |
|
|---|
| 10 | /**
|
|---|
| 11 | * Note: While the primary target of libcpp is the
|
|---|
| 12 | * g++ compiler, using builtin functions that would
|
|---|
| 13 | * unnecessarily limit the library to any specific
|
|---|
| 14 | * compiler is discouraged and as such all the used
|
|---|
| 15 | * builtins should be available atleast in g++ and
|
|---|
| 16 | * clang++.
|
|---|
| 17 | * GCC: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
|
|---|
| 18 | * LLVM: https://github.com/llvm-mirror/clang/blob/master/include/clang/Basic/Builtins.def
|
|---|
| 19 | * (If anyone has a better link for LLVM, feel free to update it.)
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | #include <cstdlib>
|
|---|
| 23 |
|
|---|
| 24 | namespace std::aux
|
|---|
| 25 | {
|
|---|
| 26 | template<class T>
|
|---|
| 27 | constexpr double log2(T val)
|
|---|
| 28 | {
|
|---|
| 29 | return __builtin_log2(static_cast<double>(val));
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | template<class T>
|
|---|
| 33 | constexpr double pow2(T exp)
|
|---|
| 34 | {
|
|---|
| 35 | return __builtin_pow(2.0, static_cast<double>(exp));
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | template<class T>
|
|---|
| 39 | constexpr size_t pow2u(T exp)
|
|---|
| 40 | {
|
|---|
| 41 | return static_cast<size_t>(pow2(exp));
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | template<class T, class U>
|
|---|
| 45 | constexpr T pow(T base, U exp)
|
|---|
| 46 | {
|
|---|
| 47 | return static_cast<T>(
|
|---|
| 48 | __builtin_pow(static_cast<double>(base), static_cast<double>(exp))
|
|---|
| 49 | );
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | template<class T>
|
|---|
| 53 | constexpr size_t ceil(T val)
|
|---|
| 54 | {
|
|---|
| 55 | return static_cast<size_t>(__builtin_ceil(static_cast<double>(val)));
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | template<class T>
|
|---|
| 59 | constexpr size_t floor(T val)
|
|---|
| 60 | {
|
|---|
| 61 | return static_cast<size_t>(__builtin_floor(static_cast<double>(val)));
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | #endif
|
|---|