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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use serde::{Serialize, Serializer, Deserialize, Deserializer};
use serde::de::Visitor;
use std::fmt;

macro_rules! signed_fixed_point_impl {
  ($name:ident, $epsilon_type:ty, $int_bits:expr, $frac_bits:expr) => {

    #[derive(PartialEq, Eq)]
    pub struct $name {
      epsilon: $epsilon_type,
    }

    impl $name {
      pub fn from_epsilons(epsilon: $epsilon_type) -> $name {
        $name {epsilon: epsilon}
      }
    }

    impl fmt::Debug for $name {
      fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
          f,
          "{}0x{:0int_width$x}.{:0frac_width$x}",
          if self.epsilon < 0 {"-"} else {"+"},
          self.epsilon >> $frac_bits,
          self.epsilon & ((1 << $frac_bits) - 1),
          int_width = $int_bits / 4,
          frac_width = $frac_bits / 4,
        )
      }
    }

    impl Serialize for $name {
      fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
        serializer.serialize_str(&format!("{:?}", self))
      }
    }

    impl<'a> Deserialize<'a> for $name {
      fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'a> {
        use serde::de;
        use regex::Regex;

        struct FixedPointVisitor;

        impl<'b> Visitor<'b> for FixedPointVisitor {
          type Value = $name;

          fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("a string following the pattern ^[+-]0x[0-9a-f]+\\.[0-9a-f]+$")
          }

          fn visit_str<E>(self, value: &str) -> Result<$name, E> where E: de::Error {
            lazy_static! {
              // TODO(demurgos): The number of digits should be based on the number of bits
              static ref FIXED_POINT_RE: Regex = Regex::new("^([+-])0x([0-9a-f]{2,4})\\.([0-9a-f]{2,4})$").unwrap();
            }
            match FIXED_POINT_RE.captures(value) {
              Some(caps) => {
                let sign: $epsilon_type = if &caps[1] == "-" {-1} else {1};
                let int_part: $epsilon_type = i64::from_str_radix(&caps[2], 16).unwrap() as $epsilon_type;
                let frac_part: $epsilon_type = i64::from_str_radix(&caps[3], 16).unwrap() as $epsilon_type;
                Ok($name::from_epsilons((sign * int_part << $frac_bits) + sign * frac_part))
              },
              None => Err(E::custom(format!("Invalid fixed point: {}", value))),
            }
          }
        }

        deserializer.deserialize_str(FixedPointVisitor)
      }
    }
  }
}

macro_rules! unsigned_fixed_point_impl {
  ($name:ident, $epsilon_type:ty, $int_bits:expr, $frac_bits:expr) => {

    #[derive(PartialEq, Eq)]
    pub struct $name {
      epsilon: $epsilon_type,
    }

    impl $name {
      pub fn from_epsilons(epsilon: $epsilon_type) -> $name {
        $name {epsilon: epsilon}
      }
    }

    impl fmt::Debug for $name {
      fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
          f,
          "+0x{:0int_width$x}.{:0frac_width$x}",
          self.epsilon >> $frac_bits,
          self.epsilon & ((1 << $frac_bits) - 1),
          int_width = $int_bits / 4,
          frac_width = $frac_bits / 4,
        )
      }
    }

    impl Serialize for $name {
      fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
        serializer.serialize_str(&format!("{:?}", self))
      }
    }

    impl<'a> Deserialize<'a> for $name {
      fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'a> {
        use serde::de;
        use regex::Regex;

        struct FixedPointVisitor;

        impl<'b> Visitor<'b> for FixedPointVisitor {
          type Value = $name;

          fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("a string following the pattern ^[+-]0x[0-9a-f]+\\.[0-9a-f]+$")
          }

          fn visit_str<E>(self, value: &str) -> Result<$name, E> where E: de::Error {
            lazy_static! {
              static ref FIXED_POINT_RE: Regex = Regex::new("^+0x([0-9a-f]{2,4})\\.([0-9a-f]{2,4})$").unwrap();
            }
            match FIXED_POINT_RE.captures(value) {
              Some(caps) => {
                let int_part: $epsilon_type = u64::from_str_radix(&caps[1], 16).unwrap() as $epsilon_type;
                let frac_part: $epsilon_type = u64::from_str_radix(&caps[2], 16).unwrap() as $epsilon_type;
                Ok($name::from_epsilons((int_part << $frac_bits) + frac_part))
              },
              None => Err(E::custom(format!("Invalid fixed point: {}", value))),
            }
          }
        }

        deserializer.deserialize_str(FixedPointVisitor)
      }
    }
  }
}

signed_fixed_point_impl!(Fixed8P8, i16, 8, 8);
signed_fixed_point_impl!(Fixed16P16, i32, 16, 16);
unsigned_fixed_point_impl!(Ufixed8P8, u16, 8, 8);
unsigned_fixed_point_impl!(Ufixed16P16, u32, 16, 16);

#[cfg(test)]
mod tests {
  use super::Fixed16P16;
  use serde_json;
  use std::fmt::Write;

  #[test]
  fn test_eq() {
    assert_eq!(Fixed16P16::from_epsilons(3), Fixed16P16::from_epsilons(3));
  }

  #[test]
  fn test_json_serde_serialization() {
    assert_eq!(serde_json::to_string(&Fixed16P16::from_epsilons(3)).unwrap(), "\"+0x0000.0003\"");
  }

  #[test]
  fn test_json_serde_deserialization() {
    assert_eq!(serde_json::from_str::<Fixed16P16>("\"+0x0000.0000\"").unwrap(), Fixed16P16::from_epsilons(0));
    assert_eq!(serde_json::from_str::<Fixed16P16>("\"+0x0000.0003\"").unwrap(), Fixed16P16::from_epsilons(3));
    assert_eq!(serde_json::from_str::<Fixed16P16>("\"+0x0001.0000\"").unwrap(), Fixed16P16::from_epsilons(65536));
    assert_eq!(serde_json::from_str::<Fixed16P16>("\"+0x7fff.ffff\"").unwrap(), Fixed16P16::from_epsilons(2147483647));
    assert_eq!(serde_json::from_str::<Fixed16P16>("\"-0x8000.0000\"").unwrap(), Fixed16P16::from_epsilons(-2147483648));
  }
}