summaryrefslogtreecommitdiff
path: root/asl/base/numeric.hpp
blob: bbd229ddf4f141fa492567a0992a32d07e93147f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright 2025 Steven Le Rouzic
//
// SPDX-License-Identifier: BSD-3-Clause

#pragma once

#include "asl/base/integers.hpp"
#include "asl/base/bit.hpp"
#include "asl/base/meta.hpp"

namespace asl
{

template<is_integer T>
constexpr bool is_pow2(T x)
{
    using unsigned_type = select_t<is_unsigned_integer<T>, T, as_unsigned_integer<T>>;
    return x > 0 && has_single_bit(static_cast<unsigned_type>(x));
}

template<typename T>
concept is_numeric = is_integer<T> || is_floating_point<T>;

template<is_numeric T>
constexpr T min(T a, T b)
{
    return (a <= b) ? a : b;
}

template<is_numeric T>
constexpr T max(T a, T b)
{
    return (a >= b) ? a : b;
}

template<is_numeric T>
constexpr T clamp(T x, T a, T b)
{
    return min(max(x, a), b);
}

} // namespace asl