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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::fmt;

/// A 24.8 fixed point number.
///
/// Internally this is stored as a single `i32` value, with methods to convert
/// to/from `f32` floating point values.
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct Fixed(i32);

impl Fixed {
    /// Creates a `Fixed` from raw bytes.
    pub fn from_bits(v: i32) -> Self {
        Fixed(v)
    }

    /// Creates a `Fixed` from a floating point value.
    pub fn from_float(v: f32) -> Self {
        Fixed((v * 256.0) as i32)
    }

    /// Returns a floating point representation of this value.
    pub fn to_float(self) -> f32 {
        (self.0 as f32) / 256.0
    }

    /// Returns the underlying integer representation of this value.
    pub fn bits(self) -> i32 {
        self.0
    }
}

impl fmt::Display for Fixed {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(fmt, "{}", self.to_float())
    }
}

impl fmt::Debug for Fixed {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(fmt, "{:?}", self.to_float())
    }
}

impl From<f32> for Fixed {
    fn from(v: f32) -> Self {
        Self::from_float(v)
    }
}

impl From<i32> for Fixed {
    fn from(v: i32) -> Self {
        Self::from_bits(v)
    }
}

impl Into<f32> for Fixed {
    fn into(self) -> f32 {
        self.to_float()
    }
}

#[cfg(test)]
mod tests {
    use zerocopy::transmute;

    use super::*;

    #[test]
    fn fixed_to_float() {
        let fixed: Fixed = 256.into();
        assert_eq!(1.0, fixed.to_float());

        let fixed: Fixed = 257.into();
        assert_eq!(1.00390625, fixed.to_float());

        let i: i32 = transmute!(0xffffff00u32);
        let fixed: Fixed = i.into();
        assert_eq!(-1.0, fixed.to_float());

        let i: i32 = transmute!(0xfffffeffu32);
        let fixed: Fixed = i.into();
        assert_eq!(-1.00390625, fixed.to_float());
    }

    #[test]
    fn float_to_fixed() {
        let fixed: Fixed = 1.0.into();
        assert_eq!(256, fixed.bits());

        let fixed: Fixed = 1.00390625.into();
        assert_eq!(257, fixed.bits());

        let fixed: Fixed = (-1.0).into();
        let i: i32 = transmute!(0xffffff00u32);
        assert_eq!(i, fixed.bits());

        let fixed: Fixed = (-1.00390625).into();
        let i: i32 = transmute!(0xfffffeffu32);
        assert_eq!(i, fixed.bits());
    }
}