Great! Let's extend the bundle struct to support a custom int4 type that decodes 16 int8 values from 8 bytes (each byte contains two 4-bit signed integers packed).
What this means:
- The input is 8 bytes (64 bits).
- Each byte contains two 4-bit signed integers (nibbles).
- We want to unpack these into 16 int8_t lanes, each sign-extended from 4 bits.
Implementation plan:
- Define a new empty tag type
int4 to represent this custom type.
- Specialize
bundle<int4, 16> with custom load and store implementations.
load will unpack 8 bytes into 16 int8_t lanes.
store will pack 16 int8_t lanes back into 8 bytes.
- Use Neon intrinsics for efficient unpacking and sign extension.
Updated code snippet with int4 extension:
#include <arm_neon.h>
#include <cstddef>
#include <type_traits>
#include <cstdint>
// Custom int4 tag type
struct int4 {};
// Base bundle template (same as before, but simplified here for clarity)
template<typename ValueType, size_t Lanes>
requires (Lanes == 16) &&
(std::is_same_v<ValueType, float> ||
std::is_same_v<ValueType, int16_t> ||
std::is_same_v<ValueType, int8_t> ||
std::is_same_v<ValueType, float16_t> ||
std::is_same_v<ValueType, int4>)
struct bundle {
using vector_type = std::conditional_t<
std::is_same_v<ValueType, float>,
float32x4x4_t,
std::conditional_t<
std::is_same_v<ValueType, int16_t>,
int16x8x2_t,
std::conditional_t<
std::is_same_v<ValueType, int8_t>,
int8x16_t,
std::conditional_t<
std::is_same_v<ValueType, float16_t>,
float16x8x2_t,
void // no vector_type for int4, handled separately
>
>
>
>;
// For int4, we store as int8x16_t internally after decoding
std::conditional_t<std::is_same_v<ValueType, int4>, int8x16_t, vector_type> data;
// Broadcast (only for supported types except int4)
static constexpr bundle broadcast(ValueType value) {
if constexpr (std::is_same_v<ValueType, float>) {
return bundle{vdupq_n_f32(value)};
} else if constexpr (std::is_same_v<ValueType, int16_t>) {
return bundle{vdupq_n_s16(value)};
} else if constexpr (std::is_same_v<ValueType, int8_t>) {
return bundle{vdupq_n_s8(value)};
} else if constexpr (std::is_same_v<ValueType, float16_t>) {
return bundle{vdupq_n_f16(value)};
} else {
static_assert(!std::is_same_v<ValueType, ValueType>, "broadcast not supported for this type");
}
}
// Load generic (extension point)
template<typename T = ValueType>
static bundle load(const void* src) {
if constexpr (std::is_same_v<T, ValueType>) {
if constexpr (std::is_same_v<ValueType, float>) {
return bundle{vld1q_f32(static_cast<const float*>(src))};
} else if constexpr (std::is_same_v<ValueType, int16_t>) {
return bundle{vld1q_s16(static_cast<const int16_t*>(src))};
} else if constexpr (std::is_same_v<ValueType, int8_t>) {
return bundle{vld1q_s8(static_cast<const int8_t*>(src))};
} else if constexpr (std::is_same_v<ValueType, float16_t>) {
return bundle{vld1q_f16(static_cast<const float16_t*>(src))};
} else if constexpr (std::is_same_v<ValueType, int4>) {
// Custom int4 load
return load_int4(src);
}
} else {
// Extension point for other types
return T::template load<ValueType>(src);
}
}
// Store generic (extension point)
template<typename T = ValueType>
void store(void* dst) const {
if constexpr (std::is_same_v<T, ValueType>) {
if constexpr (std::is_same_v<ValueType, float>) {
vst1q_f32(static_cast<float*>(dst), data);
} else if constexpr (std::is_same_v<ValueType, int16_t>) {
vst1q_s16(static_cast<int16_t*>(dst), data);
} else if constexpr (std::is_same_v<ValueType, int8_t>) {
vst1q_s8(static_cast<int8_t*>(dst), data);
} else if constexpr (std::is_same_v<ValueType, float16_t>) {
vst1q_f16(static_cast<float16_t*>(dst), data);
} else if constexpr (std::is_same_v<ValueType, int4>) {
store_int4(dst);
}
} else {
T::template store<ValueType>(*this, dst);
}
}
// Conversion operator for non-int4 types
constexpr operator vector_type() const requires (!std::is_same_v<ValueType, int4>) {
return data;
}
// Lane access for non-int4 types
ValueType operator[](size_t lane) const requires (!std::is_same_v<ValueType, int4>) {
if constexpr (std::is_same_v<ValueType, float>) {
return vgetq_lane_f32(data, lane);
} else if constexpr (std::is_same_v<ValueType, int16_t>) {
return vgetq_lane_s16(data, lane);
} else if constexpr (std::is_same_v<ValueType, int8_t>) {
return vgetq_lane_s8(data, lane);
} else if constexpr (std::is_same_v<ValueType, float16_t>) {
return vgetq_lane_f16(data, lane);
}
}
// Lane access for int4
int8_t operator[](size_t lane) const requires std::is_same_v<ValueType, int4> {
return vgetq_lane_s8(data, lane);
}
private:
// Helper: load int4 from 8 bytes
static bundle load_int4(const void* src) {
bundle b;
// Load 8 bytes (64 bits)
uint8x8_t packed = vld1_u8(static_cast<const uint8_t*>(src));
// Extract low nibbles
uint8x8_t low_nibbles = vand_u8(packed, vdup_n_u8(0x0F));
// Extract high nibbles
uint8x8_t high_nibbles = vshrn_n_u16(vshlq_n_u16(vmovl_u8(packed), 4), 4);
// Sign extend 4-bit values to 8-bit signed
// Convert to int8x8_t with sign extension
int8x8_t low_signed = vreinterpret_s8_u8(vshl_u8(low_nibbles, vdup_n_u8(4)));
low_signed = vshrq_n_s8(low_signed, 4);
int8x8_t high_signed = vreinterpret_s8_u8(vshl_u8(high_nibbles, vdup_n_u8(4)));
high_signed = vshrq_n_s8(high_signed, 4);
// Combine low and high into int8x16_t
b.data = vcombine_s8(low_signed, high_signed);
return b;
}
// Helper: store int4 to 8 bytes
void store_int4(void* dst) const {
// Split into low and high 8 lanes
int8x8_t low = vget_low_s8(data);
int8x8_t high = vget_high_s8(data);
// Mask to 4 bits and pack
uint8x8_t low_nibbles = vand_u8(vreinterpret_u8_s8(low), vdup_n_u8(0x0F));
uint8x8_t high_nibbles = vand_u8(vreinterpret_u8_s8(high), vdup_n_u8(0x0F));
// Shift high nibbles left by 4 bits
uint8x8_t packed = vorr_u8(low_nibbles, vshl_n_u8(high_nibbles, 4));
vst1_u8(static_cast<uint8_t*>(dst), packed);
}
};
Explanation:
int4 is a tag type to represent 4-bit signed integers packed into bytes.
bundle<int4, 16> stores data internally as int8x16_t after decoding.
load_int4:
- Loads 8 bytes.
- Extracts low and high nibbles.
- Sign-extends each nibble to int8.
- Combines into a 16-lane int8 vector.
store_int4:
- Takes 16 int8 lanes.
- Masks to 4 bits.
- Packs two 4-bit values into each byte.
- Stores 8 bytes.
- Lane access returns the sign-extended int8 value.
Usage example:
int main() {
alignas(8) uint8_t packed_data[8] = { /* 8 bytes of packed int4 data */ };
auto b = bundle<int4, 16>::load(packed_data);
for (int i = 0; i < 16; ++i) {
int8_t val = b[i];
// Use val...
}
alignas(8) uint8_t output[8];
b.store(output);
}
If you want, I can help you with a full working example or tests!