07-10-2019, 09:38 AM
Okay, I tried to run this code locally and there is indeed a bug if I take the code you posted literally.
The code
does not produce the correct value because on the right side it is multiplying 32-bit ints (that's what my compiler says even though they are chars) before adding it into a 64 bit result. It will overflow before adding so converting that will cause strange results (97^5 will result in 4292372961 instead of the correct value 8587340257 for example). I think this is what you mean with 'it's because the function has overflows'.
If this wasn't the case you could easily calculate back a string to fit any value (by dividing it with the numbers I mentioned earlier).
This unfortunately also means it's a bit harder to make a GPU variant because you need to account for the incorrect adding and overflows.
The code
Code:
hash += (s[i] * s[i] * s[i] * s[i] * s[i]);
does not produce the correct value because on the right side it is multiplying 32-bit ints (that's what my compiler says even though they are chars) before adding it into a 64 bit result. It will overflow before adding so converting that will cause strange results (97^5 will result in 4292372961 instead of the correct value 8587340257 for example). I think this is what you mean with 'it's because the function has overflows'.
If this wasn't the case you could easily calculate back a string to fit any value (by dividing it with the numbers I mentioned earlier).
This unfortunately also means it's a bit harder to make a GPU variant because you need to account for the incorrect adding and overflows.