MCPcopy Create free account
hub / github.com/apache/arrow / UnpackStructGenerator

Class UnpackStructGenerator

cpp/src/arrow/util/bpacking_simd_codegen.py:50–211  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

48
49@dataclasses.dataclass
50class UnpackStructGenerator:
51 out_type: str
52 simd_bit_width: int
53
54 @property
55 def out_bit_width(self) -> int:
56 if self.out_type == "bool":
57 return 8
58 elif self.out_type.startswith("uint"):
59 return int(self.out_type.removeprefix("uint").removesuffix("_t"))
60
61 @property
62 def simd_byte_width(self) -> int:
63 return self.simd_bit_width // 8
64
65 @property
66 def simd_value_count(self) -> int:
67 return self.simd_bit_width // self.out_bit_width
68
69 @property
70 def out_byte_width(self) -> int:
71 return self.out_bit_width // 8
72
73 @property
74 def struct_name(self) -> str:
75 return f"Simd{self.simd_bit_width}UnpackerForWidth"
76
77 def struct_specialization(self, bit: int | str) -> str:
78 return f"{self.struct_name}<{self.out_type}, {bit}>"
79
80 def print_struct_declaration(self):
81 print("template<typename Uint, int BitWidth>")
82 print(f"struct {self.struct_name};")
83
84 def print_unpack_bit_func(self, bit: int):
85 def p(code, level=2):
86 print(indent(code, prefix=" " * level))
87
88 p(
89 f"static const uint8_t* unpack(const uint8_t* in, {self.out_type}* out) {{",
90 level=1,
91 )
92
93 mask = (1 << bit) - 1
94 p(f"constexpr {self.out_type} kMask = 0x{mask:0x};")
95 print()
96 p("simd_batch masks(kMask);")
97 p("simd_batch words, shifts;")
98 p("simd_batch results;")
99
100 def safe_load(index):
101 return f"SafeLoadAs<{self.out_type}>(in + {self.out_byte_width} * {index})"
102
103 def static_cast_as_needed(str):
104 if self.out_bit_width < 32:
105 return f"static_cast<{self.out_type}>({str})"
106 return str
107

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected