AK: Add the wrap_to_range() function

This commit is contained in:
Lucas CHOLLET authored and Nico Weber committed 2025-10-14 08:01:34 -04:00
1 parent d9d961439a
commit 5dbea4283f
2 files changed
+16

No files matched your search

+7
View File
@@ -1284,6 +1284,13 @@ constexpr I clamp_to(T value)
return value;
}
// Wrap a to keep it in the range [-b, b].
template<typename T>
constexpr T wrap_to_range(T a, IdentityType<T> b)
{
return fmod(fmod(a + b, 2 * b) + 2 * b, 2 * b) - b;
}
#undef CONSTEXPR_STATE
#undef AARCH64_INSTRUCTION
}
+9
View File
@@ -36,3 +36,12 @@ TEST_CASE(pow)
EXPECT_EQ(AK::pow(1., -9223372036854775808.0), 1.);
EXPECT_EQ(AK::pow(1., -9223372036854777856.0), 1.);
}
TEST_CASE(wrap_to_range)
{
EXPECT_EQ(AK::wrap_to_range(-185., 180.), 175.);
EXPECT_EQ(AK::wrap_to_range(185., 180.), -175.);
EXPECT_EQ(AK::wrap_to_range(85., 180.), 85.);
EXPECT_EQ(AK::wrap_to_range(0., 180.), 0.);
EXPECT_EQ(AK::wrap_to_range(-85., 180.), -85.);
}