aboutsummaryrefslogtreecommitdiff
path: root/sim/common/sim-alu.h
blob: 46306c9eb747b8f64fe82415a6d7b72f0c798f11 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*  This file is part of the program psim.

    Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
    Copyright (C) 1997, Free Software Foundation, Inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of 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.

    This program 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 a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
    */


#ifndef _SIM_ALU_H_
#define _SIM_ALU_H_

#include "sim-xcat.h"

/* 32bit target expressions:

   Each calculation is performed three times using each of the
   signed64, unsigned64 and long integer types.  The macro ALU_END
   (in _ALU_RESULT_VAL) then selects which of the three alternative
   results will be used in the final assignment of the target
   register.  As this selection is determined at compile time by
   fields in the instruction (OE, EA, Rc) the compiler has sufficient
   information to firstly simplify the selection code into a single
   case and then back anotate the equations and hence eliminate any
   resulting dead code.  That dead code being the calculations that,
   as it turned out were not in the end needed.

   64bit arrithemetic is used firstly because it allows the use of
   gcc's efficient long long operators (typically efficiently output
   inline) and secondly because the resultant answer will contain in
   the low 32bits the answer while in the high 32bits is either carry
   or status information. */



/* 16bit target expressions:

   These are a simplified version of the 32bit target expressions */



/* 64bit target expressions:

   Unfortunatly 128bit arrithemetic isn't that common.  Consequently
   the 32/64 bit trick can not be used.  Instead all calculations are
   required to retain carry/overflow information in separate
   variables.  Even with this restriction it is still possible for the
   trick of letting the compiler discard the calculation of unneeded
   values */


/* Start a section of ALU code */

#define ALU16_BEGIN(VAL) \
{ \
  signed_word alu_carry_val; \
  unsigned_word alu_overflow_val; \
  ALU16_SET(VAL)

#define ALU32_BEGIN(VAL) \
{ \
  natural_word alu_val; \
  unsigned64 alu_carry_val; \
  signed64 alu_overflow_val; \
  ALU32_SET(VAL)

#define ALU64_BEGIN(VAL) \
{ \
  natural64 alu_val; \
  unsigned64 alu_carry_val; \
  signed64 alu_overflow_val; \
  ALU64_SET(VAL)


#define ALU_BEGIN(VAL) XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_BEGIN)(VAL)

/* More basic alu operations */



#define ALU16_SET(VAL) \
do { \
  alu_carry_val = (unsigned16)(VAL); \
  alu_overflow_val = (signed16)(VAL); \
} while (0)

#define ALU32_SET(VAL) \
do { \
  alu_val = (unsigned32)(VAL); \
  alu_carry_val = (unsigned32)(alu_val); \
  alu_overflow_val = (signed32)(alu_val); \
} while (0)

#define ALU64_SET(VAL) \
do { \
  alu_val = (VAL); \
  alu_carry_val = ((unsigned64)alu_val) >> 32; \
  alu_overflow_val = ((signed64)alu_val) >> 32; \
} while (0)

#define ALU_SET(VAL) XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_SET)(VAL)




#define ALU16_ADD(VAL) \
do { \
  alu_carry_val += (unsigned16)(VAL); \
  alu_overflow_val += (signed16)(VAL); \
} while (0)

#define ALU32_ADD(VAL) \
do { \
  alu_val += (VAL); \
  alu_carry_val += (unsigned32)(VAL); \
  alu_overflow_val += (signed32)(VAL); \
} while (0)

#define ALU64_ADD(VAL) \
do { \
  unsigned64 val = (VAL); \
  unsigned64 alu_lo = alu_val + val); \
  signed alu_carry = ((alu_lo & LSBIT64 (31)) != 0); \
  alu_carry_val = (alu_carry_val \
		   + MSEXTRACTED64 (val, 0, 31) \
		   + alu_carry); \
  alu_overflow_val = (alu_overflow_val \
		      + MSEXTRACTED64 (val, 0, 31) \
		      + alu_carry); \
  alu_val = alu_val + val; \
} while (0)

#define ALU_ADD(VAL) XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_ADD)(VAL)




#define ALU16_ADD_CA \
do { \
  signed carry = ALU_CARRY; \
  ALU16_ADD(carry); \
} while (0)

#define ALU32_ADD_CA \
do { \
  signed carry = ALU_CARRY; \
  ALU32_ADD(carry); \
} while (0)

#define ALU64_ADD_CA \
do { \
  signed carry = ALU_CARRY; \
  ALU64_ADD(carry); \
} while (0)

#define ALU_ADD_CA XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_ADD_CA)




#define ALU16_SUB(VAL) \
do { \
  alu_carry_val -= (unsigned16)(VAL); \
  alu_overflow_val -= (signed16)(VAL); \
} while (0)

#define ALU32_SUB(VAL) \
do { \
  alu_val -= (VAL); \
  alu_carry_val -= (unsigned32)(VAL); \
  alu_overflow_val -= (signed32)(VAL); \
} while (0)

#define ALU64_SUB(VAL) \
do { \
  error("ALU_SUB64"); \
} while (0)

#define ALU_SUB(VAL) XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_SUB)(VAL)




#define ALU16_SUB_CA \
do { \
  signed carry = ALU_CARRY; \
  ALU16_SUB(carry); \
} while (0)

#define ALU32_SUB_CA \
do { \
  signed carry = ALU_CARRY; \
  ALU32_SUB(carry); \
} while (0)

#define ALU64_SUB_CA \
do { \
  signed carry = ALU_CARRY; \
  ALU64_SUB(carry); \
} while (0)

#define ALU_SUB_CA XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_SUB_CA)




#define ALU16_OR(VAL) \
do { \
  error("ALU16_OR"); \
} while (0)

#define ALU32_OR(VAL) \
do { \
  alu_val |= (VAL); \
  alu_carry_val = (unsigned32)(alu_val); \
  alu_overflow_val = (signed32)(alu_val); \
} while (0)

#define ALU64_OR(VAL) \
do { \
  error("ALU_OR64"); \
} while (0)

#define ALU_OR(VAL) XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_OR)(VAL)




#define ALU16_XOR(VAL) \
do { \
  error("ALU16_XOR"); \
} while (0)

#define ALU32_XOR(VAL) \
do { \
  alu_val ^= (VAL); \
  alu_carry_val = (unsigned32)(alu_val); \
  alu_overflow_val = (signed32)(alu_val); \
} while (0)

#define ALU64_XOR(VAL) \
do { \
  error("ALU_XOR64"); \
} while (0)

#define ALU_XOR(VAL) XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_XOR)(VAL)




#define ALU16_NEGATE \
do { \
  error("ALU_NEGATE16"); \
} while (0)

#define ALU32_NEGATE \
do { \
  alu_val = -alu_val; \
  alu_carry_val = -alu_carry_val; \
  alu_overflow_val = -alu_overflow_val; \
} while(0)

#define ALU64_NEGATE \
do { \
  error("ALU_NEGATE64"); \
} while (0)

#define ALU_NEGATE XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_NEGATE)




#define ALU16_AND(VAL) \
do { \
  error("ALU_AND16"); \
} while (0)

#define ALU32_AND(VAL) \
do { \
  alu_val &= (VAL); \
  alu_carry_val = (unsigned32)(alu_val); \
  alu_overflow_val = (signed32)(alu_val); \
} while (0)

#define ALU64_AND(VAL) \
do { \
  error("ALU_AND64"); \
} while (0)

#define ALU_AND(VAL) XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_AND)(VAL)




#define ALU16_NOT(VAL) \
do { \
  error("ALU_NOT16"); \
} while (0)

#define ALU32_NOT \
do { \
  signed64 new_alu_val = ~alu_val; \
  ALU_SET(new_alu_val); \
} while (0)

#define ALU64_NOT \
do { \
  error("ALU_NOT64"); \
} while (0)

#define ALU_NOT XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_NOT)



/* Make available various results */


/* overflow occures if the sign bit differs from the carry bit */

#define ALU16_HAD_OVERFLOW \
  (!(alu_overflow_val & MSBIT32 (0)) != !(alu_overflow_val & MSBIT32 (16)))
  
#define ALU32_HAD_OVERFLOW \
  ((((unsigned64)(alu_overflow_val & BIT64(0))) >> 32) \
   != (alu_overflow_val & MSBIT64(32)))

#define ALU_HAD_OVERFLOW XCONCAT3(ALU,WITH_TARGET_WORD_BITSIZE,_HAD_OVERFLOW)

/* carry found in bit before sign */

#define ALU16_HAD_CARRY \
    (alu_carry_val & MSBIT32(16))

#define ALU32_HAD_CARRY \
    (alu_carry_val & MSBIT64(31))


#endif