]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/common/sim-n-bits.h
Sanity check for tic80 simulator.
[thirdparty/binutils-gdb.git] / sim / common / sim-n-bits.h
CommitLineData
b3e426bc
AC
1/* This file is part of the program psim.
2
3 Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
4 Copyright (C) 1997, Free Software Foundation, Inc.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 */
21
22
23#ifndef N
24#error "N must be #defined"
25#endif
26
7ec396d2
AC
27#include "sim-xcat.h"
28
74db699d 29#if defined(__STDC__) && defined(signed)
564e2a3f
MM
30/* If signed were defined to be say __signed (ie, some versions of Linux),
31 then the signedN macro would not work correctly. If we have a standard
32 compiler, we have signed. */
33#undef signed
34#endif
35
b3e426bc
AC
36/* NOTE: See end of file for #undef */
37#define unsignedN XCONCAT2(unsigned,N)
38#define signedN XCONCAT2(signed,N)
39#define MASKEDn XCONCAT2(MASKED,N)
40#define MASKn XCONCAT2(MASK,N)
41#define LSMASKEDn XCONCAT2(LSMASKED,N)
7ec396d2
AC
42#define LSMASKn XCONCAT2(LSMASK,N)
43#define MSMASKEDn XCONCAT2(MSMASKED,N)
44#define MSMASKn XCONCAT2(MSMASK,N)
30efae3a
AC
45#define LSEXTRACTEDn XCONCAT2(LSEXTRACTED,N)
46#define MSEXTRACTEDn XCONCAT2(MSEXTRACTED,N)
b3e426bc
AC
47#define INSERTEDn XCONCAT2(INSERTED,N)
48#define ROTn XCONCAT2(ROT,N)
49#define ROTLn XCONCAT2(ROTL,N)
50#define ROTRn XCONCAT2(ROTR,N)
51#define SEXTn XCONCAT2(SEXT,N)
52
74db699d 53/* TAGS: MASKED16 MASKED32 MASKED64 */
b3e426bc
AC
54
55INLINE_SIM_BITS\
56(unsignedN)
7ec396d2
AC
57MASKEDn (unsignedN word,
58 unsigned start,
59 unsigned stop)
b3e426bc 60{
7ec396d2 61 return (word & MASKn (start, stop));
b3e426bc
AC
62}
63
74db699d 64/* TAGS: LSMASKED16 LSMASKED32 LSMASKED64 */
b3e426bc
AC
65
66INLINE_SIM_BITS\
67(unsignedN)
7ec396d2
AC
68LSMASKEDn (unsignedN word,
69 unsigned nr_bits)
b3e426bc 70{
7ec396d2 71 return (word & LSMASKn (nr_bits));
b3e426bc
AC
72}
73
74db699d 74/* TAGS: MSMASKED16 MSMASKED32 MSMASKED64 */
b3e426bc
AC
75
76INLINE_SIM_BITS\
77(unsignedN)
7ec396d2
AC
78MSMASKEDn (unsignedN word,
79 unsigned nr_bits)
b3e426bc 80{
7ec396d2 81 return (word & MSMASKn (nr_bits));
b3e426bc
AC
82}
83
30efae3a 84/* TAGS: LSEXTRACTED16 LSEXTRACTED32 LSEXTRACTED64 */
b3e426bc
AC
85
86INLINE_SIM_BITS\
87(unsignedN)
30efae3a
AC
88LSEXTRACTEDn (unsignedN val,
89 unsigned start,
90 unsigned stop)
b3e426bc 91{
30efae3a
AC
92 val <<= (N - 1 - start); /* drop high bits */
93 val >>= (N - 1 - start) + (stop); /* drop low bits */
94 return val;
95}
96
97/* TAGS: MSEXTRACTED16 MSEXTRACTED32 MSEXTRACTED64 */
98
99INLINE_SIM_BITS\
100(unsignedN)
101MSEXTRACTEDn (unsignedN val,
102 unsigned start,
103 unsigned stop)
104{
105 val <<= (start); /* drop high bits */
106 val >>= (start) + (N - 1 - stop); /* drop low bits */
7ec396d2 107 return val;
b3e426bc
AC
108}
109
74db699d 110/* TAGS: INSERTED16 INSERTED32 INSERTED64 */
b3e426bc
AC
111
112INLINE_SIM_BITS\
113(unsignedN)
7ec396d2
AC
114INSERTEDn (unsignedN val,
115 unsigned start,
116 unsigned stop)
117{
118 val &= LSMASKn (_MAKE_WIDTH (start, stop));
119 val <<= _LSB_SHIFT (N, stop);
120 return val;
121}
122
74db699d 123/* TAGS: ROT16 ROT32 ROT64 */
7ec396d2
AC
124
125INLINE_SIM_BITS\
126(unsignedN)
127ROTn (unsignedN val,
128 int shift)
b3e426bc 129{
b3e426bc 130 if (shift > 0)
7ec396d2 131 return ROTRn (val, shift);
b3e426bc 132 else if (shift < 0)
7ec396d2 133 return ROTLn (val, -shift);
b3e426bc
AC
134 else
135 return val;
136}
137
74db699d 138/* TAGS: ROTL16 ROTL32 ROTL64 */
b3e426bc
AC
139
140INLINE_SIM_BITS\
141(unsignedN)
7ec396d2
AC
142ROTLn (unsignedN val,
143 unsigned shift)
b3e426bc
AC
144{
145 unsignedN result;
7ec396d2 146 ASSERT (shift <= N);
b3e426bc
AC
147 result = (((val) << (shift)) | ((val) >> ((N)-(shift))));
148 return result;
149}
150
74db699d 151/* TAGS: ROTR16 ROTR32 ROTR64 */
b3e426bc
AC
152
153INLINE_SIM_BITS\
154(unsignedN)
7ec396d2
AC
155ROTRn (unsignedN val,
156 unsigned shift)
b3e426bc
AC
157{
158 unsignedN result;
7ec396d2 159 ASSERT (shift <= N);
b3e426bc
AC
160 result = (((val) >> (shift)) | ((val) << ((N)-(shift))));
161 return result;
162}
163
74db699d 164/* TAGS: SEXT16 SEXT32 SEXT64 */
b3e426bc
AC
165
166INLINE_SIM_BITS\
167(unsignedN)
7ec396d2
AC
168SEXTn (signedN val,
169 unsigned sign_bit)
b3e426bc
AC
170{
171 /* make the sign-bit most significant and then smear it back into
172 position */
7ec396d2
AC
173 ASSERT (sign_bit < N);
174 val <<= _MSB_SHIFT (N, sign_bit);
175 val >>= _MSB_SHIFT (N, sign_bit);
176 return val;
b3e426bc
AC
177}
178
179
180/* NOTE: See start of file for #define */
181#undef SEXTn
182#undef ROTLn
183#undef ROTRn
184#undef ROTn
185#undef INSERTEDn
30efae3a
AC
186#undef LSEXTRACTEDn
187#undef MSEXTRACTEDn
b3e426bc 188#undef LSMASKEDn
7ec396d2
AC
189#undef LSMASKn
190#undef MSMASKEDn
191#undef MSMASKn
b3e426bc
AC
192#undef MASKn
193#undef MASKEDn
194#undef signedN
195#undef unsignedN