hash.c 1.9 KB

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