BLI: Ensure rcp and safe_rcp are not used with integral types

Since we expect the result of `rcp` and `safe_rcp` to always be in the
range of [0, 1], it doesn't make sense to allow passing in integer types
without casting them, as this has a high possibility of introducing
unwanted behavior by only returning either 0 or 1.

To prevent this, this commit adds a static assert on the type.

Pull Request: https://projects.blender.org/blender/blender/pulls/141567
This commit is contained in:
Sean Kim
2025-07-15 19:12:34 +02:00
committed by Sean Kim
parent 191a1a5ba7
commit e1d46c1570

View File

@@ -149,6 +149,7 @@ template<typename T> inline T sqrt(const T &a)
* If the input is zero the output is NaN. */
template<typename T> inline T rcp(const T &a)
{
static_assert(!std::is_integral_v<T>, "T must not be an integral type.");
return T(1) / a;
}
@@ -156,6 +157,7 @@ template<typename T> inline T rcp(const T &a)
* If the input is zero the output is zero. */
template<typename T> inline T safe_rcp(const T &a)
{
static_assert(!std::is_integral_v<T>, "T must be not be an integral type.");
return a ? T(1) / a : T(0);
}