htable32.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. #include <common/config.h>
  18. #include <common/htable32.h>
  19. #include <stdint.h>
  20. #include <string.h>
  21. void *_starpu_htbl_search_32(struct starpu_htbl32_node_s *htbl, uint32_t key)
  22. {
  23. unsigned currentbit;
  24. unsigned keysize = 32;
  25. starpu_htbl32_node_t *current_htbl = htbl;
  26. /* 000000000001111 with HTBL_NODE_SIZE 1's */
  27. uint32_t mask = (1<<STARPU_HTBL32_NODE_SIZE)-1;
  28. for(currentbit = 0; currentbit < keysize; currentbit+=STARPU_HTBL32_NODE_SIZE)
  29. {
  30. // printf("search : current bit = %d \n", currentbit);
  31. if (STARPU_UNLIKELY(current_htbl == NULL))
  32. return NULL;
  33. /* 0000000000001111
  34. * | currentbit
  35. * 0000111100000000 = offloaded_mask
  36. * |last_currentbit
  37. * */
  38. unsigned last_currentbit =
  39. keysize - (currentbit + STARPU_HTBL32_NODE_SIZE);
  40. uint32_t offloaded_mask = mask << last_currentbit;
  41. unsigned current_index =
  42. (key & (offloaded_mask)) >> (last_currentbit);
  43. current_htbl = current_htbl->children[current_index];
  44. }
  45. return current_htbl;
  46. }
  47. /*
  48. * returns the previous value of the tag, or NULL else
  49. */
  50. void *_starpu_htbl_insert_32(struct starpu_htbl32_node_s **htbl, uint32_t key, void *entry)
  51. {
  52. unsigned currentbit;
  53. unsigned keysize = 32;
  54. starpu_htbl32_node_t **current_htbl_ptr = htbl;
  55. /* 000000000001111 with HTBL_NODE_SIZE 1's */
  56. uint32_t mask = (1<<STARPU_HTBL32_NODE_SIZE)-1;
  57. for(currentbit = 0; currentbit < keysize; currentbit+=STARPU_HTBL32_NODE_SIZE)
  58. {
  59. //printf("insert : current bit = %d \n", currentbit);
  60. if (*current_htbl_ptr == NULL) {
  61. /* TODO pad to change that 1 into 16 ? */
  62. *current_htbl_ptr = calloc(sizeof(starpu_htbl32_node_t), 1);
  63. assert(*current_htbl_ptr);
  64. }
  65. /* 0000000000001111
  66. * | currentbit
  67. * 0000111100000000 = offloaded_mask
  68. * |last_currentbit
  69. * */
  70. unsigned last_currentbit =
  71. keysize - (currentbit + STARPU_HTBL32_NODE_SIZE);
  72. uint32_t offloaded_mask = mask << last_currentbit;
  73. unsigned current_index =
  74. (key & (offloaded_mask)) >> (last_currentbit);
  75. current_htbl_ptr =
  76. &((*current_htbl_ptr)->children[current_index]);
  77. }
  78. /* current_htbl either contains NULL or a previous entry
  79. * we overwrite it anyway */
  80. void *old_entry = *current_htbl_ptr;
  81. *current_htbl_ptr = entry;
  82. return old_entry;
  83. }