hash.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2008-2011,2013,2014,2016 Université de Bordeaux
  4. * Copyright (C) 2012 Inria
  5. * Copyright (C) 2010,2011,2013,2015,2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include <starpu_hash.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #define _STARPU_CRC32C_POLY_BE 0x1EDC6F41
  23. static inline uint32_t STARPU_ATTRIBUTE_PURE starpu_crc32c_be_8(uint8_t inputbyte, uint32_t inputcrc)
  24. {
  25. unsigned i;
  26. uint32_t crc;
  27. crc = inputcrc ^ (((uint32_t) inputbyte) << 24);
  28. for (i = 0; i < 8; i++)
  29. crc = (crc << 1) ^ ((crc & 0x80000000) ? _STARPU_CRC32C_POLY_BE : 0);
  30. return crc;
  31. }
  32. uint32_t starpu_hash_crc32c_be_n(const void *input, size_t n, uint32_t inputcrc)
  33. {
  34. uint8_t *p = (uint8_t *)input;
  35. size_t i;
  36. uint32_t crc = inputcrc;
  37. for (i = 0; i < n; i++)
  38. crc = starpu_crc32c_be_8(p[i], crc);
  39. return crc;
  40. }
  41. uint32_t starpu_hash_crc32c_be(uint32_t input, uint32_t inputcrc)
  42. {
  43. uint8_t *p = (uint8_t *)&input;
  44. uint32_t crc = inputcrc;
  45. crc = starpu_crc32c_be_8(p[0], crc);
  46. crc = starpu_crc32c_be_8(p[1], crc);
  47. crc = starpu_crc32c_be_8(p[2], crc);
  48. crc = starpu_crc32c_be_8(p[3], crc);
  49. return crc;
  50. }
  51. uint32_t starpu_hash_crc32c_string(const char *str, uint32_t inputcrc)
  52. {
  53. uint32_t hash = inputcrc;
  54. size_t len = strlen(str);
  55. unsigned i;
  56. for (i = 0; i < len; i++)
  57. {
  58. hash = starpu_crc32c_be_8((uint8_t)str[i], hash);
  59. }
  60. return hash;
  61. }