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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import_stdlib!();

use half::f16;
use anyhow::{bail, Result, Error};

use crate::{int::From64, CBORCase, CBORError, ExactFrom, Simple, CBOR};

use super::varint::{EncodeVarInt, MajorType};

static CBOR_NAN: [u8; 3] = [0xf9, 0x7e, 0x00];

impl From<f64> for CBOR {
    fn from(value: f64) -> Self {
        let n = value;
        if n < 0.0f64 {
            if let Some(n) = i128::exact_from_f64(n) {
                if let Some(i) = u64::exact_from_i128(-1 - n) {
                    return CBORCase::Negative(i).into();
                }
            }
        }
        if let Some(i) = u64::exact_from_f64(n) {
            return i.into();
        }
        CBORCase::Simple(Simple::Float(n)).into()
    }
}

pub fn f64_cbor_data(value: f64) -> Vec<u8> {
    let n = value;
    let f = n as f32;
    if f as f64 == n {
        return f32_cbor_data(f);
    }
    if n < 0.0f64 {
        if let Some(n) = i128::exact_from_f64(n) {
            if let Some(i) = u64::exact_from_i128(-1 - n) {
                let cbor: CBOR = CBORCase::Negative(i).into();
                return cbor.to_cbor_data();
            }
        }
    }
    if let Some(i) = u64::exact_from_f64(n) {
        return i.cbor_data();
    }
    if value.is_nan() {
        return CBOR_NAN.to_vec();
    }
    n.to_bits().encode_int(MajorType::Simple)
}

pub(crate) fn validate_canonical_f64(n: f64) -> Result<()> {
    if
        n == n as f32 as f64 ||
        n == n as i64 as f64 ||
        n.is_nan()
    {
        bail!(CBORError::NonCanonicalNumeric);
    }
    Ok(())
}

impl TryFrom<CBOR> for f64 {
    type Error = Error;
    
    fn try_from(cbor: CBOR) -> Result<Self> {
        match cbor.into_case() {
            CBORCase::Unsigned(n) => {
                if let Some(f) = f64::exact_from_u64(n) {
                    Ok(f)
                } else {
                    bail!(CBORError::OutOfRange);
                }
            },
            CBORCase::Negative(n) => {
                if let Some(f) = f64::exact_from_u64(n) {
                    Ok(-1f64 - f)
                } else {
                    bail!(CBORError::OutOfRange);
                }
            },
            CBORCase::Simple(Simple::Float(n)) => Ok(n),
            _ => bail!(CBORError::WrongType)
        }
    }
}

impl From<f32> for CBOR {
    fn from(value: f32) -> Self {
        let n = value;
        if n < 0.0f32 {
            if let Some(i) = u64::exact_from_f32(-1f32 - n) {
                return CBORCase::Negative(i).into();
            }
        }
        if let Some(i) = u32::exact_from_f32(n) {
            return i.into();
        }
        CBORCase::Simple(Simple::Float(n as f64)).into()
    }
}

pub fn f32_cbor_data(value: f32) -> Vec<u8> {
    let n = value;
    let f = f16::from_f32(n);
    if f.to_f32() == n {
        return f16_cbor_data(f);
    }
    if n < 0.0f32 {
        if let Some(i) = u64::exact_from_f32(-1f32 - n) {
            let cbor: CBOR = CBORCase::Negative(i).into();
            return cbor.to_cbor_data();
        }
    }
    if let Some(i) = u32::exact_from_f32(n) {
        return i.cbor_data();
    }
    if value.is_nan() {
        return CBOR_NAN.to_vec();
    }
    n.to_bits().encode_int(MajorType::Simple)
}

pub(crate) fn validate_canonical_f32(n: f32) -> Result<()> {
    if
        n == f16::from_f32(n).to_f32() ||
        n == n as i32 as f32 ||
        n.is_nan()
    {
        bail!(CBORError::NonCanonicalNumeric);
    }
    Ok(())
}

impl TryFrom<CBOR> for f32 {
    type Error = Error;

    fn try_from(cbor: CBOR) -> Result<Self> {
        match cbor.into_case() {
            CBORCase::Unsigned(n) => {
                if let Some(f) = f32::exact_from_u64(n) {
                    Ok(f)
                } else {
                    bail!(CBORError::OutOfRange);
                }
            },
            CBORCase::Negative(n) => {
                if let Some(f) = f32::exact_from_u64(n) {
                    Ok(f)
                } else {
                    bail!(CBORError::OutOfRange);
                }
            },
            CBORCase::Simple(Simple::Float(n)) => {
                if let Some(f) = f32::exact_from_f64(n) {
                    Ok(f)
                } else {
                    bail!(CBORError::OutOfRange);
                }
            },
            _ => bail!(CBORError::WrongType)
        }
    }
}

impl From<f16> for CBOR {
    fn from(value: f16) -> Self {
        let n = value.to_f64();
        if n < 0.0 {
            if let Some(i) = u64::exact_from_f64(-1f64 - n) {
                return CBORCase::Negative(i).into();
            }
        }
        if let Some(i) = u16::exact_from_f64(n) {
            return i.into();
        }
        CBORCase::Simple(Simple::Float(n)).into()
    }
}

pub fn f16_cbor_data(value: f16) -> Vec<u8> {
    let n = value.to_f64();
    if n < 0.0 {
        if let Some(i) = u64::exact_from_f64(-1f64 - n) {
            let cbor: CBOR = CBORCase::Negative(i).into();
            return cbor.to_cbor_data();
        }
    }
    if let Some(i) = u16::exact_from_f64(n) {
        return i.cbor_data();
    }
    if value.is_nan() {
        return CBOR_NAN.to_vec();
    }
    value.to_bits().encode_int(MajorType::Simple)
}

impl TryFrom<CBOR> for f16 {
    type Error = Error;

    fn try_from(cbor: CBOR) -> Result<Self> {
        match cbor.into_case() {
            CBORCase::Unsigned(n) => {
                if let Some(f) = f16::exact_from_u64(n) {
                    Ok(f)
                } else {
                    bail!(CBORError::OutOfRange);
                }
            },
            CBORCase::Negative(n) => {
                if let Some(f) = f64::exact_from_u64(n) {
                    if let Some(b) = f16::exact_from_f64(-1f64 - f) {
                        Ok(b)
                    } else {
                        bail!(CBORError::OutOfRange);
                    }
                } else {
                    bail!(CBORError::OutOfRange);
                }
            },
            CBORCase::Simple(Simple::Float(n)) => {
                if let Some(f) = f16::exact_from_f64(n) {
                    Ok(f)
                } else {
                    bail!(CBORError::OutOfRange);
                }
            },
            _ => bail!(CBORError::WrongType)
        }
    }
}

pub(crate) fn validate_canonical_f16(n: f16) -> Result<()> {
    let f = n.to_f64();
    if
        f == f as i64 as f64 ||
        n.is_nan() && n.to_bits() != 0x7e00
    {
        bail!(CBORError::NonCanonicalNumeric);
    }
    Ok(())
}