hash.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 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 <starpu_hash.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #define _STARPU_CRC32C_POLY_BE 0x1EDC6F41
  21. static inline uint32_t __attribute__ ((pure)) starpu_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) ? _STARPU_CRC32C_POLY_BE : 0);
  28. return crc;
  29. }
  30. uint32_t starpu_crc32_be_n(void *input, size_t n, uint32_t inputcrc)
  31. {
  32. uint8_t *p = (uint8_t *)input;
  33. size_t i;
  34. uint32_t crc = inputcrc;
  35. for (i = 0; i < n; i++)
  36. crc = starpu_crc32_be_8(p[i], crc);
  37. return crc;
  38. }
  39. uint32_t starpu_crc32_be(uint32_t input, uint32_t inputcrc)
  40. {
  41. uint8_t *p = (uint8_t *)&input;
  42. uint32_t crc = inputcrc;
  43. crc = starpu_crc32_be_8(p[0], crc);
  44. crc = starpu_crc32_be_8(p[1], crc);
  45. crc = starpu_crc32_be_8(p[2], crc);
  46. crc = starpu_crc32_be_8(p[3], crc);
  47. return crc;
  48. }
  49. uint32_t starpu_crc32_string(char *str, uint32_t inputcrc)
  50. {
  51. uint32_t hash = inputcrc;
  52. size_t len = strlen(str);
  53. unsigned i;
  54. for (i = 0; i < len; i++)
  55. {
  56. hash = starpu_crc32_be_8((uint8_t)str[i], hash);
  57. }
  58. return hash;
  59. }