dep: use f64 for clock rate

This commit is contained in:
MaxOhn
2024-11-09 21:19:53 +01:00
parent 0448980cd5
commit 9edad8a3d5
4 changed files with 16 additions and 17 deletions
+2 -1
View File
@@ -19,7 +19,8 @@ tracing = ["rosu-map/tracing"]
[dependencies] [dependencies]
# rosu-map = { version = "0.1.2" } # rosu-map = { version = "0.1.2" }
rosu-map = { git = "https://github.com/MaxOhn/rosu-map", branch = "pp-update" } rosu-map = { git = "https://github.com/MaxOhn/rosu-map", branch = "pp-update" }
rosu-mods = { version = "0.1.0" } # rosu-mods = { version = "0.1.0" }
rosu-mods = { path = "../rosu-mods" }
[dev-dependencies] [dev-dependencies]
proptest = "1.4.0" proptest = "1.4.0"
+10 -12
View File
@@ -1,7 +1,7 @@
use std::{ use std::{
borrow::Cow, borrow::Cow,
fmt::{Debug, Formatter, Result as FmtResult}, fmt::{Debug, Formatter, Result as FmtResult},
num::NonZeroU32, num::NonZeroU64,
}; };
use rosu_map::section::general::GameMode; use rosu_map::section::general::GameMode;
@@ -51,12 +51,10 @@ pub struct Difficulty {
/// Clock rate will be clamped internally between 0.01 and 100.0. /// Clock rate will be clamped internally between 0.01 and 100.0.
/// ///
/// Since its minimum value is 0.01, its bits are never zero. /// Since its minimum value is 0.01, its bits are never zero.
/// Additionally, values between 0.01 and 100 are represented sufficiently
/// precise with 32 bits.
/// ///
/// This allows for an optimization to reduce the struct size by storing its /// This allows for an optimization to reduce the struct size by storing its
/// bits as a [`NonZeroU32`]. /// bits as a [`NonZeroU64`].
clock_rate: Option<NonZeroU32>, clock_rate: Option<NonZeroU64>,
ar: Option<ModsDependent>, ar: Option<ModsDependent>,
cs: Option<ModsDependent>, cs: Option<ModsDependent>,
hp: Option<ModsDependent>, hp: Option<ModsDependent>,
@@ -128,7 +126,7 @@ impl Difficulty {
InspectDifficulty { InspectDifficulty {
mods, mods,
passed_objects, passed_objects,
clock_rate: clock_rate.map(non_zero_u32_to_f32).map(f64::from), clock_rate: clock_rate.map(non_zero_u64_to_f64),
ar, ar,
cs, cs,
hp, hp,
@@ -171,11 +169,11 @@ impl Difficulty {
/// | :-----: | :-----: | /// | :-----: | :-----: |
/// | 0.01 | 100 | /// | 0.01 | 100 |
pub fn clock_rate(self, clock_rate: f64) -> Self { pub fn clock_rate(self, clock_rate: f64) -> Self {
let clock_rate = (clock_rate as f32).clamp(0.01, 100.0).to_bits(); let clock_rate = clock_rate.clamp(0.01, 100.0).to_bits();
// SAFETY: The minimum value is 0.01 so its bits can never be fully // SAFETY: The minimum value is 0.01 so its bits can never be fully
// zero. // zero.
let non_zero = unsafe { NonZeroU32::new_unchecked(clock_rate) }; let non_zero = unsafe { NonZeroU64::new_unchecked(clock_rate) };
Self { Self {
clock_rate: Some(non_zero), clock_rate: Some(non_zero),
@@ -334,7 +332,7 @@ impl Difficulty {
pub(crate) fn get_clock_rate(&self) -> f64 { pub(crate) fn get_clock_rate(&self) -> f64 {
let clock_rate = self let clock_rate = self
.clock_rate .clock_rate
.map_or(self.mods.clock_rate(), non_zero_u32_to_f32); .map_or(self.mods.clock_rate(), non_zero_u64_to_f64);
f64::from(clock_rate) f64::from(clock_rate)
} }
@@ -369,8 +367,8 @@ impl Difficulty {
} }
} }
fn non_zero_u32_to_f32(n: NonZeroU32) -> f32 { fn non_zero_u64_to_f64(n: NonZeroU64) -> f64 {
f32::from_bits(n.get()) f64::from_bits(n.get())
} }
impl Debug for Difficulty { impl Debug for Difficulty {
@@ -390,7 +388,7 @@ impl Debug for Difficulty {
f.debug_struct("Difficulty") f.debug_struct("Difficulty")
.field("mods", mods) .field("mods", mods)
.field("passed_objects", passed_objects) .field("passed_objects", passed_objects)
.field("clock_rate", &clock_rate.map(non_zero_u32_to_f32)) .field("clock_rate", &clock_rate.map(non_zero_u64_to_f64))
.field("ar", ar) .field("ar", ar)
.field("cs", cs) .field("cs", cs)
.field("hp", hp) .field("hp", hp)
+2 -2
View File
@@ -417,9 +417,9 @@ impl ModsDependentKind {
} }
} }
fn value(&self, mods: &GameMods, mods_fn: impl Fn(&GameMods) -> Option<f32>) -> f32 { fn value(&self, mods: &GameMods, mods_fn: impl Fn(&GameMods) -> Option<f64>) -> f32 {
match self { match self {
ModsDependentKind::Default(inner) => mods_fn(mods).unwrap_or(inner.value), ModsDependentKind::Default(inner) => mods_fn(mods).map_or(inner.value, |n| n as f32),
ModsDependentKind::Custom(inner) => inner.value, ModsDependentKind::Custom(inner) => inner.value,
} }
} }
+2 -2
View File
@@ -61,7 +61,7 @@ impl GameMods {
/// ///
/// In case of variable clock rates like for `WindUp`, this will return /// In case of variable clock rates like for `WindUp`, this will return
/// `1.0`. /// `1.0`.
pub(crate) fn clock_rate(&self) -> f32 { pub(crate) fn clock_rate(&self) -> f64 {
match self.inner { match self.inner {
GameModsInner::Lazer(ref mods) => mods.clock_rate().unwrap_or(1.0), GameModsInner::Lazer(ref mods) => mods.clock_rate().unwrap_or(1.0),
GameModsInner::Intermode(ref mods) => mods.legacy_clock_rate(), GameModsInner::Intermode(ref mods) => mods.legacy_clock_rate(),
@@ -105,7 +105,7 @@ macro_rules! impl_map_attr {
#[doc = "Check whether the mods specify a custom "] #[doc = "Check whether the mods specify a custom "]
#[doc = $s] #[doc = $s]
#[doc = "value."] #[doc = "value."]
pub(crate) fn $fn(&self) -> Option<f32> { pub(crate) fn $fn(&self) -> Option<f64> {
match self.inner { match self.inner {
GameModsInner::Lazer(ref mods) => mods.iter().find_map(|gamemod| match gamemod { GameModsInner::Lazer(ref mods) => mods.iter().find_map(|gamemod| match gamemod {
$( impl_map_attr!( @ $mode $field) => *$field, )* $( impl_map_attr!( @ $mode $field) => *$field, )*