squareHash.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. Copyright (c) 2019 tevador
  3. This file is part of RandomX.
  4. RandomX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. RandomX is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with RandomX. If not, see<http://www.gnu.org/licenses/>.
  14. */
  15. #include <stdint.h>
  16. #if !defined(_M_X64) && !defined(__x86_64__)
  17. typedef struct {
  18. uint64_t lo;
  19. uint64_t hi;
  20. } uint128_t;
  21. #define LO(x) ((x)&0xffffffff)
  22. #define HI(x) ((x)>>32)
  23. static inline uint128_t square128(uint64_t x) {
  24. uint64_t xh = HI(x), xl = LO(x);
  25. uint64_t xll = xl * xl;
  26. uint64_t xlh = xl * xh;
  27. uint64_t xhh = xh * xh;
  28. uint64_t m1 = 2 * LO(xlh) + HI(xll);
  29. uint64_t m2 = 2 * HI(xlh) + LO(xhh) + HI(m1);
  30. uint64_t m3 = HI(xhh) + HI(m2);
  31. uint128_t x2;
  32. x2.lo = (m1 << 32) + LO(xll);
  33. x2.hi = (m3 << 32) + LO(m2);
  34. return x2;
  35. }
  36. #undef LO(x)
  37. #undef HI(x)
  38. inline uint64_t squareHash(uint64_t x) {
  39. x += 1613783669344650115;
  40. for (int i = 0; i < 42; ++i) {
  41. uint128_t x2 = square128(x);
  42. x = x2.lo - x2.hi;
  43. }
  44. return x;
  45. }
  46. #else
  47. #if defined(__cplusplus)
  48. extern "C" {
  49. #endif
  50. uint64_t squareHash(uint64_t);
  51. #if defined(__cplusplus)
  52. }
  53. #endif
  54. #endif