// 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" #include "asl/base/assert.hpp" namespace asl { template constexpr bool is_pow2(T x) { using unsigned_type = as_unsigned_integer; return x > 0 && has_single_bit(static_cast(x)); } template constexpr T round_down_pow2(T x, T div) { ASL_ASSERT(is_pow2(div)); return x & (-div); } template constexpr T round_up_pow2(T x, T div) { ASL_ASSERT(is_pow2(div)); return (x + (div - 1)) & (-div); } template concept is_numeric = is_integer || is_floating_point; template constexpr T min(T a, T b) { return (a <= b) ? a : b; } template constexpr T max(T a, T b) { return (a >= b) ? a : b; } template constexpr T clamp(T x, T a, T b) { return min(max(x, a), b); } } // namespace asl