1 /////////////////////////////////////////////////////////////////////////
2 // $Id: crc32.cc,v 1.1 2008/08/12 04:57:59 sshwarts Exp $
3 /////////////////////////////////////////////////////////////////////////
5 // Copyright (c) 2008 Stanislav Shwartsman
6 // Written by Stanislav Shwartsman [sshwarts at sourceforge net]
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /////////////////////////////////////////////////////////////////////////
24 #define NEED_CPU_REG_SHORTCUTS 1
27 #define LOG_THIS BX_CPU_THIS_PTR
30 #if (BX_SUPPORT_SSE >= 4) || (BX_SUPPORT_SSE >= 3 && BX_SUPPORT_SSE_EXTENSION > 0)
32 #define CRC32_POLYNOMIAL BX_CONST64(0x11edc6f41)
34 #if (BX_SUPPORT_SSE > 4) || (BX_SUPPORT_SSE >= 4 && BX_SUPPORT_SSE_EXTENSION > 0)
35 // primitives for CRC32 usage
36 BX_CPP_INLINE Bit8u
BitReflect8(Bit8u val8
)
38 return ((val8
& 0x80) >> 7) |
39 ((val8
& 0x40) >> 5) |
40 ((val8
& 0x20) >> 3) |
41 ((val8
& 0x10) >> 1) |
42 ((val8
& 0x08) << 1) |
43 ((val8
& 0x04) << 3) |
44 ((val8
& 0x02) << 5) |
48 BX_CPP_INLINE Bit16u
BitReflect16(Bit16u val16
)
50 return ((Bit16u
)(BitReflect8(val16
& 0xff)) << 8) | BitReflect8(val16
>> 8);
53 BX_CPP_INLINE Bit32u
BitReflect32(Bit32u val32
)
55 return ((Bit32u
)(BitReflect16(val32
& 0xffff)) << 16) | BitReflect16(val32
>> 16);
58 static Bit32u
mod2_64bit(Bit64u divisor
, Bit64u dividend
)
60 Bit64u remainder
= dividend
>> 32;
62 for (int bitpos
=31; bitpos
>=0; bitpos
--) {
63 // copy one more bit from the dividend
64 remainder
= (remainder
<< 1) | ((dividend
>> bitpos
) & 1);
66 // if MSB is set, then XOR divisor and get new remainder
67 if (((remainder
>> 32) & 1) == 1) {
74 #endif // (BX_SUPPORT_SSE > 4) || (BX_SUPPORT_SSE >= 4 && BX_SUPPORT_SSE_EXTENSION > 0)
76 void BX_CPP_AttrRegparmN(1) BX_CPU_C::CRC32_GdEb(bxInstruction_c
*i
)
78 #if (BX_SUPPORT_SSE > 4) || (BX_SUPPORT_SSE >= 4 && BX_SUPPORT_SSE_EXTENSION > 0)
82 op1
= BX_READ_8BIT_REGx(i
->rm(),i
->extend8bitL());
85 bx_address eaddr
= BX_CPU_CALL_METHODR(i
->ResolveModrm
, (i
));
87 op1
= read_virtual_byte(i
->seg(), eaddr
);
90 Bit32u op2
= BX_READ_32BIT_REG(i
->nnn());
91 op2
= BitReflect32(op2
);
93 Bit64u tmp1
= ((Bit64u
) BitReflect8 (op1
)) << 32;
94 Bit64u tmp2
= ((Bit64u
) op2
) << 8;
95 Bit64u tmp3
= tmp1
^ tmp2
;
96 op2
= mod2_64bit(CRC32_POLYNOMIAL
, tmp3
);
98 /* now write result back to destination */
99 BX_WRITE_32BIT_REGZ(i
->nnn(), BitReflect32(op2
));
101 BX_INFO(("CRC32_GdEb: required SSE4_2 support, use --enable-sse and --enable-sse-extension options"));
102 exception(BX_UD_EXCEPTION
, 0, 0);
106 void BX_CPP_AttrRegparmN(1) BX_CPU_C::CRC32_GdEw(bxInstruction_c
*i
)
108 #if (BX_SUPPORT_SSE > 4) || (BX_SUPPORT_SSE >= 4 && BX_SUPPORT_SSE_EXTENSION > 0)
109 Bit32u op2
= BX_READ_32BIT_REG(i
->nnn());
110 op2
= BitReflect32(op2
);
114 op1
= BX_READ_16BIT_REG(i
->rm());
117 bx_address eaddr
= BX_CPU_CALL_METHODR(i
->ResolveModrm
, (i
));
118 op1
= read_virtual_word(i
->seg(), eaddr
);
121 Bit64u tmp1
= ((Bit64u
) BitReflect16(op1
)) << 32;
122 Bit64u tmp2
= ((Bit64u
) op2
) << 16;
123 Bit64u tmp3
= tmp1
^ tmp2
;
124 op2
= mod2_64bit(CRC32_POLYNOMIAL
, tmp3
);
126 /* now write result back to destination */
127 BX_WRITE_32BIT_REGZ(i
->nnn(), BitReflect32(op2
));
130 BX_INFO(("CRC32_GdEw: required SSE4_2 support, use --enable-sse and --enable-sse-extension options"));
131 exception(BX_UD_EXCEPTION
, 0, 0);
135 void BX_CPP_AttrRegparmN(1) BX_CPU_C::CRC32_GdEd(bxInstruction_c
*i
)
137 #if (BX_SUPPORT_SSE > 4) || (BX_SUPPORT_SSE >= 4 && BX_SUPPORT_SSE_EXTENSION > 0)
138 Bit32u op2
= BX_READ_32BIT_REG(i
->nnn());
139 op2
= BitReflect32(op2
);
143 op1
= BX_READ_32BIT_REG(i
->rm());
146 bx_address eaddr
= BX_CPU_CALL_METHODR(i
->ResolveModrm
, (i
));
147 op1
= read_virtual_dword(i
->seg(), eaddr
);
150 Bit64u tmp1
= ((Bit64u
) BitReflect32(op1
)) << 32;
151 Bit64u tmp2
= ((Bit64u
) op2
) << 32;
152 Bit64u tmp3
= tmp1
^ tmp2
;
153 op2
= mod2_64bit(CRC32_POLYNOMIAL
, tmp3
);
155 /* now write result back to destination */
156 BX_WRITE_32BIT_REGZ(i
->nnn(), BitReflect32(op2
));
159 BX_INFO(("CRC32_GdEd: required SSE4_2 support, use --enable-sse and --enable-sse-extension options"));
160 exception(BX_UD_EXCEPTION
, 0, 0);
164 #if BX_SUPPORT_X86_64
166 void BX_CPP_AttrRegparmN(1) BX_CPU_C::CRC32_GdEq(bxInstruction_c
*i
)
168 #if (BX_SUPPORT_SSE > 4) || (BX_SUPPORT_SSE >= 4 && BX_SUPPORT_SSE_EXTENSION > 0)
169 Bit32u op2
= BX_READ_32BIT_REG(i
->nnn());
170 op2
= BitReflect32(op2
);
174 op1
= BX_READ_64BIT_REG(i
->rm());
177 bx_address eaddr
= BX_CPU_CALL_METHODR(i
->ResolveModrm
, (i
));
178 op1
= read_virtual_qword_64(i
->seg(), eaddr
);
181 Bit64u tmp1
= ((Bit64u
) BitReflect32(op1
& 0xffffffff)) << 32;
182 Bit64u tmp2
= ((Bit64u
) op2
) << 32;
183 Bit64u tmp3
= tmp1
^ tmp2
;
184 op2
= mod2_64bit(CRC32_POLYNOMIAL
, tmp3
);
185 tmp1
= ((Bit64u
) BitReflect32(op1
>> 32)) << 32;
186 tmp2
= ((Bit64u
) op2
) << 32;
188 op2
= mod2_64bit(CRC32_POLYNOMIAL
, tmp3
);
190 /* now write result back to destination */
191 BX_WRITE_32BIT_REGZ(i
->nnn(), BitReflect32(op2
));
194 BX_INFO(("CRC32_GdEq: required SSE4_2 support, use --enable-sse and --enable-sse-extension options"));
195 exception(BX_UD_EXCEPTION
, 0, 0);
199 #endif // BX_SUPPORT_X86_64
201 #endif // (BX_SUPPORT_SSE >= 4) || (BX_SUPPORT_SSE >= 3 && BX_SUPPORT_SSE_EXTENSION > 0)