2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
// author: Max Kellermann <max.kellermann@gmail.com>
|
2013-12-22 21:08:06 +01:00
|
|
|
|
|
|
|
#ifndef CLAMP_HPP
|
|
|
|
#define CLAMP_HPP
|
|
|
|
|
|
|
|
#include "Compiler.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clamps the specified value in a range. Returns #min or #max if the
|
|
|
|
* value is outside.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
2019-03-12 11:54:51 +01:00
|
|
|
constexpr const T &
|
2013-12-22 21:08:06 +01:00
|
|
|
Clamp(const T &value, const T &min, const T &max)
|
|
|
|
{
|
|
|
|
return gcc_unlikely(value < min)
|
|
|
|
? min
|
|
|
|
: (gcc_unlikely(value > max)
|
|
|
|
? max : value);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|