version 0.4
math.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2024 The Ikarus Developers mueller@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
9#pragma once
10#include <concepts>
11
12namespace Ikarus {
13 namespace Impl {
23 template <typename T>
24 constexpr T sqrt_helper(T x, T lo, T hi) {
25 if (lo == hi) return lo;
26
27 const T mid = (lo + hi + 1) / 2;
28
29 if (x / mid < mid)
30 return sqrt_helper<T>(x, lo, mid - 1);
31 else
32 return sqrt_helper(x, mid, hi);
33 }
34
35 } // namespace Impl
36
44 template <typename T>
45 requires std::integral<T>
46 constexpr T ct_sqrt(T x) { return Impl::sqrt_helper<T>(x, 0, x / 2 + 1); }
47} // namespace Ikarus
Definition: simpleassemblers.hh:21
constexpr T ct_sqrt(T x)
Compile-time square root for integer types.
Definition: math.hh:46