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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
use num::One;
use std::fmt::Debug;
use std::ops::Range;
use std::marker::PhantomData;
use crate::idx::Idx;
use crate::deser::fits::keywords::MocDim;
const N_RESERVED_BITS: u8 = 2;
pub trait Bounded<T> {
fn upper_bound_exclusive() -> T;
}
impl<T, Q> Bounded<T> for Q where T: Idx, Q: MocQty<T> {
fn upper_bound_exclusive() -> T {
Self::n_cells_max()
}
}
pub trait MocableQty: PartialEq + Eq + Send + Sync + Clone + Debug{
const NAME: &'static str;
const PREFIX: char;
const DIM: u8;
const N_D0_CELLS: u8;
const N_D0_BITS: u8 = n_bits_to_code_from_0_to_n_exclusive(Self::N_D0_CELLS);
const LEVEL_MASK: u8 = (1 << Self::DIM) - 1;
const MOC_DIM: MocDim;
const HAS_COOSYS: bool;
const HAS_TIMESYS: bool;
fn mult_by_dim<T: Idx>(v: T) -> T;
fn div_by_dim<T: Idx>(v: T) -> T;
#[inline(always)]
fn shift(delta_depth: u8) -> u8 {
Self::mult_by_dim(delta_depth)
}
}
const fn n_bits_to_code_from_0_to_n_exclusive(n: u8) -> u8 {
let n_bits_in_u8 = u8::N_BITS as u32;
let index_max = n - 1;
(n_bits_in_u8 - index_max.leading_zeros()) as u8
}
pub trait MocQty<T>: MocableQty where T: Idx
{
const MAX_DEPTH: u8 = (T::N_BITS - (N_RESERVED_BITS + Self::N_D0_BITS)) / Self::DIM;
const MAX_SHIFT: u32 = (Self::DIM * Self::MAX_DEPTH) as u32;
fn n_cells_max() -> T {
let nd0: T = Self::N_D0_CELLS.into();
nd0.unsigned_shl(Self::MAX_SHIFT)
}
fn n_cells(depth: u8) -> T {
let nd0: T = Self::N_D0_CELLS.into();
nd0.unsigned_shl(Self::shift(depth) as u32)
}
fn delta_depth_max_from_n_bits(n_bits: u8) -> u8 {
Self::delta_depth_max_from_n_bits_unchecked(n_bits).min(Self::MAX_DEPTH)
}
fn delta_depth_max_from_n_bits_unchecked(n_bits: u8) -> u8 {
n_bits >> (Self::DIM - 1)
}
fn delta_with_depth_max(depth: u8) -> u8 {
Self::MAX_DEPTH - depth
}
fn shift_from_depth_max(depth: u8) -> u8 {
Self::shift(Self::delta_with_depth_max(depth))
}
#[inline(always)]
fn get_msb(x: T) -> u32 {
T::N_BITS as u32 - x.leading_zeros() - 1
}
#[inline(always)]
fn get_lsb(x: T) -> u32 {
x.trailing_zeros() as u32
}
#[inline(always)]
fn compute_min_depth(x: T) -> u8 {
let dd = Self::div_by_dim(x.trailing_zeros() as u8).min(Self::MAX_DEPTH);
Self::MAX_DEPTH - dd
}
#[inline(always)]
fn from_uniq_gen(uniq: T) -> (u8, T) {
let depth = Self::div_by_dim(T::N_BITS - uniq.leading_zeros() as u8 - 1 - Self::N_D0_BITS);
let idx = uniq & !Self::sentinel_bit(depth);
(depth as u8, idx)
}
#[inline(always)]
fn to_uniq_gen(depth: u8, idx: T) -> T {
Self::sentinel_bit(depth) | idx
}
#[inline(always)]
fn sentinel_bit(depth: u8) -> T {
T::one().unsigned_shl(Self::N_D0_BITS as u32).unsigned_shl(Self::shift(depth) as u32)
}
#[inline(always)]
fn uniq_gen_to_range(uniq: T) -> Range<T> {
let (depth, pix) = Self::from_uniq_gen(uniq);
let tdd = ((Self::MAX_DEPTH - depth) << 1) as u32;
Range {
start: pix.unsigned_shl(tdd),
end: (pix + One::one()).unsigned_shl(tdd),
}
}
fn to_zuniq(depth: u8, idx: T) -> T {
let zuniq = (idx << 1) | T::one();
zuniq.unsigned_shl(Self::shift_from_depth_max(depth) as u32)
}
fn from_zuniq(zuniq: T) -> (u8, T) {
let n_trailing_zero = zuniq.trailing_zeros() as u8;
let delta_depth = Self::div_by_dim(n_trailing_zero);
let depth = Self::MAX_DEPTH - delta_depth;
let idx = zuniq >> (n_trailing_zero + 1) as usize;
(depth, idx)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Hpx<T: Idx> (std::marker::PhantomData<T>);
impl<T: Idx> MocableQty for Hpx<T> {
const NAME: &'static str = "HPX";
const PREFIX: char = 's';
const DIM: u8 = 2;
const N_D0_CELLS: u8 = 12;
const MOC_DIM: MocDim = MocDim::Space;
const HAS_COOSYS: bool = true;
const HAS_TIMESYS: bool = false;
#[inline(always)]
fn mult_by_dim<U: Idx>(v: U) -> U {
v << 1
}
#[inline(always)]
fn div_by_dim<U: Idx>(v: U) -> U {
v >> 1
}
}
impl<T> MocQty<T> for Hpx<T> where T: Idx { }
impl<T: Idx> Hpx<T> {
#[inline(always)]
pub fn from_uniq_hpx(uniq: T) -> (u8, T) {
let depth= (Self::get_msb(uniq) - 2) >> 1;
let idx = uniq - Self::four_shl_twice_depth(depth);
(depth as u8, idx)
}
#[inline(always)]
pub fn uniq_hpx(depth: u8, idx: T) -> T {
idx + Self::four_shl_twice_depth(depth as u32)
}
#[inline(always)]
pub fn four_shl_twice_depth(depth: u32) -> T {
T::one().unsigned_shl(2).unsigned_shl(depth << 1)
}
#[inline(always)]
pub fn uniq_hpx_to_range(uniq: T) -> Range<T> {
let (depth, pix) = Self::from_uniq_hpx(uniq);
let tdd = ((Self::MAX_DEPTH - depth) << 1) as u32;
Range {
start: pix.unsigned_shl(tdd),
end: (pix + One::one()).unsigned_shl(tdd),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Time<T: Idx> (PhantomData<T>);
impl<T: Idx> MocableQty for Time<T> {
const NAME: &'static str = "TIME";
const PREFIX: char = 't';
const DIM: u8 = 1;
const N_D0_CELLS: u8 = 2;
const MOC_DIM: MocDim = MocDim::Time;
const HAS_COOSYS: bool = false;
const HAS_TIMESYS: bool = true;
#[inline(always)]
fn mult_by_dim<U: Idx>(v: U) -> U {
v
}
#[inline(always)]
fn div_by_dim<U: Idx>(v: U) -> U {
v
}
}
impl<T> MocQty<T> for Time<T> where T: Idx { }
#[cfg(test)]
mod tests {
use crate::qty::{MocableQty, MocQty, Time, Hpx};
#[test]
fn test_hpx_uniq() {
for depth in 0..8 {
for idx in 0..Hpx::<u64>::n_cells(depth) {
assert_eq!((depth, idx), Hpx::<u64>::from_uniq_hpx(Hpx::<u64>::uniq_hpx(depth, idx)));
}
}
assert_eq!(Hpx::<u64>::DIM, 2);
assert_eq!(Hpx::<u64>::N_D0_CELLS, 12);
assert_eq!(Hpx::<u64>::N_D0_BITS, 4);
assert_eq!(Hpx::<u64>::LEVEL_MASK, 3);
assert_eq!(Hpx::<u64>::shift(1), 2);
assert_eq!(Hpx::<u64>::shift(10), 20);
assert_eq!(Hpx::<u64>::MAX_DEPTH, 29);
assert_eq!(Hpx::<u64>::MAX_SHIFT, 58);
assert_eq!(Hpx::<u64>::n_cells_max(), 12 * 4_u64.pow(29));
}
#[test]
fn test_hpx_zuniq() {
for depth in 0..8 {
for idx in 0..Hpx::<u64>::n_cells(depth) {
assert_eq!((depth, idx), Hpx::<u64>::from_zuniq(Hpx::<u64>::to_zuniq(depth, idx)));
}
}
}
#[test]
fn test_hpx() {
assert_eq!(Hpx::<u64>::DIM, 2);
assert_eq!(Hpx::<u64>::N_D0_CELLS, 12);
assert_eq!(Hpx::<u64>::N_D0_BITS, 4);
assert_eq!(Hpx::<u64>::LEVEL_MASK, 3);
assert_eq!(Hpx::<u64>::shift(1), 2);
assert_eq!(Hpx::<u64>::shift(10), 20);
assert_eq!(Hpx::<u64>::MAX_DEPTH, 29);
assert_eq!(Hpx::<u64>::MAX_SHIFT, 58);
assert_eq!(Hpx::<u64>::n_cells_max(), 12 * 4_u64.pow(29));
}
#[test]
fn test_time() {
assert_eq!(Time::<u64>::DIM, 1);
assert_eq!(Time::<u64>::N_D0_CELLS, 2);
assert_eq!(Time::<u64>::N_D0_BITS, 1);
assert_eq!(Time::<u64>::LEVEL_MASK, 1);
assert_eq!(Time::<u64>::shift(1), 1);
assert_eq!(Time::<u64>::shift(10), 10);
assert_eq!(Time::<u64>::MAX_DEPTH, 61);
assert_eq!(Time::<u64>::MAX_SHIFT, 61);
assert_eq!(Time::<u64>::n_cells_max(), 2_u64.pow(62));
}
}