summaryrefslogtreecommitdiff
path: root/Build/source/libs/gmp/gmp-src/asl.h
blob: 64deea068254ec4adf9505004a97367aebfa4cef (plain)
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
/* asl.h -- artificially small limbs support by means of C++ operator
   overloading.

Copyright 2016 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of either:

  * the GNU Lesser General Public License as published by the Free
    Software Foundation; either version 3 of the License, or (at your
    option) any later version.

or

  * the GNU General Public License as published by the Free Software
    Foundation; either version 2 of the License, or (at your option) any
    later version.

or both in parallel, as here.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the GNU MP Library.  If not,
see https://www.gnu.org/licenses/.  */

#include <iostream>
#include <cstdint>
#include <cstdlib>
// #include <stdexcept>

#ifndef GMP_ASSERT_ALWAYS
#define GMP_ASSERT_ALWAYS(cc) do {if (!(cc)) abort();} while (0)
#endif

// Missing: post++ post-- ++pre --prec bool(limb) !limb

#ifndef GMP_LIMB_BITS
#define GMP_LIMB_BITS 4
#endif

#define GMP_NUMB_MASK (2 * (1ul << (GMP_LIMB_BITS - 1)) - 1)

#define BINOP_MASK(op, type)				\
  mp_limb_t& operator op##=(const type& rhs) {		\
    limbo = (limbo op rhs.limbo) & GMP_NUMB_MASK;	\
    return *this;					\
  }
#define BINOP_NOMASK(op, type)				\
  mp_limb_t& operator op##=(const type& rhs) {		\
    limbo = limbo op rhs.limbo;				\
    return *this;					\
  }

typedef std::conditional<(GMP_NUMB_MASK <= 0xffff), uint16_t, uint32_t >::type type24;
typedef std::conditional<(GMP_NUMB_MASK <= 0xff), uint8_t, type24>::type mtype;

class mp_limb_t {
public:
  mp_limb_t() {}	// put random garbage in limbo?
  mp_limb_t(const unsigned int rhs) { limbo = rhs & GMP_NUMB_MASK; }
  // mp_limb_t(const mp_limb_t& rhs) { limbo = rhs.limbo; } // Causes havoc
  BINOP_MASK(+, mp_limb_t)
  BINOP_MASK(-, mp_limb_t)
  BINOP_MASK(*, mp_limb_t)
  BINOP_NOMASK(/, mp_limb_t)
  BINOP_NOMASK(%, mp_limb_t)
  BINOP_NOMASK(&, mp_limb_t)
  BINOP_NOMASK(|, mp_limb_t)
  BINOP_NOMASK(^, mp_limb_t)
  mp_limb_t& operator<<=(const unsigned int rhs) {
    GMP_ASSERT_ALWAYS (rhs < GMP_LIMB_BITS);
    limbo = (limbo << rhs) & GMP_NUMB_MASK;
    return *this;
  }
  mp_limb_t& operator>>=(const unsigned int rhs) {
    GMP_ASSERT_ALWAYS (rhs < GMP_LIMB_BITS);
    limbo = limbo >> rhs;
    return *this;
  }
  mp_limb_t operator-() {
    return static_cast<mp_limb_t>((-limbo) & GMP_NUMB_MASK);
    // mp_limb_t x;  x.limbo = (-limbo) & GMP_NUMB_MASK;  return x;
  }
  mp_limb_t operator~() {
    return static_cast<mp_limb_t>((~limbo) & GMP_NUMB_MASK);
    // mp_limb_t x;  x.limbo = (~limbo) & GMP_NUMB_MASK;  return x;
  }
  operator unsigned int() const { return limbo; }
  operator          int() const { return limbo; }

#define RELOP(op)							\
  inline bool operator op(const mp_limb_t rhs) {			\
    return limbo op rhs.limbo;						\
  }
  RELOP(==)
  RELOP(!=)
  RELOP(<)
  RELOP(>)
  RELOP(<=)
  RELOP(>=)

private:
  mtype limbo;
};

#define BINOP2(op, type)						\
  inline mp_limb_t operator op(mp_limb_t lhs, const type& rhs) {	\
    lhs op##= rhs;							\
    return lhs;								\
  }

BINOP2(+, mp_limb_t)
BINOP2(-, mp_limb_t)
BINOP2(*, mp_limb_t)
BINOP2(/, mp_limb_t)
BINOP2(%, mp_limb_t)
BINOP2(&, mp_limb_t)
BINOP2(|, mp_limb_t)
BINOP2(^, mp_limb_t)
BINOP2(<<, unsigned int)
BINOP2(>>, unsigned int)