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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
//! In this document, we define the precisions as a trait and we implement it for structs
//! ranging from Precision4 to Precision16. This is necessary so that the compiler can
//! know the size necessary to store the number of zeros, and allows us the save when using
//! a number of registers equal of inferior to 256 a Byte, compared to what is possible when
//! using a number of registers equal or inferior to 65536, which would make us waste another byte.
//!

use core::ops::{Index, IndexMut};
use core::{fmt::Debug, ops::SubAssign};

use serde::{Deserialize, Serialize};

use crate::{
    array_default::ArrayDefault,
    array_default::ArrayIterArgmin,
    array_default::{ArrayIter, ArrayIterArgmax},
    prelude::{precompute_linear_counting, One},
    primitive::Primitive,
    zeros::Zero,
};

pub trait WordType<const BITS: usize>: Precision {
    /// The type to use for the associated vector of words.
    /// The type of Words is always an array of u32, as this is the smallest
    /// type that can be used to store the number of registers.
    ///
    /// The length of the array is: ceil(PRECISION::NUMBER_OF_REGISTERS, 32 / BITS)
    ///
    /// We cannot use the above expression directly, as it would force the library
    /// user to propagate some very ugly constraints around.
    ///
    type Words: Copy
        + Debug
        + IndexMut<usize, Output = u32>
        + Index<usize, Output = u32>
        + Send
        + Sync
        + Eq
        + PartialEq
        + ArrayIter<u32>
        + ArrayDefault<u32>; // = [u32; ceil(PRECISION::NUMBER_OF_REGISTERS, 32 / BITS)]
    /// The register multeplicities is an array with the length of the largest possible
    /// value that can appear in a register. The value at index i is the number of registers
    /// that have a value equal to i, meaning the largest value that any register can have
    /// is the number of registers, i.e. 2^EXPONENT. This is the m parameter in the HyperLogLog.
    /// The lenth of the multeplicities array varies depending on the exponent and the number of bits,
    /// as the length of the array is equal to the maximal value that can be stored in a register.
    /// The value being stored in the register is the number of leading zeros in the hash of the
    /// value that is being inserted.
    /// When the register is of 1 bit, the maximal value is 1, and the length of the array is 2.
    /// When the register is of 2 bits, the maximal value is 3, and the length of the array is 4.
    /// When the register is of 3 bits, the maximal value is 7, and the length of the array is 8.
    /// When the register is of 4 bits, the maximal value is 15, and the length of the array is 16.
    /// When the register is of 5 bits, the maximal value is 31, and the length of the array is 32.
    /// Things start to get interesting for the cases of register at least 6 bits, as the maximal
    /// value representable in 6 bits is 63, but here we have to take into account the fact that
    /// the precision will be the limiting factor. In fact, the higher the precision, the larger
    /// the number of registers, and therefore more bits of the hash will be used to sample the
    /// register associated to a given value. For this reason, in those cases, the maximal value
    /// that we may encounter stored in the register is not 63, but 64 - (PRECISION - 1) + 1.
    /// So, when the register is of 6 bits, we need to consider the precision as follows:
    /// When the precision is 4, the maximal value is 61, and the length of the array is 62.
    /// When the precision is 5, the length of the array is 61.
    /// When the precision is 6, the length of the array is 60.
    /// The decrease in length continues linearly until the maximal precision for which we
    /// have experimental parameters, which is 18, where the length of the array is 48.
    type Registermulteplicities: Index<usize, Output = Self::NumberOfZeros>
        + IndexMut<usize, Output = Self::NumberOfZeros>
        + Copy
        + Debug
        + Eq
        + ArrayIterArgmin<Self::NumberOfZeros>
        + ArrayIterArgmax<Self::NumberOfZeros>
        + ArrayDefault<Self::NumberOfZeros>
        + ArrayIter<Self::NumberOfZeros>;
}

pub trait Precision: Default + Copy + Eq + Serialize + Debug + Send + Sync {
    /// The data type to use for the number of zeros registers counter.
    /// This should be the smallest possinle data type that allows us to count
    /// all the registers without overflowing. We can tollerate a one-off error
    /// when counting the number of zeros, as it will be corrected when computing
    /// the cardinality as it is known before hand whether this can happen at all.
    type NumberOfZeros: Copy
        + Debug
        + Eq
        + PartialEq
        + Primitive<usize>
        + Send
        + Sync
        + Zero
        + One
        + Ord
        + PartialOrd
        + SubAssign;
    /// The exponent of the number of registers, meaning the number of registers
    /// that will be used is 2^EXPONENT. This is the p parameter in the HyperLogLog.
    const EXPONENT: usize;
    /// The number of registers that will be used.
    const NUMBER_OF_REGISTERS: usize = 1 << Self::EXPONENT;
    /// The maximal number that can be represented with the selected NumberOfZeros.
    const MAXIMAL: usize;
    /// Type for small corrections:
    type SmallCorrections: Index<usize, Output = f32> + Copy;
    type Registers: Index<usize, Output = u32> + Copy + ArrayDefault<u32> + ArrayIter<u32>;
    /// The precomputed small corrections used in the HyperLogLog algorithm for better performance.
    const SMALL_CORRECTIONS: Self::SmallCorrections;
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Precision4;

impl Precision for Precision4 {
    type NumberOfZeros = u8;
    const EXPONENT: usize = 4;
    const MAXIMAL: usize = u8::MAX as usize;
    type SmallCorrections = [f32; 16];
    type Registers = [u32; 16];
    const SMALL_CORRECTIONS: Self::SmallCorrections =
        precompute_linear_counting::<{ Self::NUMBER_OF_REGISTERS }>();
}

impl WordType<1> for Precision4 {
    type Words = [u32; 1];
    type Registermulteplicities = [Self::NumberOfZeros; 2];
}

impl WordType<2> for Precision4 {
    type Words = [u32; 1];
    type Registermulteplicities = [Self::NumberOfZeros; 4];
}

impl WordType<3> for Precision4 {
    type Words = [u32; 2];
    type Registermulteplicities = [Self::NumberOfZeros; 8];
}

impl WordType<4> for Precision4 {
    type Words = [u32; 2];
    type Registermulteplicities = [Self::NumberOfZeros; 16];
}

impl WordType<5> for Precision4 {
    type Words = [u32; 3];
    type Registermulteplicities = [Self::NumberOfZeros; 32];
}

impl WordType<6> for Precision4 {
    type Words = [u32; 4];
    type Registermulteplicities = [Self::NumberOfZeros; 62];
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Precision5;

impl Precision for Precision5 {
    type NumberOfZeros = u8;
    const EXPONENT: usize = 5;
    const MAXIMAL: usize = u8::MAX as usize;
    type SmallCorrections = [f32; 32];
    type Registers = [u32; 32];
    const SMALL_CORRECTIONS: Self::SmallCorrections =
        precompute_linear_counting::<{ Self::NUMBER_OF_REGISTERS }>();
}

impl WordType<1> for Precision5 {
    type Words = [u32; 1];
    type Registermulteplicities = [Self::NumberOfZeros; 2];
}

impl WordType<2> for Precision5 {
    type Words = [u32; 2];
    type Registermulteplicities = [Self::NumberOfZeros; 4];
}

impl WordType<3> for Precision5 {
    type Words = [u32; 4];
    type Registermulteplicities = [Self::NumberOfZeros; 8];
}

impl WordType<4> for Precision5 {
    type Words = [u32; 4];
    type Registermulteplicities = [Self::NumberOfZeros; 16];
}

impl WordType<5> for Precision5 {
    type Words = [u32; 6];
    type Registermulteplicities = [Self::NumberOfZeros; 32];
}

impl WordType<6> for Precision5 {
    type Words = [u32; 7];
    type Registermulteplicities = [Self::NumberOfZeros; 61];
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Precision6;

impl Precision for Precision6 {
    type NumberOfZeros = u8;
    const EXPONENT: usize = 6;
    const MAXIMAL: usize = u8::MAX as usize;
    type SmallCorrections = [f32; 64];
    type Registers = [u32; 64];
    const SMALL_CORRECTIONS: Self::SmallCorrections =
        precompute_linear_counting::<{ Self::NUMBER_OF_REGISTERS }>();
}

impl WordType<1> for Precision6 {
    type Words = [u32; 2];
    type Registermulteplicities = [Self::NumberOfZeros; 2];
}

impl WordType<2> for Precision6 {
    type Words = [u32; 4];
    type Registermulteplicities = [Self::NumberOfZeros; 4];
}

impl WordType<3> for Precision6 {
    type Words = [u32; 7];
    type Registermulteplicities = [Self::NumberOfZeros; 8];
}

impl WordType<4> for Precision6 {
    type Words = [u32; 8];
    type Registermulteplicities = [Self::NumberOfZeros; 16];
}

impl WordType<5> for Precision6 {
    type Words = [u32; 11];
    type Registermulteplicities = [Self::NumberOfZeros; 32];
}

impl WordType<6> for Precision6 {
    type Words = [u32; 13];
    type Registermulteplicities = [Self::NumberOfZeros; 60];
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Precision7;

impl Precision for Precision7 {
    type NumberOfZeros = u8;
    const EXPONENT: usize = 7;
    const MAXIMAL: usize = u8::MAX as usize;
    type SmallCorrections = [f32; 128];
    type Registers = [u32; 128];
    const SMALL_CORRECTIONS: Self::SmallCorrections =
        precompute_linear_counting::<{ Self::NUMBER_OF_REGISTERS }>();
}

impl WordType<1> for Precision7 {
    type Words = [u32; 4];
    type Registermulteplicities = [Self::NumberOfZeros; 2];
}

impl WordType<2> for Precision7 {
    type Words = [u32; 8];
    type Registermulteplicities = [Self::NumberOfZeros; 4];
}

impl WordType<3> for Precision7 {
    type Words = [u32; 13];
    type Registermulteplicities = [Self::NumberOfZeros; 8];
}

impl WordType<4> for Precision7 {
    type Words = [u32; 16];
    type Registermulteplicities = [Self::NumberOfZeros; 16];
}

impl WordType<5> for Precision7 {
    type Words = [u32; 22];
    type Registermulteplicities = [Self::NumberOfZeros; 32];
}

impl WordType<6> for Precision7 {
    type Words = [u32; 26];
    type Registermulteplicities = [Self::NumberOfZeros; 59];
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Precision8;

impl Precision for Precision8 {
    type NumberOfZeros = u16;
    const EXPONENT: usize = 8;
    const MAXIMAL: usize = u16::MAX as usize;
    type SmallCorrections = [f32; 256];
    type Registers = [u32; 256];
    const SMALL_CORRECTIONS: Self::SmallCorrections =
        precompute_linear_counting::<{ Self::NUMBER_OF_REGISTERS }>();
}
impl WordType<1> for Precision8 {
    type Words = [u32; 8];
    type Registermulteplicities = [Self::NumberOfZeros; 2];
}

impl WordType<2> for Precision8 {
    type Words = [u32; 16];
    type Registermulteplicities = [Self::NumberOfZeros; 4];
}

impl WordType<3> for Precision8 {
    type Words = [u32; 26];
    type Registermulteplicities = [Self::NumberOfZeros; 8];
}

impl WordType<4> for Precision8 {
    type Words = [u32; 32];
    type Registermulteplicities = [Self::NumberOfZeros; 16];
}

impl WordType<5> for Precision8 {
    type Words = [u32; 43];
    type Registermulteplicities = [Self::NumberOfZeros; 32];
}

impl WordType<6> for Precision8 {
    type Words = [u32; 52];
    type Registermulteplicities = [Self::NumberOfZeros; 58];
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Precision9;

impl Precision for Precision9 {
    type NumberOfZeros = u16;
    const EXPONENT: usize = 9;
    const MAXIMAL: usize = 65536;
    type SmallCorrections = [f32; 512];
    type Registers = [u32; 512];
    const SMALL_CORRECTIONS: Self::SmallCorrections =
        precompute_linear_counting::<{ Self::NUMBER_OF_REGISTERS }>();
}
impl WordType<1> for Precision9 {
    type Words = [u32; 16];
    type Registermulteplicities = [Self::NumberOfZeros; 2];
}

impl WordType<2> for Precision9 {
    type Words = [u32; 32];
    type Registermulteplicities = [Self::NumberOfZeros; 4];
}

impl WordType<3> for Precision9 {
    type Words = [u32; 52];
    type Registermulteplicities = [Self::NumberOfZeros; 8];
}

impl WordType<4> for Precision9 {
    type Words = [u32; 64];
    type Registermulteplicities = [Self::NumberOfZeros; 16];
}

impl WordType<5> for Precision9 {
    type Words = [u32; 86];
    type Registermulteplicities = [Self::NumberOfZeros; 32];
}

impl WordType<6> for Precision9 {
    type Words = [u32; 103];
    type Registermulteplicities = [Self::NumberOfZeros; 57];
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Precision10;

impl Precision for Precision10 {
    type NumberOfZeros = u16;
    const EXPONENT: usize = 10;
    const MAXIMAL: usize = u16::MAX as usize;
    type SmallCorrections = [f32; 1024];
    type Registers = [u32; 1024];
    const SMALL_CORRECTIONS: Self::SmallCorrections =
        precompute_linear_counting::<{ Self::NUMBER_OF_REGISTERS }>();
}
impl WordType<1> for Precision10 {
    type Words = [u32; 32];
    type Registermulteplicities = [Self::NumberOfZeros; 2];
}

impl WordType<2> for Precision10 {
    type Words = [u32; 64];
    type Registermulteplicities = [Self::NumberOfZeros; 4];
}

impl WordType<3> for Precision10 {
    type Words = [u32; 103];
    type Registermulteplicities = [Self::NumberOfZeros; 8];
}

impl WordType<4> for Precision10 {
    type Words = [u32; 128];
    type Registermulteplicities = [Self::NumberOfZeros; 16];
}

impl WordType<5> for Precision10 {
    type Words = [u32; 171];
    type Registermulteplicities = [Self::NumberOfZeros; 32];
}

impl WordType<6> for Precision10 {
    type Words = [u32; 205];
    type Registermulteplicities = [Self::NumberOfZeros; 56];
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Precision11;

impl Precision for Precision11 {
    type NumberOfZeros = u16;
    const EXPONENT: usize = 11;
    const MAXIMAL: usize = u16::MAX as usize;
    type SmallCorrections = [f32; 2048];
    type Registers = [u32; 2048];
    const SMALL_CORRECTIONS: Self::SmallCorrections =
        precompute_linear_counting::<{ Self::NUMBER_OF_REGISTERS }>();
}
impl WordType<1> for Precision11 {
    type Words = [u32; 64];
    type Registermulteplicities = [Self::NumberOfZeros; 2];
}

impl WordType<2> for Precision11 {
    type Words = [u32; 128];
    type Registermulteplicities = [Self::NumberOfZeros; 4];
}

impl WordType<3> for Precision11 {
    type Words = [u32; 205];
    type Registermulteplicities = [Self::NumberOfZeros; 8];
}

impl WordType<4> for Precision11 {
    type Words = [u32; 256];
    type Registermulteplicities = [Self::NumberOfZeros; 16];
}

impl WordType<5> for Precision11 {
    type Words = [u32; 342];
    type Registermulteplicities = [Self::NumberOfZeros; 32];
}

impl WordType<6> for Precision11 {
    type Words = [u32; 410];
    type Registermulteplicities = [Self::NumberOfZeros; 55];
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Precision12;

impl Precision for Precision12 {
    type NumberOfZeros = u16;
    const EXPONENT: usize = 12;
    const MAXIMAL: usize = u16::MAX as usize;
    type SmallCorrections = [f32; 4096];
    type Registers = [u32; 4096];
    const SMALL_CORRECTIONS: Self::SmallCorrections =
        precompute_linear_counting::<{ Self::NUMBER_OF_REGISTERS }>();
}
impl WordType<1> for Precision12 {
    type Words = [u32; 128];
    type Registermulteplicities = [Self::NumberOfZeros; 2];
}

impl WordType<2> for Precision12 {
    type Words = [u32; 256];
    type Registermulteplicities = [Self::NumberOfZeros; 4];
}

impl WordType<3> for Precision12 {
    type Words = [u32; 410];
    type Registermulteplicities = [Self::NumberOfZeros; 8];
}

impl WordType<4> for Precision12 {
    type Words = [u32; 512];
    type Registermulteplicities = [Self::NumberOfZeros; 16];
}

impl WordType<5> for Precision12 {
    type Words = [u32; 683];
    type Registermulteplicities = [Self::NumberOfZeros; 32];
}

impl WordType<6> for Precision12 {
    type Words = [u32; 820];
    type Registermulteplicities = [Self::NumberOfZeros; 54];
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Precision13;

impl Precision for Precision13 {
    type NumberOfZeros = u16;
    const EXPONENT: usize = 13;
    const MAXIMAL: usize = u16::MAX as usize;
    type SmallCorrections = [f32; 8192];
    type Registers = [u32; 8192];
    const SMALL_CORRECTIONS: Self::SmallCorrections =
        precompute_linear_counting::<{ Self::NUMBER_OF_REGISTERS }>();
}
impl WordType<1> for Precision13 {
    type Words = [u32; 256];
    type Registermulteplicities = [Self::NumberOfZeros; 2];
}

impl WordType<2> for Precision13 {
    type Words = [u32; 512];
    type Registermulteplicities = [Self::NumberOfZeros; 4];
}

impl WordType<3> for Precision13 {
    type Words = [u32; 820];
    type Registermulteplicities = [Self::NumberOfZeros; 8];
}

impl WordType<4> for Precision13 {
    type Words = [u32; 1024];
    type Registermulteplicities = [Self::NumberOfZeros; 16];
}

impl WordType<5> for Precision13 {
    type Words = [u32; 1366];
    type Registermulteplicities = [Self::NumberOfZeros; 32];
}

impl WordType<6> for Precision13 {
    type Words = [u32; 1639];
    type Registermulteplicities = [Self::NumberOfZeros; 53];
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Precision14;

impl Precision for Precision14 {
    type NumberOfZeros = u16;
    const EXPONENT: usize = 14;
    const MAXIMAL: usize = u16::MAX as usize;
    type SmallCorrections = [f32; 16384];
    type Registers = [u32; 16384];
    const SMALL_CORRECTIONS: Self::SmallCorrections =
        precompute_linear_counting::<{ Self::NUMBER_OF_REGISTERS }>();
}
impl WordType<1> for Precision14 {
    type Words = [u32; 512];
    type Registermulteplicities = [Self::NumberOfZeros; 2];
}

impl WordType<2> for Precision14 {
    type Words = [u32; 1024];
    type Registermulteplicities = [Self::NumberOfZeros; 4];
}

impl WordType<3> for Precision14 {
    type Words = [u32; 1639];
    type Registermulteplicities = [Self::NumberOfZeros; 8];
}

impl WordType<4> for Precision14 {
    type Words = [u32; 2048];
    type Registermulteplicities = [Self::NumberOfZeros; 16];
}

impl WordType<5> for Precision14 {
    type Words = [u32; 2731];
    type Registermulteplicities = [Self::NumberOfZeros; 32];
}

impl WordType<6> for Precision14 {
    type Words = [u32; 3277];
    type Registermulteplicities = [Self::NumberOfZeros; 52];
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Precision15;

impl Precision for Precision15 {
    type NumberOfZeros = u16;
    const EXPONENT: usize = 15;
    const MAXIMAL: usize = u16::MAX as usize;
    type SmallCorrections = [f32; 32768];
    type Registers = [u32; 32768];
    const SMALL_CORRECTIONS: Self::SmallCorrections =
        precompute_linear_counting::<{ Self::NUMBER_OF_REGISTERS }>();
}
impl WordType<1> for Precision15 {
    type Words = [u32; 1024];
    type Registermulteplicities = [Self::NumberOfZeros; 2];
}

impl WordType<2> for Precision15 {
    type Words = [u32; 2048];
    type Registermulteplicities = [Self::NumberOfZeros; 4];
}

impl WordType<3> for Precision15 {
    type Words = [u32; 3277];
    type Registermulteplicities = [Self::NumberOfZeros; 8];
}

impl WordType<4> for Precision15 {
    type Words = [u32; 4096];
    type Registermulteplicities = [Self::NumberOfZeros; 16];
}

impl WordType<5> for Precision15 {
    type Words = [u32; 5462];
    type Registermulteplicities = [Self::NumberOfZeros; 32];
}

impl WordType<6> for Precision15 {
    type Words = [u32; 6554];
    type Registermulteplicities = [Self::NumberOfZeros; 51];
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Precision16;

impl Precision for Precision16 {
    // For Precision 16, we are still able to use a u16, but we
    // will loose the ability to count one of the registers.
    type NumberOfZeros = u32;
    const EXPONENT: usize = 16;
    const MAXIMAL: usize = u32::MAX as usize;
    type SmallCorrections = [f32; 65536];
    type Registers = [u32; 65536];
    const SMALL_CORRECTIONS: Self::SmallCorrections =
        precompute_linear_counting::<{ Self::NUMBER_OF_REGISTERS }>();
}
impl WordType<1> for Precision16 {
    type Words = [u32; 2048];
    type Registermulteplicities = [Self::NumberOfZeros; 2];
}

impl WordType<2> for Precision16 {
    type Words = [u32; 4096];
    type Registermulteplicities = [Self::NumberOfZeros; 4];
}

impl WordType<3> for Precision16 {
    type Words = [u32; 6554];
    type Registermulteplicities = [Self::NumberOfZeros; 8];
}

impl WordType<4> for Precision16 {
    type Words = [u32; 8192];
    type Registermulteplicities = [Self::NumberOfZeros; 16];
}

impl WordType<5> for Precision16 {
    type Words = [u32; 10923];
    type Registermulteplicities = [Self::NumberOfZeros; 32];
}

impl WordType<6> for Precision16 {
    type Words = [u32; 13108];
    type Registermulteplicities = [Self::NumberOfZeros; 50];
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Precision17;

impl Precision for Precision17 {
    type NumberOfZeros = u32;
    const EXPONENT: usize = 17;
    const MAXIMAL: usize = u32::MAX as usize;
    type SmallCorrections = [f32; 131072];
    type Registers = [u32; 131072];
    const SMALL_CORRECTIONS: Self::SmallCorrections =
        precompute_linear_counting::<{ Self::NUMBER_OF_REGISTERS }>();
}
impl WordType<1> for Precision17 {
    type Words = [u32; 4096];
    type Registermulteplicities = [Self::NumberOfZeros; 2];
}

impl WordType<2> for Precision17 {
    type Words = [u32; 8192];
    type Registermulteplicities = [Self::NumberOfZeros; 4];
}

impl WordType<3> for Precision17 {
    type Words = [u32; 13108];
    type Registermulteplicities = [Self::NumberOfZeros; 8];
}

impl WordType<4> for Precision17 {
    type Words = [u32; 16384];
    type Registermulteplicities = [Self::NumberOfZeros; 16];
}

impl WordType<5> for Precision17 {
    type Words = [u32; 21846];
    type Registermulteplicities = [Self::NumberOfZeros; 32];
}

impl WordType<6> for Precision17 {
    type Words = [u32; 26215];
    type Registermulteplicities = [Self::NumberOfZeros; 49];
}

#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Precision18;

impl Precision for Precision18 {
    type NumberOfZeros = u32;
    const EXPONENT: usize = 18;
    const MAXIMAL: usize = u32::MAX as usize;
    type SmallCorrections = [f32; 262144];
    type Registers = [u32; 262144];
    const SMALL_CORRECTIONS: Self::SmallCorrections =
        precompute_linear_counting::<{ Self::NUMBER_OF_REGISTERS }>();
}
impl WordType<1> for Precision18 {
    type Words = [u32; 8192];
    type Registermulteplicities = [Self::NumberOfZeros; 2];
}

impl WordType<2> for Precision18 {
    type Words = [u32; 16384];
    type Registermulteplicities = [Self::NumberOfZeros; 4];
}

impl WordType<3> for Precision18 {
    type Words = [u32; 26215];
    type Registermulteplicities = [Self::NumberOfZeros; 8];
}

impl WordType<4> for Precision18 {
    type Words = [u32; 32768];
    type Registermulteplicities = [Self::NumberOfZeros; 16];
}

impl WordType<5> for Precision18 {
    type Words = [u32; 43691];
    type Registermulteplicities = [Self::NumberOfZeros; 32];
}

impl WordType<6> for Precision18 {
    type Words = [u32; 52429];
    type Registermulteplicities = [Self::NumberOfZeros; 48];
}