base: Add support for merging of interleaved address ranges
[gem5.git] / src / base / mod_num.hh
1 /*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Steve Raasch
29 */
30
31 template<class T, T MV>
32 class ModNum {
33 private:
34 T value;
35
36 // Compiler should optimize this
37 void setValue(T n) { value = n % MV; }
38
39 public:
40 ModNum() {}
41 ModNum(T n) { setValue(n); }
42 ModNum(const ModNum<T, MV> &n) : value(n.value) {}
43
44 ModNum operator=(T n) {
45 setValue(n);
46 return *this;
47 }
48
49 const ModNum operator=(ModNum n) {
50 value = n.value;
51 return *this;
52 }
53
54 // Return the value if object used as RHS
55 operator T() const { return value; }
56
57 //
58 // Operator "+="
59 //
60 const ModNum<T, MV> operator+=(ModNum<T, MV> r) {
61 setValue(value + r.value);
62 return *this;
63 }
64
65 const ModNum<T, MV> operator+=(T r) {
66 setValue(value + r);
67 return *this;
68 }
69
70 //
71 // Operator "-="
72 //
73 const ModNum<T, MV> operator-=(ModNum<T, MV> r) {
74 setValue(value - r.value);
75 return *this;
76 }
77
78 const ModNum<T, MV> operator-=(T r) {
79 setValue(value - r);
80 return *this;
81 }
82
83 //
84 // Operator "++"
85 //
86 // PREFIX (like ++a)
87 const ModNum<T, MV> operator++() {
88 *this += 1;
89 return *this;
90 }
91
92 // POSTFIX (like a++)
93 const ModNum<T, MV> operator++(int) {
94 ModNum<T, MV> rv = *this;
95
96 *this += 1;
97
98 return rv;
99 }
100
101 //
102 // Operator "--"
103 //
104 // PREFIX (like --a)
105 const ModNum<T, MV> operator--() {
106 *this -= 1;
107 return *this;
108 }
109
110 // POSTFIX (like a--)
111 const ModNum<T, MV> operator--(int) {
112 ModNum<T, MV> rv = *this;
113 *this -= 1;
114 return rv;
115 }
116 };
117
118
119 //
120 // Define operator "+" like this to avoid creating a temporary
121 //
122 template<class T, T MV>
123 inline ModNum<T, MV>
124 operator+(ModNum<T, MV> l, ModNum<T, MV> r) {
125 l += r;
126 return l;
127 }
128
129 template<class T, T MV>
130 inline ModNum<T, MV>
131 operator+(ModNum<T, MV> l, T r) {
132 l += r;
133 return l;
134 }
135
136 template<class T, T MV>
137 inline ModNum<T, MV>
138 operator+(T l, ModNum<T, MV> r) {
139 r += l;
140 return r;
141 }
142
143
144 //
145 // Define operator "-" like this to avoid creating a temporary
146 //
147 template<class T, T MV>
148 inline ModNum<T, MV>
149 operator-(ModNum<T, MV> l, ModNum<T, MV> r) {
150 l -= r;
151 return l;
152 }
153
154 template<class T, T MV>
155 inline ModNum<T, MV>
156 operator-(ModNum<T, MV> l, T r) {
157 l -= r;
158 return l;
159 }
160
161 template<class T, T MV>
162 inline ModNum<T, MV>
163 operator-(T l, ModNum<T, MV> r) {
164 r -= l;
165 return r;
166 }
167
168
169 //
170 // Comparison operators
171 // (all other cases are handled with conversons)
172 //
173 template<class T, T MV>
174 inline bool
175 operator<(ModNum<T, MV> l, ModNum<T, MV> r) {
176 return l.value < r.value;
177 }
178
179 template<class T, T MV>
180 inline bool
181 operator>(ModNum<T, MV> l, ModNum<T, MV> r) {
182 return l.value > r.value;
183 }
184
185 template<class T, T MV>
186 inline bool
187 operator==(ModNum<T, MV> l, ModNum<T, MV> r) {
188 return l.value == r.value;
189 }
190
191 template<class T, T MV>
192 inline bool
193 operator<=(ModNum<T, MV> l, ModNum<T, MV> r) {
194 return l.value <= r.value;
195 }
196
197 template<class T, T MV>
198 inline bool
199 operator>=(ModNum<T, MV> l, ModNum<T, MV> r) {
200 return l.value >= r.value;
201 }
202
203