pRC
multi-purpose Tensor Train library for C++
Loading...
Searching...
No Matches
isqrt.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_BASIC_FUNCTIONS_ISQRT_H
4#define pRC_CORE_BASIC_FUNCTIONS_ISQRT_H
5
8
9namespace pRC
10{
11 template<class T, If<IsUnsignedIntegral<T>> = 0>
12 static inline constexpr T iSqrt(T const a)
13 {
14 return RecursiveLambda(
15 [&a](auto const &self, T const low, T const high)
16 {
17 if(low == high)
18 {
19 return low;
20 }
21 else
22 {
23 auto const mid = (low + high + T(1)) / T(2);
24
25 if(a / mid < mid)
26 return self(low, mid - T(1));
27 else
28 return self(mid, high);
29 }
30 })(T(0), a / T(2) + T(1));
31 }
32}
33#endif // pRC_CORE_BASIC_FUNCTIONS_ISQRT_H
Definition recursive_lambda.hpp:12
Definition cholesky.hpp:18
static constexpr T iSqrt(T const a)
Definition isqrt.hpp:12
static constexpr Conditional< IsSatisfied< C >, RemoveConstReference< X >, X > copy(X &&a)
Definition copy.hpp:13