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
// Copyright © 2018–2024 Trevor Spiteri

// This library is free software: you can redistribute it and/or
// modify it under the terms of either
//
//   * the Apache License, Version 2.0 or
//   * the MIT License
//
// at your option.
//
// You should have recieved copies of the Apache License and the MIT
// License along with the library. If not, see
// <https://www.apache.org/licenses/LICENSE-2.0> and
// <https://opensource.org/licenses/MIT>.

use crate::int256;

macro_rules! make_lerp {
    ($i:ident, $u:ident, $ii:ident, $uu:ident, $uu1:expr) => {
        pub const fn $i(r: $i, start: $i, end: $i, frac_bits: u32) -> ($i, bool) {
            // 0x00 ≤ r_abs ≤ 0x80
            let (r_abs, r_neg) = if r >= 0 {
                (r as $u, false)
            } else {
                (r.wrapping_neg() as $u, true)
            };
            // 0x00 ≤ range_abs ≤ 0xff
            let (range_abs, range_neg) = if end >= start {
                (end.wrapping_sub(start) as $u, false)
            } else {
                (start.wrapping_sub(end) as $u, true)
            };
            // 0x0000 ≤ wide_abs ≤ 0x7f80
            let wide_abs = (r_abs as $uu) * (range_abs as $uu);
            let wide_neg = r_neg != range_neg;
            let wide_uns = if wide_neg {
                // 0x0000 ≤ add ≤ 0x00ff
                let add = ($uu1 << frac_bits) - 1;
                // If frac_bits is 0: add = 0x0000; 0x0000 ≤ shifted ≤ 0x7f80
                // If frac_bits is max: add = 0x00ff; 0x0000 ≤ shifted ≤ 0x0080
                let shifted = (wide_abs + add) >> frac_bits;
                shifted.wrapping_neg()
            } else {
                wide_abs >> frac_bits
            };
            let wide = wide_uns as $ii;
            let (wide_ret, overflow1) = wide.overflowing_add(start as $ii);
            let ret = wide_ret as $i;
            let overflow2 = wide_ret != ret as $ii;
            (ret, overflow1 | overflow2)
        }

        pub const fn $u(r: $u, start: $u, end: $u, frac_bits: u32) -> ($u, bool) {
            // 0x00 ≤ range_abs ≤ 0xff
            let (range_abs, range_neg) = if end >= start {
                (end - start, false)
            } else {
                (start - end, true)
            };
            // 0x0000 ≤ wide_abs ≤  0xfe01
            let wide_abs = (r as $uu) * (range_abs as $uu);
            let (wide_ret, overflow1) = if range_neg {
                // 0x0000 ≤ add ≤ 0x00ff
                let add = ($uu1 << frac_bits) - 1;
                // If frac_bits is 0: add = 0x0000; 0x0000 ≤ shifted ≤ 0xfe01
                // If frac_bits is max: add = 0x00ff; 0x0000 ≤ shifted ≤ 0x00ff
                let shifted = (wide_abs + add) >> frac_bits;
                (start as $uu).overflowing_sub(shifted)
            } else {
                let shifted = wide_abs >> frac_bits;
                (start as $uu).overflowing_add(shifted)
            };
            let ret = wide_ret as $u;
            let overflow2 = wide_ret != ret as $uu;
            (ret, overflow1 | overflow2)
        }
    };
}

make_lerp! { i8, u8, i16, u16, 1u16 }
make_lerp! { i16, u16, i32, u32, 1u32 }
make_lerp! { i32, u32, i64, u64, 1u64 }
make_lerp! { i64, u64, i128, u128, 1u128 }

pub const fn i128(r: i128, start: i128, end: i128, frac_bits: u32) -> (i128, bool) {
    // 0x00 ≤ r_abs ≤ 0x80
    let (r_abs, r_neg) = if r >= 0 {
        (r as u128, false)
    } else {
        (r.wrapping_neg() as u128, true)
    };
    // 0x00 ≤ range_abs ≤ 0xff
    let (range_abs, range_neg) = if end >= start {
        (end.wrapping_sub(start) as u128, false)
    } else {
        (start.wrapping_sub(end) as u128, true)
    };
    // 0x0000 ≤ wide_abs ≤ 0x7f80
    let wide_abs = int256::wide_mul_u128(r_abs, range_abs);
    let wide_neg = r_neg != range_neg;
    let wide_uns = if wide_neg {
        // 0x0000 ≤ add ≤ 0x00ff
        let add = if frac_bits == 128 {
            u128::MAX
        } else {
            (1u128 << frac_bits) - 1
        };
        // If frac_bits is 0: add = 0x0000; 0x0000 ≤ shifted ≤ 0x7f80
        // If frac_bits is max: add = 0x00ff; 0x0000 ≤ shifted ≤ 0x0080
        let sum = int256::wrapping_add_u256_u128(wide_abs, add);
        let shifted = int256::shl_u256_max_128(sum, frac_bits);
        int256::wrapping_neg_u256(shifted)
    } else {
        int256::shl_u256_max_128(wide_abs, frac_bits)
    };
    let wide = int256::u256_wrapping_as_i256(wide_uns);
    let (wide_ret, overflow1) = int256::overflowing_add_i256_i128(wide, start);
    let ret = wide_ret.lo as i128;
    let overflow2 = if ret < 0 {
        wide_ret.hi != -1
    } else {
        wide_ret.hi != 0
    };
    (ret, overflow1 | overflow2)
}

pub const fn u128(r: u128, start: u128, end: u128, frac_bits: u32) -> (u128, bool) {
    // 0x00 ≤ range_abs ≤ 0xff
    let (range_abs, range_neg) = if end >= start {
        (end - start, false)
    } else {
        (start - end, true)
    };
    // 0x0000 ≤ wide_abs ≤  0xfe01
    let wide_abs = int256::wide_mul_u128(r, range_abs);
    if range_neg {
        // 0x0000 ≤ add ≤ 0x00ff
        let add = if frac_bits == 128 {
            u128::MAX
        } else {
            (1u128 << frac_bits) - 1
        };
        // If frac_bits is 0: add = 0x0000; 0x0000 ≤ shifted ≤ 0xfe01
        // If frac_bits is max: add = 0x00ff; 0x0000 ≤ shifted ≤ 0x00ff
        let sum = int256::wrapping_add_u256_u128(wide_abs, add);
        let shifted = int256::shl_u256_max_128(sum, frac_bits);
        int256::overflowing_sub_u128_u256(start, shifted)
    } else {
        let shifted = int256::shl_u256_max_128(wide_abs, frac_bits);
        int256::overflowing_add_u128_u256(start, shifted)
    }
}

#[cfg(test)]
mod tests {
    use crate::lerp;

    #[test]
    fn lerp_i8() {
        assert_eq!(lerp::i8(0, -128, 127, 0), (-128, false));
        assert_eq!(lerp::i8(0, 127, -128, 8), (127, false));
        assert_eq!(lerp::i8(1, -128, 127, 1), (-1, false));
        assert_eq!(lerp::i8(-64, -100, -110, 6), (-90, false));

        assert_eq!(lerp::i8(2 << 2, 0, 50, 2), (100, false));
        assert_eq!(lerp::i8(2 << 2, 0, 100, 2), (-56, true));
        assert_eq!(lerp::i8((-1) << 2, 50, 0, 2), (100, false));
        assert_eq!(lerp::i8((-1) << 2, 100, 0, 2), (-56, true));
    }

    #[test]
    fn lerp_u8() {
        assert_eq!(lerp::u8(0, 0, 255, 0), (0, false));
        assert_eq!(lerp::u8(0, 255, 0, 8), (255, false));
        assert_eq!(lerp::u8(1, 0, 255, 0), (255, false));
        assert_eq!(lerp::u8(128, 255, 0, 7), (0, false));

        assert_eq!(lerp::u8(2 << 2, 0, 100, 2), (200, false));
        assert_eq!(lerp::u8(2 << 2, 0, 200, 2), (200 - 56, true));
    }

    #[test]
    fn lerp_i128() {
        assert_eq!(lerp::i128(0, -128, 127, 0), (-128, false));
        assert_eq!(lerp::i128(0, 127, -128, 128), (127, false));
        assert_eq!(lerp::i128(1, -128, 127, 1), (-1, false));
        assert_eq!(lerp::i128(1 << 98, 127, -128, 100), (63, false));
        assert_eq!(lerp::i128((-64) << 120, -100, -110, 126), (-90, false));

        assert_eq!(
            lerp::i128(2 << 2, 0, i128::MAX / 2, 2),
            (i128::MAX / 2 * 2, false)
        );
        assert_eq!(
            lerp::i128(2 << 2, 0, i128::MAX, 2),
            (i128::MAX.wrapping_mul(2), true)
        );
        assert_eq!(
            lerp::i128((-1) << 2, i128::MAX / 2, 0, 2),
            (i128::MAX / 2 * 2, false)
        );
        assert_eq!(
            lerp::i128((-1) << 2, i128::MAX, 0, 2),
            (i128::MAX.wrapping_mul(2), true)
        );
    }

    #[test]
    fn lerp_u128() {
        assert_eq!(lerp::u128(0, 0, 255, 0), (0, false));
        assert_eq!(lerp::u128(0, 255, 0, 128), (255, false));
        assert_eq!(lerp::u128(1, 0, 255, 0), (255, false));
        assert_eq!(lerp::u128(128 << 120, 255, 0, 127), (0, false));

        assert_eq!(
            lerp::u128(2 << 2, 0, u128::MAX / 2, 2),
            (u128::MAX / 2 * 2, false)
        );
        assert_eq!(
            lerp::u128(2 << 2, 0, u128::MAX, 2),
            (u128::MAX.wrapping_mul(2), true)
        );
    }
}