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
//! Normalize a CSS z-index to an f32 floating-point number between 0.0 and 1.0. (not quite...)
//!
//! Theoretically, this is not entirely feasible because we can generate only
//! `2^23 (subnormal) * 127 (normal) = 1065353216` distinct floating-point numbers in this range.
//!
//! And, the best part is that we only get a handful of precise numbers. Specifically, this crate
//! can generate 67,108,865 unique floating-point numbers between 0.0 and 1.0, which should be more
//! than sufficient for most use cases (hopefully).
//!
//! ## Supported ranges of z-indexes
//!
//! ```markdown
//! | upper                     | middle                 | lower                       |
//! | ------------------------- | ---------------------- | --------------------------- |
//! | `2130706432..=2147483647` | `-16777215..=16777216` | `-2147483647..=-2130706431` |
//! ```
//!
//! Later, I aim to expand this to allow for customizable ranges, but for now, this should be adequate.

const MAX_CSS_Z: i32 = 2147483647;

const EXP: i32 = 7;
const X: i32 = (EXP + 1) / 4;

const EXP_OFFSET1: i32 = 0;
const EXP_OFFSET2: i32 = X;
const EXP_OFFSET3: i32 = EXP_OFFSET2 * 3;

const MANTISSA: i32 = 8388608;
const MANTISSA_X: i32 = 8388608 * X;

const RANGE_UPPER_1: i32 = MAX_CSS_Z;
const RANGE_LOWER_1: i32 = MAX_CSS_Z - MANTISSA_X + 1;

const RANGE_UPPER_2: i32 = MANTISSA_X;
const RANGE_LOWER_2: i32 = -MANTISSA_X + 1;

const RANGE_UPPER_3: i32 = -MAX_CSS_Z + MANTISSA_X;
const RANGE_LOWER_3: i32 = -MAX_CSS_Z;

pub fn normalize(z: i32) -> f32 {
    assert!(
        (z >= RANGE_LOWER_1)
            || (RANGE_LOWER_2 <= z && z <= RANGE_UPPER_2)
            || (RANGE_LOWER_3 <= z && z <= RANGE_UPPER_3),
        "Unsupported z-index value: {}",
        z
    );

    match z {
        RANGE_UPPER_1 => 1.0,
        RANGE_LOWER_3 => 0.0,
        RANGE_LOWER_1..=RANGE_UPPER_1 => normalize_helper(z, RANGE_UPPER_1, EXP_OFFSET1),
        RANGE_LOWER_2..=RANGE_UPPER_2 => normalize_helper(z, RANGE_UPPER_2, EXP_OFFSET2),
        RANGE_LOWER_3..=RANGE_UPPER_3 => normalize_helper(z, RANGE_UPPER_3, EXP_OFFSET3),
        _ => unreachable!(),
    }
}

fn normalize_helper(z_: i32, upper_bound: i32, exp_offset: i32) -> f32 {
    let z = upper_bound - z_;
    let quo = z / MANTISSA;
    let rem = z % MANTISSA;
    let normal = 2f32.powi(-quo - exp_offset);

    // Returns the `n`th subnormal number for the given `normal`.
    f32::from_bits(normal.to_bits() - rem as u32)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[should_panic]
    fn test_unsupported_z1() {
        normalize(RANGE_LOWER_1 - 1);
    }
    #[test]
    #[should_panic]
    fn test_unsupported_z2() {
        normalize(RANGE_UPPER_2 + 1);
    }
    #[test]
    #[should_panic]
    fn test_unsupported_z3() {
        normalize(RANGE_LOWER_2 - 1);
    }
    #[test]
    #[should_panic]
    fn test_unsupported_z4() {
        normalize(RANGE_LOWER_3 - 1);
    }
    #[test]
    #[should_panic]
    fn test_unsupported_z5() {
        normalize(RANGE_UPPER_3 + 1);
    }

    #[test]
    fn test_supported_z1() {
        normalize(RANGE_UPPER_1);
    }
    #[test]
    fn test_supported_z2() {
        normalize(RANGE_LOWER_1);
    }
    #[test]
    fn test_supported_z3() {
        normalize(RANGE_LOWER_2);
    }
    #[test]
    fn test_supported_z4() {
        normalize(RANGE_UPPER_2);
    }
    #[test]
    fn test_supported_z5() {
        normalize(RANGE_LOWER_3);
    }
    #[test]
    fn test_supported_z6() {
        normalize(RANGE_UPPER_3);
    }

    #[ignore]
    #[test]
    fn test_normalize_upper() {
        let mut prev = -f32::MIN_POSITIVE;
        let mut count = 0;
        for i in RANGE_LOWER_1..=RANGE_UPPER_1 {
            count += 1;
            let curr = normalize(i);
            assert!(curr > prev, "{i}: {} < {}", curr, prev);
            prev = curr;
        }
        eprintln!("{count}");
    }

    #[ignore]
    #[test]
    fn test_normalize_middle() {
        let mut prev = -f32::MIN_POSITIVE;
        let mut count = 0;
        for i in RANGE_LOWER_2..=RANGE_UPPER_2 {
            count += 1;
            let curr = normalize(i);
            assert!(curr > prev, "{i}: {} < {}", curr, prev);
            prev = curr;
        }
        eprintln!("{count}");
    }

    #[ignore]
    #[test]
    fn test_normalize_lower() {
        let mut prev = -f32::MIN_POSITIVE;
        let mut count = 0;
        for i in RANGE_LOWER_3..=RANGE_UPPER_3 {
            count += 1;
            let curr = normalize(i);
            assert!(curr > prev, "{i}: {} < {}", curr, prev);
            prev = curr;
        }
        eprintln!("{count}");
    }

    #[test]
    fn test_normalize_all() {
        test_normalize_lower();
        test_normalize_middle();
        test_normalize_upper();
        test_continuity();
    }

    #[ignore]
    #[test]
    fn test_continuity() {
        let mut prev = -f32::MIN_POSITIVE;
        let curr = normalize(RANGE_UPPER_3);
        assert!(curr > prev, "{}: {} > {}", RANGE_UPPER_3, curr, prev);
        eprintln!("(LU): {curr} : {}", RANGE_UPPER_3);
        prev = curr;
        let curr = normalize(RANGE_LOWER_2);
        assert!(curr > prev, "{}: {} > {}", RANGE_LOWER_2, curr, prev);
        eprintln!("(ML): {curr} : {}", RANGE_LOWER_2);
        prev = curr;
        let curr = normalize(RANGE_UPPER_2);
        assert!(curr > prev, "{}: {} > {}", RANGE_UPPER_2, curr, prev);
        eprintln!("(MU): {curr} : {}", RANGE_UPPER_2);
        prev = curr;
        let curr = normalize(RANGE_LOWER_1);
        assert!(curr > prev, "{}: {} > {}", RANGE_LOWER_1, curr, prev);
        eprintln!("(UL): {curr} : {}", RANGE_LOWER_1);
    }
}