      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

    //! **Ignore me!** This module is for things that are conceptually private but that must
    //! be made public for typenum to work correctly.
    //!
    //! Unless you are working on typenum itself, **there is no need to view anything here**.
    //!
    //! Certainly don't implement any of the traits here for anything.
    //!
    //!
    //! Just look away.
    //!
    //!
    //! Loooooooooooooooooooooooooooooooooook awaaaaaaaaaaaayyyyyyyyyyyyyyyyyyyyyyyyyyyyy...
    //!
    //!
    //! If you do manage to find something of use in here, please let me know. If you can make a
    //! compelling case, it may be moved out of __private.
    //!
    //! Note: Aliases for private type operators will all be named simply that operator followed
    //! by an abbreviated name of its associated type.
    //!

    #![doc(hidden)]

    use bit::{Bit, B0, B1};
    use uint::{UInt, UTerm, Unsigned};

    /// A marker for restricting a method on a public trait to internal use only.
    pub(crate) enum Internal {}

    pub trait InternalMarker {}

    impl InternalMarker for Internal {}

    /// Convenience trait. Calls `Invert` -> `TrimTrailingZeros` -> `Invert`
    pub trait Trim {
        type Output;

        fn trim(self) -> Self::Output;
    }
    pub type TrimOut<A> = <A as Trim>::Output;

    /// Gets rid of all zeros until it hits a one.

    // ONLY IMPLEMENT FOR INVERTED NUMBERS!
    pub trait TrimTrailingZeros {
        type Output;

        fn trim_trailing_zeros(self) -> Self::Output;
    }
    pub type TrimTrailingZerosOut<A> = <A as TrimTrailingZeros>::Output;

    /// Converts between standard numbers and inverted ones that have the most significant
    /// digit on the outside.
    pub trait Invert {
        type Output;

        fn invert(self) -> Self::Output;
    }
    pub type InvertOut<A> = <A as Invert>::Output;

    /// Doubly private! Called by invert to make the magic happen once its done the first step.
    /// The Rhs is what we've got so far.
    pub trait PrivateInvert<Rhs> {
        type Output;

        fn private_invert(self, Rhs) -> Self::Output;
    }
    pub type PrivateInvertOut<A, Rhs> = <A as PrivateInvert<Rhs>>::Output;

    /// Terminating character for `InvertedUInt`s
    pub struct InvertedUTerm;

    /// Inverted `UInt` (has most significant digit on the outside)
    pub struct InvertedUInt<IU: InvertedUnsigned, B: Bit> {
        msb: IU,
        lsb: B,
    }

    /// Does the real anding for `UInt`s; `And` just calls this and then `Trim`.
    pub trait PrivateAnd<Rhs = Self> {
        type Output;

        fn private_and(self, Rhs) -> Self::Output;
    }
    pub type PrivateAndOut<A, Rhs> = <A as PrivateAnd<Rhs>>::Output;

    /// Does the real xoring for `UInt`s; `Xor` just calls this and then `Trim`.
    pub trait PrivateXor<Rhs = Self> {
        type Output;

        fn private_xor(self, Rhs) -> Self::Output;
    }
    pub type PrivateXorOut<A, Rhs> = <A as PrivateXor<Rhs>>::Output;

    /// Does the real subtraction for `UInt`s; `Sub` just calls this and then `Trim`.
    pub trait PrivateSub<Rhs = Self> {
        type Output;

        fn private_sub(self, Rhs) -> Self::Output;
    }
    pub type PrivateSubOut<A, Rhs> = <A as PrivateSub<Rhs>>::Output;

    /// Used for addition of signed integers; `C = P.cmp(N)`
    /// Assumes `P = Self` is positive and `N` is negative
    /// where `P` and `N` are both passed as unsigned integers
    pub trait PrivateIntegerAdd<C, N> {
        type Output;

        fn private_integer_add(self, C, N) -> Self::Output;
    }
    pub type PrivateIntegerAddOut<P, C, N> = <P as PrivateIntegerAdd<C, N>>::Output;

    pub trait PrivatePow<Y, N> {
        type Output;

        fn private_pow(self, Y, N) -> Self::Output;
    }
    pub type PrivatePowOut<A, Y, N> = <A as PrivatePow<Y, N>>::Output;

    /// Performs `Shl` on `Lhs` so that `SizeOf(Lhs) = SizeOf(Rhs)`
    /// Fails if `SizeOf(Lhs) > SizeOf(Rhs)`
    pub trait ShiftDiff<Rhs> {
        type Output;
    }
    pub type ShiftDiffOut<A, Rhs> = <A as ShiftDiff<Rhs>>::Output;

    /// Gives `SizeOf(Lhs) - SizeOf(Rhs)`
    pub trait BitDiff<Rhs> {
        type Output;
    }
    pub type BitDiffOut<A, Rhs> = <A as BitDiff<Rhs>>::Output;

    /// Inverted unsigned numbers
    pub trait InvertedUnsigned {
        fn to_u64() -> u64;
    }

    impl InvertedUnsigned for InvertedUTerm {
        #[inline]
        fn to_u64() -> u64 {
            0
        }
    }

    impl<IU: InvertedUnsigned, B: Bit> InvertedUnsigned for InvertedUInt<IU, B> {
        #[inline]
        fn to_u64() -> u64 {
            u64::from(B::to_u8()) | IU::to_u64() << 1
        }
    }

    impl Invert for UTerm {
        type Output = InvertedUTerm;

        #[inline]
        fn invert(self) -> Self::Output {
            InvertedUTerm
        }
    }

    impl<U: Unsigned, B: Bit> Invert for UInt<U, B>
    where
        U: PrivateInvert<InvertedUInt<InvertedUTerm, B>>,
    {
        type Output = PrivateInvertOut<U, InvertedUInt<InvertedUTerm, B>>;

        #[inline]
        fn invert(self) -> Self::Output {
            self.msb.private_invert(InvertedUInt {
                msb: InvertedUTerm,
                lsb: self.lsb,
            })
        }
    }

    impl<IU: InvertedUnsigned> PrivateInvert<IU> for UTerm {
        type Output = IU;

        #[inline]
        fn private_invert(self, rhs: IU) -> Self::Output {
            rhs
        }
    }

    impl<IU: InvertedUnsigned, U: Unsigned, B: Bit> PrivateInvert<IU> for UInt<U, B>
    where
        U: PrivateInvert<InvertedUInt<IU, B>>,
    {
        type Output = PrivateInvertOut<U, InvertedUInt<IU, B>>;

        #[inline]
        fn private_invert(self, rhs: IU) -> Self::Output {
            self.msb.private_invert(InvertedUInt {
                msb: rhs,
                lsb: self.lsb,
            })
        }
    }

    #[test]
    fn test_inversion() {
        type Test4 = <::consts::U4 as Invert>::Output;
        type Test5 = <::consts::U5 as Invert>::Output;
        type Test12 = <::consts::U12 as Invert>::Output;
        type Test16 = <::consts::U16 as Invert>::Output;

        assert_eq!(1, <Test4 as InvertedUnsigned>::to_u64());
        assert_eq!(5, <Test5 as InvertedUnsigned>::to_u64());
        assert_eq!(3, <Test12 as InvertedUnsigned>::to_u64());
        assert_eq!(1, <Test16 as InvertedUnsigned>::to_u64());
    }

    impl Invert for InvertedUTerm {
        type Output = UTerm;

        #[inline]
        fn invert(self) -> Self::Output {
            UTerm
        }
    }

    impl<IU: InvertedUnsigned, B: Bit> Invert for InvertedUInt<IU, B>
    where
        IU: PrivateInvert<UInt<UTerm, B>>,
    {
        type Output = <IU as PrivateInvert<UInt<UTerm, B>>>::Output;

        #[inline]
        fn invert(self) -> Self::Output {
            self.msb.private_invert(UInt {
                msb: UTerm,
                lsb: self.lsb,
            })
        }
    }

    impl<U: Unsigned> PrivateInvert<U> for InvertedUTerm {
        type Output = U;

        #[inline]
        fn private_invert(self, rhs: U) -> Self::Output {
            rhs
        }
    }

    impl<U: Unsigned, IU: InvertedUnsigned, B: Bit> PrivateInvert<U> for InvertedUInt<IU, B>
    where
        IU: PrivateInvert<UInt<U, B>>,
    {
        type Output = <IU as PrivateInvert<UInt<U, B>>>::Output;

        #[inline]
        fn private_invert(self, rhs: U) -> Self::Output {
            self.msb.private_invert(UInt {
                msb: rhs,
                lsb: self.lsb,
            })
        }
    }

    #[test]
    fn test_double_inversion() {
        type Test4 = <<::consts::U4 as Invert>::Output as Invert>::Output;
        type Test5 = <<::consts::U5 as Invert>::Output as Invert>::Output;
        type Test12 = <<::consts::U12 as Invert>::Output as Invert>::Output;
        type Test16 = <<::consts::U16 as Invert>::Output as Invert>::Output;

        assert_eq!(4, <Test4 as Unsigned>::to_u64());
        assert_eq!(5, <Test5 as Unsigned>::to_u64());
        assert_eq!(12, <Test12 as Unsigned>::to_u64());
        assert_eq!(16, <Test16 as Unsigned>::to_u64());
    }

    impl TrimTrailingZeros for InvertedUTerm {
        type Output = InvertedUTerm;

        #[inline]
        fn trim_trailing_zeros(self) -> Self::Output {
            InvertedUTerm
        }
    }

    impl<IU: InvertedUnsigned> TrimTrailingZeros for InvertedUInt<IU, B1> {
        type Output = Self;

        #[inline]
        fn trim_trailing_zeros(self) -> Self::Output {
            self
        }
    }

    impl<IU: InvertedUnsigned> TrimTrailingZeros for InvertedUInt<IU, B0>
    where
        IU: TrimTrailingZeros,
    {
        type Output = <IU as TrimTrailingZeros>::Output;

        #[inline]
        fn trim_trailing_zeros(self) -> Self::Output {
            self.msb.trim_trailing_zeros()
        }
    }

    impl<U: Unsigned> Trim for U
    where
        U: Invert,
        <U as Invert>::Output: TrimTrailingZeros,
        <<U as Invert>::Output as TrimTrailingZeros>::Output: Invert,
    {
        type Output = <<<U as Invert>::Output as TrimTrailingZeros>::Output as Invert>::Output;

        #[inline]
        fn trim(self) -> Self::Output {
            self.invert().trim_trailing_zeros().invert()
        }
    }

    // Note: Trimming is tested when we do subtraction.

    pub trait PrivateCmp<Rhs, SoFar> {
        type Output;

        fn private_cmp(&self, &Rhs, SoFar) -> Self::Output;
    }
    pub type PrivateCmpOut<A, Rhs, SoFar> = <A as PrivateCmp<Rhs, SoFar>>::Output;

    // Set Bit
    pub trait PrivateSetBit<I, B> {
        type Output;

        fn private_set_bit(self, I, B) -> Self::Output;
    }
    pub type PrivateSetBitOut<N, I, B> = <N as PrivateSetBit<I, B>>::Output;

    // Div
    pub trait PrivateDiv<N, D, Q, R, I> {
        type Quotient;
        type Remainder;

        fn private_div_quotient(self, N, D, Q, R, I) -> Self::Quotient;

        fn private_div_remainder(self, N, D, Q, R, I) -> Self::Remainder;
    }

    pub type PrivateDivQuot<N, D, Q, R, I> = <() as PrivateDiv<N, D, Q, R, I>>::Quotient;
    pub type PrivateDivRem<N, D, Q, R, I> = <() as PrivateDiv<N, D, Q, R, I>>::Remainder;

    pub trait PrivateDivIf<N, D, Q, R, I, RcmpD> {
        type Quotient;
        type Remainder;

        fn private_div_if_quotient(self, N, D, Q, R, I, RcmpD) -> Self::Quotient;

        fn private_div_if_remainder(self, N, D, Q, R, I, RcmpD) -> Self::Remainder;
    }

    pub type PrivateDivIfQuot<N, D, Q, R, I, RcmpD> =
        <() as PrivateDivIf<N, D, Q, R, I, RcmpD>>::Quotient;
    pub type PrivateDivIfRem<N, D, Q, R, I, RcmpD> =
        <() as PrivateDivIf<N, D, Q, R, I, RcmpD>>::Remainder;

    // Div for signed ints
    pub trait PrivateDivInt<C, Divisor> {
        type Output;

        fn private_div_int(self, C, Divisor) -> Self::Output;
    }
    pub type PrivateDivIntOut<A, C, Divisor> = <A as PrivateDivInt<C, Divisor>>::Output;

    pub trait PrivateRem<URem, Divisor> {
        type Output;

        fn private_rem(self, URem, Divisor) -> Self::Output;
    }
    pub type PrivateRemOut<A, URem, Divisor> = <A as PrivateRem<URem, Divisor>>::Output;

    // min max
    pub trait PrivateMin<Rhs, CmpResult> {
        type Output;
        fn private_min(self, Rhs) -> Self::Output;
    }
    pub type PrivateMinOut<A, B, CmpResult> = <A as PrivateMin<B, CmpResult>>::Output;

    pub trait PrivateMax<Rhs, CmpResult> {
        type Output;
        fn private_max(self, Rhs) -> Self::Output;
    }
    pub type PrivateMaxOut<A, B, CmpResult> = <A as PrivateMax<B, CmpResult>>::Output;

    // Comparisons

    use {Equal, False, Greater, Less, True};

    pub trait IsLessPrivate<Rhs, Cmp> {
        type Output: Bit;

        fn is_less_private(self, Rhs, Cmp) -> Self::Output;
    }

    impl<A, B> IsLessPrivate<B, Less> for A {
        type Output = True;

        #[inline]
        fn is_less_private(self, _: B, _: Less) -> Self::Output {
            B1
        }
    }
    impl<A, B> IsLessPrivate<B, Equal> for A {
        type Output = False;

        #[inline]
        fn is_less_private(self, _: B, _: Equal) -> Self::Output {
            B0
        }
    }
    impl<A, B> IsLessPrivate<B, Greater> for A {
        type Output = False;

        #[inline]
        fn is_less_private(self, _: B, _: Greater) -> Self::Output {
            B0
        }
    }

    pub trait IsEqualPrivate<Rhs, Cmp> {
        type Output: Bit;

        fn is_equal_private(self, Rhs, Cmp) -> Self::Output;
    }

    impl<A, B> IsEqualPrivate<B, Less> for A {
        type Output = False;

        #[inline]
        fn is_equal_private(self, _: B, _: Less) -> Self::Output {
            B0
        }
    }
    impl<A, B> IsEqualPrivate<B, Equal> for A {
        type Output = True;

        #[inline]
        fn is_equal_private(self, _: B, _: Equal) -> Self::Output {
            B1
        }
    }
    impl<A, B> IsEqualPrivate<B, Greater> for A {
        type Output = False;

        #[inline]
        fn is_equal_private(self, _: B, _: Greater) -> Self::Output {
            B0
        }
    }

    pub trait IsGreaterPrivate<Rhs, Cmp> {
        type Output: Bit;

        fn is_greater_private(self, Rhs, Cmp) -> Self::Output;
    }

    impl<A, B> IsGreaterPrivate<B, Less> for A {
        type Output = False;

        #[inline]
        fn is_greater_private(self, _: B, _: Less) -> Self::Output {
            B0
        }
    }
    impl<A, B> IsGreaterPrivate<B, Equal> for A {
        type Output = False;

        #[inline]
        fn is_greater_private(self, _: B, _: Equal) -> Self::Output {
            B0
        }
    }
    impl<A, B> IsGreaterPrivate<B, Greater> for A {
        type Output = True;

        #[inline]
        fn is_greater_private(self, _: B, _: Greater) -> Self::Output {
            B1
        }
    }

    pub trait IsLessOrEqualPrivate<Rhs, Cmp> {
        type Output: Bit;

        fn is_less_or_equal_private(self, Rhs, Cmp) -> Self::Output;
    }

    impl<A, B> IsLessOrEqualPrivate<B, Less> for A {
        type Output = True;

        #[inline]
        fn is_less_or_equal_private(self, _: B, _: Less) -> Self::Output {
            B1
        }
    }
    impl<A, B> IsLessOrEqualPrivate<B, Equal> for A {
        type Output = True;

        #[inline]
        fn is_less_or_equal_private(self, _: B, _: Equal) -> Self::Output {
            B1
        }
    }
    impl<A, B> IsLessOrEqualPrivate<B, Greater> for A {
        type Output = False;

        #[inline]
        fn is_less_or_equal_private(self, _: B, _: Greater) -> Self::Output {
            B0
        }
    }

    pub trait IsNotEqualPrivate<Rhs, Cmp> {
        type Output: Bit;

        fn is_not_equal_private(self, Rhs, Cmp) -> Self::Output;
    }

    impl<A, B> IsNotEqualPrivate<B, Less> for A {
        type Output = True;

        #[inline]
        fn is_not_equal_private(self, _: B, _: Less) -> Self::Output {
            B1
        }
    }
    impl<A, B> IsNotEqualPrivate<B, Equal> for A {
        type Output = False;

        #[inline]
        fn is_not_equal_private(self, _: B, _: Equal) -> Self::Output {
            B0
        }
    }
    impl<A, B> IsNotEqualPrivate<B, Greater> for A {
        type Output = True;

        #[inline]
        fn is_not_equal_private(self, _: B, _: Greater) -> Self::Output {
            B1
        }
    }

    pub trait IsGreaterOrEqualPrivate<Rhs, Cmp> {
        type Output: Bit;

        fn is_greater_or_equal_private(self, Rhs, Cmp) -> Self::Output;
    }

    impl<A, B> IsGreaterOrEqualPrivate<B, Less> for A {
        type Output = False;

        #[inline]
        fn is_greater_or_equal_private(self, _: B, _: Less) -> Self::Output {
            B0
        }
    }
    impl<A, B> IsGreaterOrEqualPrivate<B, Equal> for A {
        type Output = True;

        #[inline]
        fn is_greater_or_equal_private(self, _: B, _: Equal) -> Self::Output {
            B1
        }
    }
    impl<A, B> IsGreaterOrEqualPrivate<B, Greater> for A {
        type Output = True;

        #[inline]
        fn is_greater_or_equal_private(self, _: B, _: Greater) -> Self::Output {
            B1
        }
    }

    pub trait PrivateSquareRoot {
        type Output;
    }

    pub trait PrivateLogarithm2 {
        type Output;
    }