ffav_sys/avutil/
mathematics.rs1use crate::AVRounding;
2
3impl From<AVRounding> for u32 {
4 fn from(v: AVRounding) -> u32 {
5 unsafe { std::mem::transmute::<AVRounding, u32>(v) }
6 }
7}
8
9impl Default for AVRounding {
10 fn default() -> Self {
11 AVRounding::new()
12 }
13}
14
15impl AVRounding {
16 #[inline]
18 pub fn new() -> Self {
19 AVRounding::AV_ROUND_ZERO
20 }
21
22 #[inline]
24 pub fn zero(self) -> Self {
25 unsafe { std::mem::transmute::<u32, AVRounding>(0) }
26 }
27
28 #[inline]
30 pub fn inf(self) -> Self {
31 unsafe { std::mem::transmute::<u32, AVRounding>(self as u32 | 1) }
32 }
33
34 #[inline]
36 pub fn down(self) -> Self {
37 unsafe { std::mem::transmute::<u32, AVRounding>(self as u32 | 2) }
38 }
39
40 #[inline]
42 pub fn up(self) -> Self {
43 unsafe { std::mem::transmute::<u32, AVRounding>(self as u32 | 3) }
44 }
45
46 #[inline]
48 pub fn near_inf(self) -> Self {
49 unsafe { std::mem::transmute::<u32, AVRounding>(self as u32 | 5) }
50 }
51
52 #[inline]
56 pub fn pass_min_max(self) -> Self {
57 unsafe { std::mem::transmute::<u32, AVRounding>(self as u32 | 8192) }
58 }
59}
60
61#[cfg(test)]
62mod tests {
63 use super::*;
64
65 #[test]
66 fn test_avrounding() {
67 assert_eq!(
68 std::mem::size_of::<AVRounding>(),
69 std::mem::size_of::<u32>()
70 );
71 assert_eq!(AVRounding::new(), AVRounding::AV_ROUND_ZERO);
72 assert_eq!(AVRounding::new().zero(), AVRounding::AV_ROUND_ZERO);
73 assert_eq!(AVRounding::new().inf(), AVRounding::AV_ROUND_INF);
74 assert_eq!(AVRounding::new().down(), AVRounding::AV_ROUND_DOWN);
75 assert_eq!(AVRounding::new().up(), AVRounding::AV_ROUND_UP);
76 assert_eq!(AVRounding::new().near_inf(), AVRounding::AV_ROUND_NEAR_INF);
77 assert_eq!(
78 AVRounding::new().pass_min_max(),
79 AVRounding::AV_ROUND_PASS_MINMAX
80 );
81 assert_eq!(AVRounding::new().near_inf().pass_min_max() as u32, 8197);
82 }
83}