fork download
  1. #include <cstdint>
  2.  
  3. constexpr uint32_t pow(const uint32_t base, const uint32_t exponent)
  4. {
  5. return (exponent == 0) ? 1 : (base * pow(base, exponent-1));
  6. }
  7.  
  8. constexpr uint32_t log2(const uint32_t n, const uint32_t p = 0) {
  9. return (n <= 1) ? p : log2(n / 2, p + 1);
  10. }
  11.  
  12. constexpr bool isPowerOf2(const uint32_t uInput)
  13. {
  14. return uInput == 0 ? false : ((uInput & (uInput - 1)) == 0);
  15. }
  16.  
  17. //TODO replace sizeof...(I) with std::index_sequence<I...>::size() #include <utility>
  18. //TODO replace std::integral_constant<int,D> with int2type<D> (and 'int D' → 'std::size_t D') if needed
  19. // template<std::size_t> struct int2type{};
  20.  
  21. #include <array>
  22. #include <type_traits>
  23.  
  24. //Base case for depth==0
  25. template<int Head, int... Tail>
  26. constexpr std::array<int, sizeof...(Tail)> create_morton_delta_table(std::integral_constant<int,0>)
  27. {
  28. return std::array<int, sizeof...(Tail)>{Tail...};
  29. }
  30.  
  31. template<int Head=1, int... Tail, int Depth>
  32. constexpr std::array<int, pow(2,Depth)*(sizeof...(Tail)+1)-1> create_morton_delta_table(std::integral_constant<int,Depth>)
  33. {
  34. return create_morton_delta_table<Head+6*pow(sizeof...(Tail)+1,3), Tail..., Head, Tail...>(std::integral_constant<int,Depth-1>{});
  35. }
  36.  
  37. //Helper function
  38. template<int Size>
  39. constexpr std::array<int, Size-1> morton_delta_table()
  40. {
  41. static_assert(isPowerOf2(Size), "Morton delta table can only be generated for powers of two");
  42. static_assert(Size>1, "Morton delta table can only be generated for a size>1");
  43. return create_morton_delta_table(std::integral_constant<int,log2(Size)>{});
  44. }
  45.  
  46. #include <iostream>
  47.  
  48. int main(int argc, char **argv) {
  49. constexpr auto d = morton_delta_table<32>();
  50.  
  51. for(const int& i : d)
  52. {
  53. std::cout << i << std::endl;
  54. }
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
1
7
1
55
1
7
1
439
1
7
1
55
1
7
1
3511
1
7
1
55
1
7
1
439
1
7
1
55
1
7
1