xref: /netbsd-src/external/gpl3/gdb/dist/sim/common/sim-n-bits.h (revision 88241920d21b339bf319c0e979ffda80c49a2936)
14e98e3e1Schristos /* The common simulator framework for GDB, the GNU Debugger.
24e98e3e1Schristos 
3*88241920Schristos    Copyright 2002-2024 Free Software Foundation, Inc.
44e98e3e1Schristos 
54e98e3e1Schristos    Contributed by Andrew Cagney and Red Hat.
64e98e3e1Schristos 
74e98e3e1Schristos    This file is part of GDB.
84e98e3e1Schristos 
94e98e3e1Schristos    This program is free software; you can redistribute it and/or modify
104e98e3e1Schristos    it under the terms of the GNU General Public License as published by
114e98e3e1Schristos    the Free Software Foundation; either version 3 of the License, or
124e98e3e1Schristos    (at your option) any later version.
134e98e3e1Schristos 
144e98e3e1Schristos    This program is distributed in the hope that it will be useful,
154e98e3e1Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
164e98e3e1Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
174e98e3e1Schristos    GNU General Public License for more details.
184e98e3e1Schristos 
194e98e3e1Schristos    You should have received a copy of the GNU General Public License
204e98e3e1Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
214e98e3e1Schristos 
224e98e3e1Schristos 
234e98e3e1Schristos #ifndef N
244e98e3e1Schristos #error "N must be #defined"
254e98e3e1Schristos #endif
264e98e3e1Schristos 
274e98e3e1Schristos #include "symcat.h"
284e98e3e1Schristos 
294e98e3e1Schristos #if defined(__STDC__) && defined(signed)
304e98e3e1Schristos /* If signed were defined to be say __signed (ie, some versions of Linux),
314e98e3e1Schristos    then the signedN macro would not work correctly.  If we have a standard
324e98e3e1Schristos    compiler, we have signed.  */
334e98e3e1Schristos #undef signed
344e98e3e1Schristos #endif
354e98e3e1Schristos 
364e98e3e1Schristos /* NOTE: See end of file for #undef */
374b169a6bSchristos #define unsignedN XCONCAT3(uint,N,_t)
384b169a6bSchristos #define signedN XCONCAT3(int,N,_t)
394e98e3e1Schristos #define LSMASKn XCONCAT2(LSMASK,N)
404e98e3e1Schristos #define MSMASKn XCONCAT2(MSMASK,N)
414e98e3e1Schristos #define LSMASKEDn XCONCAT2(LSMASKED,N)
424e98e3e1Schristos #define MSMASKEDn XCONCAT2(MSMASKED,N)
434e98e3e1Schristos #define LSEXTRACTEDn XCONCAT2(LSEXTRACTED,N)
444e98e3e1Schristos #define MSEXTRACTEDn XCONCAT2(MSEXTRACTED,N)
454e98e3e1Schristos #define LSINSERTEDn XCONCAT2(LSINSERTED,N)
464e98e3e1Schristos #define MSINSERTEDn XCONCAT2(MSINSERTED,N)
474e98e3e1Schristos #define ROTn XCONCAT2(ROT,N)
484e98e3e1Schristos #define ROTLn XCONCAT2(ROTL,N)
494e98e3e1Schristos #define ROTRn XCONCAT2(ROTR,N)
504e98e3e1Schristos #define MSSEXTn XCONCAT2(MSSEXT,N)
514e98e3e1Schristos #define LSSEXTn XCONCAT2(LSSEXT,N)
524e98e3e1Schristos 
534e98e3e1Schristos /* TAGS: LSMASKED16 LSMASKED32 LSMASKED64 */
544e98e3e1Schristos 
554e98e3e1Schristos INLINE_SIM_BITS\
564e98e3e1Schristos (unsignedN)
574e98e3e1Schristos LSMASKEDn (unsignedN word,
584e98e3e1Schristos 	   int start,
594e98e3e1Schristos 	   int stop)
604e98e3e1Schristos {
614e98e3e1Schristos   word &= LSMASKn (start, stop);
624e98e3e1Schristos   return word;
634e98e3e1Schristos }
644e98e3e1Schristos 
654e98e3e1Schristos /* TAGS: MSMASKED16 MSMASKED32 MSMASKED64 */
664e98e3e1Schristos 
674e98e3e1Schristos INLINE_SIM_BITS\
684e98e3e1Schristos (unsignedN)
694e98e3e1Schristos MSMASKEDn (unsignedN word,
704e98e3e1Schristos 	   int start,
714e98e3e1Schristos 	   int stop)
724e98e3e1Schristos {
734e98e3e1Schristos   word &= MSMASKn (start, stop);
744e98e3e1Schristos   return word;
754e98e3e1Schristos }
764e98e3e1Schristos 
774e98e3e1Schristos /* TAGS: LSEXTRACTED16 LSEXTRACTED32 LSEXTRACTED64 */
784e98e3e1Schristos 
794e98e3e1Schristos INLINE_SIM_BITS\
804e98e3e1Schristos (unsignedN)
814e98e3e1Schristos LSEXTRACTEDn (unsignedN val,
824e98e3e1Schristos 	      int start,
834e98e3e1Schristos 	      int stop)
844e98e3e1Schristos {
854e98e3e1Schristos   val <<= (N - 1 - start); /* drop high bits */
864e98e3e1Schristos   val >>= (N - 1 - start) + (stop); /* drop low bits */
874e98e3e1Schristos   return val;
884e98e3e1Schristos }
894e98e3e1Schristos 
904e98e3e1Schristos /* TAGS: MSEXTRACTED16 MSEXTRACTED32 MSEXTRACTED64 */
914e98e3e1Schristos 
924e98e3e1Schristos INLINE_SIM_BITS\
934e98e3e1Schristos (unsignedN)
944e98e3e1Schristos MSEXTRACTEDn (unsignedN val,
954e98e3e1Schristos 	      int start,
964e98e3e1Schristos 	      int stop)
974e98e3e1Schristos {
984e98e3e1Schristos   val <<= (start); /* drop high bits */
994e98e3e1Schristos   val >>= (start) + (N - 1 - stop); /* drop low bits */
1004e98e3e1Schristos   return val;
1014e98e3e1Schristos }
1024e98e3e1Schristos 
1034e98e3e1Schristos /* TAGS: LSINSERTED16 LSINSERTED32 LSINSERTED64 */
1044e98e3e1Schristos 
1054e98e3e1Schristos INLINE_SIM_BITS\
1064e98e3e1Schristos (unsignedN)
1074e98e3e1Schristos LSINSERTEDn (unsignedN val,
1084e98e3e1Schristos 	     int start,
1094e98e3e1Schristos 	     int stop)
1104e98e3e1Schristos {
1114e98e3e1Schristos   val <<= stop;
1124e98e3e1Schristos   val &= LSMASKn (start, stop);
1134e98e3e1Schristos   return val;
1144e98e3e1Schristos }
1154e98e3e1Schristos 
1164e98e3e1Schristos /* TAGS: MSINSERTED16 MSINSERTED32 MSINSERTED64 */
1174e98e3e1Schristos 
1184e98e3e1Schristos INLINE_SIM_BITS\
1194e98e3e1Schristos (unsignedN)
1204e98e3e1Schristos MSINSERTEDn (unsignedN val,
1214e98e3e1Schristos 	     int start,
1224e98e3e1Schristos 	     int stop)
1234e98e3e1Schristos {
1244e98e3e1Schristos   val <<= ((N - 1) - stop);
1254e98e3e1Schristos   val &= MSMASKn (start, stop);
1264e98e3e1Schristos   return val;
1274e98e3e1Schristos }
1284e98e3e1Schristos 
1294e98e3e1Schristos /* TAGS: ROT16 ROT32 ROT64 */
1304e98e3e1Schristos 
1314e98e3e1Schristos INLINE_SIM_BITS\
1324e98e3e1Schristos (unsignedN)
1334e98e3e1Schristos ROTn (unsignedN val,
1344e98e3e1Schristos       int shift)
1354e98e3e1Schristos {
1364e98e3e1Schristos   if (shift > 0)
1374e98e3e1Schristos     return ROTRn (val, shift);
1384e98e3e1Schristos   else if (shift < 0)
1394e98e3e1Schristos     return ROTLn (val, -shift);
1404e98e3e1Schristos   else
1414e98e3e1Schristos     return val;
1424e98e3e1Schristos }
1434e98e3e1Schristos 
1444e98e3e1Schristos /* TAGS: ROTL16 ROTL32 ROTL64 */
1454e98e3e1Schristos 
1464e98e3e1Schristos INLINE_SIM_BITS\
1474e98e3e1Schristos (unsignedN)
1484e98e3e1Schristos ROTLn (unsignedN val,
1494e98e3e1Schristos        int shift)
1504e98e3e1Schristos {
1514e98e3e1Schristos   unsignedN result;
1524e98e3e1Schristos   ASSERT (shift <= N);
1534e98e3e1Schristos   result = (((val) << (shift)) | ((val) >> ((N)-(shift))));
1544e98e3e1Schristos   return result;
1554e98e3e1Schristos }
1564e98e3e1Schristos 
1574e98e3e1Schristos /* TAGS: ROTR16 ROTR32 ROTR64 */
1584e98e3e1Schristos 
1594e98e3e1Schristos INLINE_SIM_BITS\
1604e98e3e1Schristos (unsignedN)
1614e98e3e1Schristos ROTRn (unsignedN val,
1624e98e3e1Schristos        int shift)
1634e98e3e1Schristos {
1644e98e3e1Schristos   unsignedN result;
1654e98e3e1Schristos   ASSERT (shift <= N);
1664e98e3e1Schristos   result = (((val) >> (shift)) | ((val) << ((N)-(shift))));
1674e98e3e1Schristos   return result;
1684e98e3e1Schristos }
1694e98e3e1Schristos 
1704e98e3e1Schristos /* TAGS: LSSEXT16 LSSEXT32 LSSEXT64 */
1714e98e3e1Schristos 
1724e98e3e1Schristos INLINE_SIM_BITS\
1734e98e3e1Schristos (unsignedN)
1744e98e3e1Schristos LSSEXTn (signedN val,
1754e98e3e1Schristos 	 int sign_bit)
1764e98e3e1Schristos {
1774e98e3e1Schristos   int shift;
1784e98e3e1Schristos   /* make the sign-bit most significant and then smear it back into
1794e98e3e1Schristos      position */
1804e98e3e1Schristos   ASSERT (sign_bit < N);
1814e98e3e1Schristos   shift = ((N - 1) - sign_bit);
1824e98e3e1Schristos   val <<= shift;
1834e98e3e1Schristos   val >>= shift;
1844e98e3e1Schristos   return val;
1854e98e3e1Schristos }
1864e98e3e1Schristos 
1874e98e3e1Schristos /* TAGS: MSSEXT16 MSSEXT32 MSSEXT64 */
1884e98e3e1Schristos 
1894e98e3e1Schristos INLINE_SIM_BITS\
1904e98e3e1Schristos (unsignedN)
1914e98e3e1Schristos MSSEXTn (signedN val,
1924e98e3e1Schristos 	 int sign_bit)
1934e98e3e1Schristos {
1944e98e3e1Schristos   /* make the sign-bit most significant and then smear it back into
1954e98e3e1Schristos      position */
1964e98e3e1Schristos   ASSERT (sign_bit < N);
1974e98e3e1Schristos   val <<= sign_bit;
1984e98e3e1Schristos   val >>= sign_bit;
1994e98e3e1Schristos   return val;
2004e98e3e1Schristos }
2014e98e3e1Schristos 
2024e98e3e1Schristos 
2034e98e3e1Schristos /* NOTE: See start of file for #define */
2044e98e3e1Schristos #undef LSSEXTn
2054e98e3e1Schristos #undef MSSEXTn
2064e98e3e1Schristos #undef ROTLn
2074e98e3e1Schristos #undef ROTRn
2084e98e3e1Schristos #undef ROTn
2094e98e3e1Schristos #undef LSINSERTEDn
2104e98e3e1Schristos #undef MSINSERTEDn
2114e98e3e1Schristos #undef LSEXTRACTEDn
2124e98e3e1Schristos #undef MSEXTRACTEDn
2134e98e3e1Schristos #undef LSMASKEDn
2144e98e3e1Schristos #undef LSMASKn
2154e98e3e1Schristos #undef MSMASKEDn
2164e98e3e1Schristos #undef MSMASKn
2174e98e3e1Schristos #undef signedN
2184e98e3e1Schristos #undef unsignedN
219