Skip to main content

fixed_bigint/
machineword.rs

1// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Note: in the future, #![feature(const_trait_impl)] should allow
16// turning this into a const trait
17
18pub use crate::const_numtraits::ConstPrimInt;
19use crate::const_numtraits::{
20    ConstOverflowingAdd, ConstOverflowingSub, ConstToBytes, ConstWideningMul,
21};
22
23c0nst::c0nst! {
24    /// A const-friendly trait for MachineWord operations.
25    /// Extends ConstWideningMul to provide widening multiplication.
26    pub c0nst trait ConstMachineWord:
27        [c0nst] ConstPrimInt +
28        [c0nst] ConstOverflowingAdd +
29        [c0nst] ConstOverflowingSub +
30        [c0nst] ConstToBytes +
31        [c0nst] ConstWideningMul
32    {
33        type ConstDoubleWord: [c0nst] ConstPrimInt;
34        fn to_double(self) -> Self::ConstDoubleWord;
35        fn from_double(word: Self::ConstDoubleWord) -> Self;
36    }
37
38    impl c0nst ConstMachineWord for u8 {
39        type ConstDoubleWord = u16;
40        fn to_double(self) -> u16 { self as u16 }
41        fn from_double(word: u16) -> u8 { word as u8 }
42    }
43    impl c0nst ConstMachineWord for u16 {
44        type ConstDoubleWord = u32;
45        fn to_double(self) -> u32 { self as u32 }
46        fn from_double(word: u32) -> u16 { word as u16 }
47    }
48    impl c0nst ConstMachineWord for u32 {
49        type ConstDoubleWord = u64;
50        fn to_double(self) -> u64 { self as u64 }
51        fn from_double(word: u64) -> u32 { word as u32 }
52    }
53    impl c0nst ConstMachineWord for u64 {
54        type ConstDoubleWord = u128;
55        fn to_double(self) -> u128 { self as u128 }
56        fn from_double(word: u128) -> u64 { word as u64 }
57    }
58}
59
60/// Represents a CPU native word, from 8-bit to 64-bit, with corresponding
61/// double-word to hold multiplication/division products.
62///
63/// This trait is intentionally sealed via the `ConstMachineWord` supertrait,
64/// as custom implementations are not supported.
65pub trait MachineWord:
66    ConstMachineWord<ConstDoubleWord = Self::DoubleWord> + core::hash::Hash + num_traits::ToPrimitive
67{
68    type DoubleWord: ConstPrimInt;
69}
70
71impl MachineWord for u8 {
72    type DoubleWord = u16;
73}
74impl MachineWord for u16 {
75    type DoubleWord = u32;
76}
77impl MachineWord for u32 {
78    type DoubleWord = u64;
79}
80impl MachineWord for u64 {
81    type DoubleWord = u128;
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    c0nst::c0nst! {
89        pub c0nst fn to_double<T: [c0nst] ConstMachineWord>(a: T) -> T::ConstDoubleWord {
90            a.to_double()
91        }
92    }
93
94    #[test]
95    fn test_constmachineword_ops() {
96        assert_eq!(to_double(200u8), 200u16);
97
98        #[cfg(feature = "nightly")]
99        {
100            const DOUBLE_RES: u16 = to_double(200u8);
101            assert_eq!(DOUBLE_RES, 200u16);
102        }
103    }
104}