version 0.4.1
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 {
13namespace Impl {
23 template <typename T>
24 constexpr T sqrt_helper(T x, T lo, T hi) {
25 if (lo == hi)
26 return lo;
27
28 const T mid = (lo + hi + 1) / 2;
29
30 if (x / mid < mid)
31 return sqrt_helper<T>(x, lo, mid - 1);
32 else
33 return sqrt_helper(x, mid, hi);
34 }
35
36} // namespace Impl
37
45template <typename T>
46requires std::integral<T>
47constexpr T ct_sqrt(T x) {
48 return Impl::sqrt_helper<T>(x, 0, x / 2 + 1);
49}
50} // namespace Ikarus
Definition: simpleassemblers.hh:22
constexpr T ct_sqrt(T x)
Compile-time square root for integer types.
Definition: math.hh:47