explain the remaining float conversions at their use sites

This commit is contained in:
2026-09-21 01:23:45 +03:00
parent d297ec4271
commit 4d3d889f27
3 changed files with 29 additions and 4 deletions
+8 -1
View File
@@ -326,7 +326,14 @@ fn write_next_packet(
let i32_slice = let i32_slice =
unsafe { std::slice::from_raw_parts(data_ptr as *const i32, samples) }; unsafe { std::slice::from_raw_parts(data_ptr as *const i32, samples) };
for &sample in i32_slice { for &sample in i32_slice {
let f = sample as f32 / 2147483648.0; // PCM 32-bit samples are normalised by 2^31 into -1.0..1.0,
// where f32 has more than enough precision; the cast is the
// standard full-scale conversion, not an approximation.
#[allow(
clippy::cast_precision_loss,
reason = "full-scale i32 PCM divided by 2^31 lands in -1.0..1.0"
)]
let f = sample as f32 / 2_147_483_648.0;
writer writer
.write_all(&f.to_le_bytes()) .write_all(&f.to_le_bytes())
.context("writing f32 sample")?; .context("writing f32 sample")?;
+12
View File
@@ -345,6 +345,12 @@ impl TonemapPipeline {
let vb = [Some(self.vertex_buffer.clone())]; let vb = [Some(self.vertex_buffer.clone())];
let strides = [16u32]; let strides = [16u32];
let offsets = [0u32]; let offsets = [0u32];
// Capture dimensions are at most 4K (well under f32's 2^24 exact-integer
// limit), so the cast is lossless for every value this can hold.
#[allow(
clippy::cast_precision_loss,
reason = "capture width/height are bounded by MAX_ENCODE_WIDTH (3840), exactly representable in f32"
)]
let viewport = [D3D11_VIEWPORT { let viewport = [D3D11_VIEWPORT {
TopLeftX: 0.0, TopLeftX: 0.0,
TopLeftY: 0.0, TopLeftY: 0.0,
@@ -858,6 +864,12 @@ fn capture_timing(frame_count: u64, elapsed_secs: f64, pipe_fps: u32) -> Option<
if frame_count == 0 || elapsed_secs <= 0.0 { if frame_count == 0 || elapsed_secs <= 0.0 {
return None; return None;
} }
// A frame count high enough to lose f64 precision would be ~2^53 frames,
// which is centuries of capture; f64 handles this ratio exactly enough.
#[allow(
clippy::cast_precision_loss,
reason = "frame counts that could lose f64 precision are unreachable in a recording session"
)]
let actual_fps = frame_count as f64 / elapsed_secs; let actual_fps = frame_count as f64 / elapsed_secs;
Some((actual_fps, f64::from(pipe_fps) / actual_fps)) Some((actual_fps, f64::from(pipe_fps) / actual_fps))
} }
+9 -3
View File
@@ -351,9 +351,15 @@ pub(crate) fn codec_availability(ffmpeg: &Path) -> CodecAvailability {
} }
fn video_bitrate_kbps(req: &TranscodeRequest) -> Result<u64> { fn video_bitrate_kbps(req: &TranscodeRequest) -> Result<u64> {
// Byte and bitrate budgets are far below 2^53, so f64 represents them
// exactly; these casts are the only way to get u64 into the f64 maths.
#[allow(
clippy::cast_precision_loss,
reason = "target sizes and bitrates are many orders of magnitude below f64's exact-integer range"
)]
let target_bits = (req.target_bytes as f64) * 8.0 * 0.95; let target_bits = (req.target_bytes as f64) * 8.0 * 0.95;
let audio_bits = if req.keep_audio { let audio_bits = if req.keep_audio {
AUDIO_BITRATE_KBPS as f64 * 1000.0 * req.duration_secs (AUDIO_BITRATE_KBPS as f64) * 1000.0 * req.duration_secs
} else { } else {
0.0 0.0
}; };
@@ -1417,7 +1423,7 @@ mod tests {
#[test] #[test]
fn click_filters_capped_at_max() { fn click_filters_capped_at_max() {
let clicks: Vec<Click> = (0..100) let clicks: Vec<Click> = (0..100)
.map(|i| Click { time_secs: i as f64, x: i, y: i, button: crate::clicks::MouseButton::Left }) .map(|i| Click { time_secs: f64::from(i), x: i, y: i, button: crate::clicks::MouseButton::Left })
.collect(); .collect();
let filter = click_filters(&clicks, 1920, 1080).unwrap(); let filter = click_filters(&clicks, 1920, 1080).unwrap();
let count = filter.matches("drawbox").count(); let count = filter.matches("drawbox").count();
@@ -1484,7 +1490,7 @@ mod tests {
#[test] #[test]
fn gif_color_factor_matches_comment_values() { fn gif_color_factor_matches_comment_values() {
// comment claims: 256=1.0x, 128=~0.71x, 64=0.5x, 32=~0.35x, 16=0.25x // comment claims: 256=1.0x, 128=~0.71x, 64=0.5x, 32=~0.35x, 16=0.25x
let cf = |colors: u16| (colors as f64 / 256.0).sqrt().clamp(0.15, 1.0); let cf = |colors: u16| (f64::from(colors) / 256.0).sqrt().clamp(0.15, 1.0);
assert!((cf(256) - 1.0).abs() < 1e-9); assert!((cf(256) - 1.0).abs() < 1e-9);
assert!((cf(128) - 2f64.sqrt() / 2.0).abs() < 1e-9); // ~0.707 assert!((cf(128) - 2f64.sqrt() / 2.0).abs() < 1e-9); // ~0.707
assert!((cf(64) - 0.5).abs() < 1e-9); assert!((cf(64) - 0.5).abs() < 1e-9);