xref: /netbsd-src/external/lgpl3/gmp/dist/asl.h (revision 72c7faa4dbb41dbb0238d6b4a109da0d4b236dd4)
1 /* asl.h -- artificially small limbs support by means of C++ operator
2    overloading.
3 
4 Copyright 2016 Free Software Foundation, Inc.
5 
6 This file is part of the GNU MP Library.
7 
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it 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 
15 or
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 
21 or both in parallel, as here.
22 
23 The GNU MP Library is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26 for more details.
27 
28 You should have received copies of the GNU General Public License and the
29 GNU Lesser General Public License along with the GNU MP Library.  If not,
30 see 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 
60 typedef std::conditional<(GMP_NUMB_MASK <= 0xffff), uint16_t, uint32_t >::type type24;
61 typedef std::conditional<(GMP_NUMB_MASK <= 0xff), uint8_t, type24>::type mtype;
62 
63 class mp_limb_t {
64 public:
mp_limb_t()65   mp_limb_t() {}	// put random garbage in limbo?
mp_limb_t(const unsigned int rhs)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 
108 private:
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 
118 BINOP2(+, mp_limb_t)
119 BINOP2(-, mp_limb_t)
120 BINOP2(*, mp_limb_t)
121 BINOP2(/, mp_limb_t)
122 BINOP2(%, mp_limb_t)
123 BINOP2(&, mp_limb_t)
124 BINOP2(|, mp_limb_t)
125 BINOP2(^, mp_limb_t)
126 BINOP2(<<, unsigned int)
127 BINOP2(>>, unsigned int)
128