blob: 64deea068254ec4adf9505004a97367aebfa4cef [file] [log] [blame]
Austin Schuhbb1338c2024-06-15 19:31:16 -07001/* asl.h -- artificially small limbs support by means of C++ operator
2 overloading.
3
4Copyright 2016 Free Software Foundation, Inc.
5
6This file is part of the GNU MP Library.
7
8The GNU MP Library is free software; you can redistribute it and/or modify
9it under the terms of either:
10
11 * the GNU Lesser General Public License as published by the Free
12 Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
14
15or
16
17 * the GNU General Public License as published by the Free Software
18 Foundation; either version 2 of the License, or (at your option) any
19 later version.
20
21or both in parallel, as here.
22
23The GNU MP Library is distributed in the hope that it will be useful, but
24WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26for more details.
27
28You should have received copies of the GNU General Public License and the
29GNU Lesser General Public License along with the GNU MP Library. If not,
30see https://www.gnu.org/licenses/. */
31
32#include <iostream>
33#include <cstdint>
34#include <cstdlib>
35// #include <stdexcept>
36
37#ifndef GMP_ASSERT_ALWAYS
38#define GMP_ASSERT_ALWAYS(cc) do {if (!(cc)) abort();} while (0)
39#endif
40
41// Missing: post++ post-- ++pre --prec bool(limb) !limb
42
43#ifndef GMP_LIMB_BITS
44#define GMP_LIMB_BITS 4
45#endif
46
47#define GMP_NUMB_MASK (2 * (1ul << (GMP_LIMB_BITS - 1)) - 1)
48
49#define BINOP_MASK(op, type) \
50 mp_limb_t& operator op##=(const type& rhs) { \
51 limbo = (limbo op rhs.limbo) & GMP_NUMB_MASK; \
52 return *this; \
53 }
54#define BINOP_NOMASK(op, type) \
55 mp_limb_t& operator op##=(const type& rhs) { \
56 limbo = limbo op rhs.limbo; \
57 return *this; \
58 }
59
60typedef std::conditional<(GMP_NUMB_MASK <= 0xffff), uint16_t, uint32_t >::type type24;
61typedef std::conditional<(GMP_NUMB_MASK <= 0xff), uint8_t, type24>::type mtype;
62
63class mp_limb_t {
64public:
65 mp_limb_t() {} // put random garbage in limbo?
66 mp_limb_t(const unsigned int rhs) { limbo = rhs & GMP_NUMB_MASK; }
67 // mp_limb_t(const mp_limb_t& rhs) { limbo = rhs.limbo; } // Causes havoc
68 BINOP_MASK(+, mp_limb_t)
69 BINOP_MASK(-, mp_limb_t)
70 BINOP_MASK(*, mp_limb_t)
71 BINOP_NOMASK(/, mp_limb_t)
72 BINOP_NOMASK(%, mp_limb_t)
73 BINOP_NOMASK(&, mp_limb_t)
74 BINOP_NOMASK(|, mp_limb_t)
75 BINOP_NOMASK(^, mp_limb_t)
76 mp_limb_t& operator<<=(const unsigned int rhs) {
77 GMP_ASSERT_ALWAYS (rhs < GMP_LIMB_BITS);
78 limbo = (limbo << rhs) & GMP_NUMB_MASK;
79 return *this;
80 }
81 mp_limb_t& operator>>=(const unsigned int rhs) {
82 GMP_ASSERT_ALWAYS (rhs < GMP_LIMB_BITS);
83 limbo = limbo >> rhs;
84 return *this;
85 }
86 mp_limb_t operator-() {
87 return static_cast<mp_limb_t>((-limbo) & GMP_NUMB_MASK);
88 // mp_limb_t x; x.limbo = (-limbo) & GMP_NUMB_MASK; return x;
89 }
90 mp_limb_t operator~() {
91 return static_cast<mp_limb_t>((~limbo) & GMP_NUMB_MASK);
92 // mp_limb_t x; x.limbo = (~limbo) & GMP_NUMB_MASK; return x;
93 }
94 operator unsigned int() const { return limbo; }
95 operator int() const { return limbo; }
96
97#define RELOP(op) \
98 inline bool operator op(const mp_limb_t rhs) { \
99 return limbo op rhs.limbo; \
100 }
101 RELOP(==)
102 RELOP(!=)
103 RELOP(<)
104 RELOP(>)
105 RELOP(<=)
106 RELOP(>=)
107
108private:
109 mtype limbo;
110};
111
112#define BINOP2(op, type) \
113 inline mp_limb_t operator op(mp_limb_t lhs, const type& rhs) { \
114 lhs op##= rhs; \
115 return lhs; \
116 }
117
118BINOP2(+, mp_limb_t)
119BINOP2(-, mp_limb_t)
120BINOP2(*, mp_limb_t)
121BINOP2(/, mp_limb_t)
122BINOP2(%, mp_limb_t)
123BINOP2(&, mp_limb_t)
124BINOP2(|, mp_limb_t)
125BINOP2(^, mp_limb_t)
126BINOP2(<<, unsigned int)
127BINOP2(>>, unsigned int)