I'm writing a function that would compute CRC *value for a string. ( * arbitrary width of 8-64 ). The function accepts 9 parameters, but to illustrate the problem with left-bitshifting, I'm posting only a snippet.
void leftshift(unsigned char* str, unsigned int n, int nshiftleft, unsigned __int64 *value)
{
unsigned __int64 x = str[0];
x = x << nshiftleft;
*value = x;
}
If str[0] is "A" (65) and nshiftleft is 56, I expect value to be 0x4100000000000000 (65 << 56), but it is 0x0000000000000041. The snippet produces a correct result only if I use a literal value, like follows:
void leftshift(unsigned char* str, unsigned int n, int nshiftleft, unsigned __int64 *value)
{
unsigned __int64 x = str[0];
x = x << 56;
*value = x;
}
How to achieve this? Please help.
Edit: I'm a Windows user and I intend to use this function from the scripting language AutoHotkey.
The following code works fine: CRC-64/ECMA_182 value for the string "AutoHotkey" = 0x36990A8E358262B4
void crc64(unsigned __int64 r, unsigned char* s, int n, unsigned __int64 *value)
{
unsigned __int64 poly = 0x42F0E1EBA9EA3693;
unsigned __int64 t = 0x8000000000000000;
unsigned __int64 x = 0;
for (int i=0; i<n; i+=1)
{
x = (__int64)s[i];
r ^= x << 56;
r = (r & t) ? ( (r << 1) ^ poly ) : (r << 1);
r = (r & t) ? ( (r << 1) ^ poly ) : (r << 1);
r = (r & t) ? ( (r << 1) ^ poly ) : (r << 1);
r = (r & t) ? ( (r << 1) ^ poly ) : (r << 1);
r = (r & t) ? ( (r << 1) ^ poly ) : (r << 1);
r = (r & t) ? ( (r << 1) ^ poly ) : (r << 1);
r = (r & t) ? ( (r << 1) ^ poly ) : (r << 1);
r = (r & t) ? ( (r << 1) ^ poly ) : (r << 1);
}
*value = r;
}
The above is the full code. I compile it with LCC x64 (windows) to .obj (without any errors/warning) and extract the .text section which is as follows:
5589e583ec1c535657c745f89336eaa9c745fcebe1f042c745f000000000c745f400000080c745e800000000c745ec00000000c745e400000000e92c0200008b45108b55e40fb6041089c0998945e88955ec8b45e88b55ec89c2b800000000c1e21831450831550c8b45088b550c2345f02355f483fa00750583f80074148b45088b550c0fa4c201d1e03345f83355fceb108b75088b7d0c89f089fa0fa4c201d1e089450889550c8b45088b550c2345f02355f483fa00750583f80074148b45088b550c0fa4c201d1e03345f83355fceb108b75088b7d0c89f089fa0fa4c201d1e089450889550c8b45088b550c2345f02355f483fa00750583f80074148b45088b550c0fa4c201d1e03345f83355fceb108b75088b7d0c89f089fa0fa4c201d1e089450889550c8b45088b550c2345f02355f483fa00750583f80074148b45088b550c0fa4c201d1e03345f83355fceb108b75088b7d0c89f089fa0fa4c201d1e089450889550c8b45088b550c2345f02355f483fa00750583f80074148b45088b550c0fa4c201d1e03345f83355fceb108b75088b7d0c89f089fa0fa4c201d1e089450889550c8b45088b550c2345f02355f483fa00750583f80074148b45088b550c0fa4c201d1e03345f83355fceb108b75088b7d0c89f089fa0fa4c201d1e089450889550c8b45088b550c2345f02355f483fa00750583f80074148b45088b550c0fa4c201d1e03345f83355fceb108b75088b7d0c89f089fa0fa4c201d1e089450889550c8b45088b550c2345f02355f483fa00750583f80074148b45088b550c0fa4c201d1e03345f83355fceb108b75088b7d0c89f089fa0fa4c201d1e089450889550cff45e48b45e43b45140f8cc8fdffff8b45188b4d088b5d0c89088958045f5e5b89ec5dc3
I am able to call the above machine code with AutoHotkey with proper results. Now, I'm trying to make the above function dynamic, so that it will handle crc-8 to crc-64 in one single function.
I have a working prototype written in AutoHotkey here: https://www.autohotkey.com/boards/viewtopic.php?t=89364 (please scroll down to see the function)
Loop iteration is excruciatingly slow with an interpreted language and hence resorting to c machine code.
Thanks to all for confirming the snippet is correct. I will have to inspect the .obj dump, but have no knowledge in asm. I conclude that the code isn't portable for my need.