Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

digest.c 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. Copyright 2012-2014 David Robillard <http://drobilla.net>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #include "zix/digest.h"
  15. #ifdef __SSE4_2__
  16. # include <smmintrin.h>
  17. #endif
  18. ZIX_API uint32_t
  19. zix_digest_start(void)
  20. {
  21. #ifdef __SSE4_2__
  22. return 1; // CRC32 initial value
  23. #else
  24. return 5381; // DJB hash initial value
  25. #endif
  26. }
  27. ZIX_API uint32_t
  28. zix_digest_add(uint32_t hash, const void* const buf, const size_t len)
  29. {
  30. const uint8_t* str = (const uint8_t*)buf;
  31. #ifdef __SSE4_2__
  32. // SSE 4.2 CRC32
  33. for (size_t i = 0; i < (len / sizeof(uint32_t)); ++i) {
  34. hash = _mm_crc32_u32(hash, *(const uint32_t*)str);
  35. str += sizeof(uint32_t);
  36. }
  37. if (len & sizeof(uint16_t)) {
  38. hash = _mm_crc32_u16(hash, *(const uint16_t*)str);
  39. str += sizeof(uint16_t);
  40. }
  41. if (len & sizeof(uint8_t)) {
  42. hash = _mm_crc32_u8(hash, *(const uint8_t*)str);
  43. }
  44. #else
  45. // Classic DJB hash
  46. for (size_t i = 0; i < len; ++i) {
  47. hash = (hash << 5) + hash + str[i];
  48. }
  49. #endif
  50. return hash;
  51. }