1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//! Floating-point to integer conversion helpers from `celt/float_cast.h`.
//!
//! The original header provides a family of macros that round floating-point
//! samples to integral types using the rounding behaviour guaranteed by C99's
//! `lrintf()`/`lrint()` functions. CELT relies on these helpers when bridging
//! between the float API and the fixed-point internals. The Rust port exposes
//! equivalent functions so that other translated modules can depend on the same
//! rounding semantics without reimplementing the details.
use rintf;
/// Scaling factor used by CELT to map floating-point samples to its internal
/// fixed-point representation.
pub const CELT_SIG_SCALE: f32 = 32_768.0;
/// Rounds a `f32` to the nearest `i32`, matching the behaviour of the
/// `float2int()` helper from the C implementation.
///
/// The reference code delegates to `lrintf()` when it is available, which
/// rounds to the nearest integer using the current floating-point rounding
/// mode (round-to-nearest-even in practice). Rust's `as` conversion from
/// `f32` to `i32` already saturates on overflow, so the implementation simply
/// applies `rintf()` before casting.
pub
/// Converts a floating-point sample to a signed 16-bit integer using CELT's
/// canonical scaling and rounding behaviour.
///
/// Mirrors the `FLOAT2INT16()` macro in `float_cast.h` by scaling the input,
/// clamping it to the representable range, and delegating to [`float2int`] for
/// the final rounding step.
pub