Luna/libluna/include/luna/Common.h

60 lines
1.4 KiB
C
Raw Normal View History

/**
* @file Common.h
* @author apio (cloudapio.eu)
* @brief Common utility functions.
*
* @copyright Copyright (c) 2023, the Luna authors.
*
*/
#pragma once
/**
* @brief Divide a by b, rounding the quotient up to the next multiple of b.
*
* @tparam T The type of a and b.
* @param a The dividend.
* @param b The divisor.
* @return constexpr T The result of the operation.
*/
template <typename T> inline constexpr T ceil_div(T a, T b)
{
return (a + (b - 1)) / b;
}
2023-08-08 10:39:03 +00:00
/**
* @brief Return the minimum value out of a set of arguments.
*
* @tparam T The type of the arguments.
* @param a The first value of the set of arguments.
* @param args The rest of the set of arguments.
* @return constexpr T The minimum value.
*/
template <typename T, class... Args> inline constexpr T min(T a, Args... args)
{
if constexpr (sizeof...(args) == 0) return a;
else
{
T b = min(args...);
return a > b ? b : a;
}
}
/**
* @brief Return the maximum value out of a set of arguments.
*
* @tparam T The type of the arguments.
* @param a The first value of the set of arguments.
* @param args The rest of the set of arguments.
* @return constexpr T The maximum value.
*/
template <typename T, class... Args> inline constexpr T max(T a, Args... args)
{
if constexpr (sizeof...(args) == 0) return a;
else
{
T b = max(args...);
return a > b ? a : b;
}
}