hash.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010 Centre National de la Recherche Scientifique
  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 <common/hash.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #define CRC32C_POLY_BE 0x1EDC6F41
  21. static inline uint32_t __attribute__ ((pure)) crc32_be_8(uint8_t inputbyte, uint32_t inputcrc)
  22. {
  23. unsigned i;
  24. uint32_t crc;
  25. crc = inputcrc ^ (inputbyte << 24);
  26. for (i = 0; i < 8; i++)
  27. crc = (crc << 1) ^ ((crc & 0x80000000) ? CRC32C_POLY_BE : 0);
  28. return crc;
  29. }
  30. uint32_t _starpu_crc32_be(uint32_t input, uint32_t inputcrc)
  31. {
  32. uint8_t *p = (uint8_t *)&input;
  33. uint32_t crc = inputcrc;
  34. crc = crc32_be_8(p[0], crc);
  35. crc = crc32_be_8(p[1], crc);
  36. crc = crc32_be_8(p[2], crc);
  37. crc = crc32_be_8(p[3], crc);
  38. return crc;
  39. }
  40. uint32_t _starpu_crc32_string(char *str, uint32_t inputcrc)
  41. {
  42. uint32_t hash = inputcrc;
  43. size_t len = strlen(str);
  44. unsigned i;
  45. for (i = 0; i < len; i++)
  46. {
  47. hash = crc32_be_8((uint8_t)str[i], hash);
  48. }
  49. return hash;
  50. }