hilbert.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include "morton.h"
  5. #include "hilbert.h"
  6. Leaf* allocLeaf() {
  7. Leaf* l;
  8. l = calloc(1, sizeof(Leaf));
  9. return l;
  10. }
  11. void printLeaf(Leaf* l) {
  12. uint64_t i, j;
  13. for(i = 0; i < 8; i++) {
  14. for(j = 1; j > 0; j <<= 1) {
  15. //printf("[%lud] ", j);
  16. putchar(((l->b[i] & j) == j) ? '1' : '0');
  17. }
  18. printf(" %#lX", l->b[i]);
  19. fputs("\n", stdout);
  20. }
  21. fputs("\n", stdout);
  22. };
  23. void setLeafBit(Leaf* l, int bit, int val) {
  24. uint64_t i, j, v;
  25. i = bit / 64;
  26. j = bit % 64;
  27. printf(" i: %lud, j: %lud \n", i,j);
  28. v = val & 1;
  29. // some magic from SO
  30. l->b[i] ^= (-v ^ l->b[i]) & (1UL << j);
  31. }
  32. int getLeafBit(Leaf* l, int bit) {
  33. uint64_t i, j, v;
  34. i = bit / 64;
  35. j = bit % 64;
  36. v = 1UL << j;
  37. // some magic from SO
  38. return (l->b[i] & v) == v;
  39. }
  40. // edgeSize = 2 ^ edgeBitSizeExp
  41. // allocSize = edgesize ^ 3
  42. Bitfield* allocBitfield(int edgeByteSizeExp, int bitsPerCell) {
  43. int esz, ac;
  44. Bitfield* bf;
  45. esz = 1 << edgeByteSizeExp;
  46. ac = (esz * esz * esz) * bitsPerCell;
  47. bf = calloc(1, sizeof(Bitfield) + (ac * 8));
  48. bf->allocSize = ac;
  49. bf->edgeSize = esz * 2; // cuberoot(8) == 2
  50. bf->bitsPerCell = bitsPerCell;
  51. printf(" %d bytes allocated \n", ac);
  52. return bf;
  53. }
  54. void getBit(Bitfield* bf, int x, int y, int z, int v) {
  55. }
  56. void setBit(Bitfield* bf, int x, int y, int z, int v) {
  57. }
  58. int main(int argc, char* argv[]) {
  59. Leaf* l;
  60. Bitfield* bf;
  61. initMortonLUT();
  62. /*
  63. l = allocLeaf();
  64. setBit(l, 2, 1);
  65. setBit(l, 63, 1);
  66. setBit(l, 511, 1);
  67. printLeaf(l);
  68. printf(" lol: %d\n", getBit(l, 2));
  69. printf(" lol: %d\n", getBit(l, 3));
  70. */
  71. bf = allocBitfield(8, 2);
  72. return 0;
  73. }