Skip to content
TheChatArchiveThe Chat Archive
DeepSeekdeepseek-v3-0324· DeepSeekHugging Face LMArena Human Preference 140K

Can you write me a modern c++23 struct that abstracts arm64 neon simd vectors. Running…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-3-5-haiku-20241022’s answer.
  1. User

    Can you write me a modern c++23 struct that abstracts arm64 neon simd vectors. Running on apple silicone MacBook.

    template struct bundle { using vector_type = … // e.g. float32x4x4_t for ValueType=float32_t and 16 Lane static bundle broadcast(ValueType value); static bundle load(void* src); bundle store(void* dst); operator vector_type() const; // also add an assignment constructor… ValueType operator[](int lane); }

    We only support 16 lanes, and types float32, float16, int16, and int8. Make tidy and avoid macros. Don’t go overboard with nested templates, use constexpr where it’s better… load and store should be extension points where other types can implement their encoding / decoding.

  2. DeepSeek Assistant

    Here's a modern C++23 implementation of a SIMD vector abstraction for ARM64 NEON on Apple Silicon, following your requirements:

    #include <arm_neon.h>
    #include <cstddef>
    #include <cstdint>
    #include <type_traits>
    
    // Supported types
    using float32_t = float;
    using float16_t = __fp16;
    using int16_t = std::int16_t;
    using int8_t = std::int8_t;
    
    template <typename ValueType, size_t Lanes>
    struct bundle {
        static_assert(Lanes == 16, "Only 16 lanes are supported");
        static_assert(std::is_same_v<ValueType, float32_t> || 
                      std::is_same_v<ValueType, float16_t> ||
                      std::is_same_v<ValueType, int16_t> ||
                      std::is_same_v<ValueType, int8_t>,
                      "Unsupported value type");
    
        // Vector type selection
        using vector_type = []() {
            if constexpr (std::is_same_v<ValueType, float32_t>) {
                return float32x4x4_t;
            } else if constexpr (std::is_same_v<ValueType, float16_t>) {
                return float16x8x2_t;
            } else if constexpr (std::is_same_v<ValueType, int16_t>) {
                return int16x8x2_t;
            } else if constexpr (std::is_same_v<ValueType, int8_t>) {
                return int8x16x1_t;
            }
        }();
    
        vector_type data;
    
        // Broadcast constructor
        static bundle broadcast(ValueType value) {
            if constexpr (std::is_same_v<ValueType, float32_t>) {
                auto v = vdupq_n_f32(value);
                return bundle{float32x4x4_t{v, v, v, v}};
            } else if constexpr (std::is_same_v<ValueType, float16_t>) {
                auto v = vdupq_n_f16(value);
                return bundle{float16x8x2_t{v, v}};
            } else if constexpr (std::is_same_v<ValueType, int16_t>) {
                auto v = vdupq_n_s16(value);
                return bundle{int16x8x2_t{v, v}};
            } else if constexpr (std::is_same_v<ValueType, int8_t>) {
                auto v = vdupq_n_s8(value);
                return bundle{int8x16x1_t{v}};
            }
        }
    
        // Load from memory with encoding/decoding extension point
        template <typename T>
        static bundle load(const T* src) {
            if constexpr (requires { T::neon_load(src); }) {
                return T::neon_load(src);
            } else {
                if constexpr (std::is_same_v<ValueType, float32_t>) {
                    return bundle{float32x4x4_t{
                        vld1q_f32(reinterpret_cast<const float32_t*>(src)),
                        vld1q_f32(reinterpret_cast<const float32_t*>(src) + 4),
                        vld1q_f32(reinterpret_cast<const float32_t*>(src) + 8),
                        vld1q_f32(reinterpret_cast<const float32_t*>(src) + 12)}};
                } else if constexpr (std::is_same_v<ValueType, float16_t>) {
                    return bundle{float16x8x2_t{
                        vld1q_f16(reinterpret_cast<const float16_t*>(src)),
                        vld1q_f16(reinterpret_cast<const float16_t*>(src) + 8)}};
                } else if constexpr (std::is_same_v<ValueType, int16_t>) {
                    return bundle{int16x8x2_t{
                        vld1q_s16(reinterpret_cast<const int16_t*>(src)),
                        vld1q_s16(reinterpret_cast<const int16_t*>(src) + 8)}};
                } else if constexpr (std::is_same_v<ValueType, int8_t>) {
                    return bundle{int8x16x1_t{
                        vld1q_s8(reinterpret_cast<const int8_t*>(src))}};
                }
            }
        }
    
        // Store to memory with encoding/decoding extension point
        template <typename T>
        void store(T* dst) const {
            if constexpr (requires { T::neon_store(data, dst); }) {
                T::neon_store(data, dst);
            } else {
                if constexpr (std::is_same_v<ValueType, float32_t>) {
                    vst1q_f32(reinterpret_cast<float32_t*>(dst), data.val[0]);
                    vst1q_f32(reinterpret_cast<float32_t*>(dst) + 4, data.val[1]);
                    vst1q_f32(reinterpret_cast<float32_t*>(dst) + 8, data.val[2]);
                    vst1q_f32(reinterpret_cast<float32_t*>(dst) + 12, data.val[3]);
                } else if constexpr (std::is_same_v<ValueType, float16_t>) {
                    vst1q_f16(reinterpret_cast<float16_t*>(dst), data.val[0]);
                    vst1q_f16(reinterpret_cast<float16_t*>(dst) + 8, data.val[1]);
                } else if constexpr (std::is_same_v<ValueType, int16_t>) {
                    vst1q_s16(reinterpret_cast<int16_t*>(dst), data.val[0]);
                    vst1q_s16(reinterpret_cast<int16_t*>(dst) + 8, data.val[1]);
                } else if constexpr (std::is_same_v<ValueType, int8_t>) {
                    vst1q_s8(reinterpret_cast<int8_t*>(dst), data.val[0]);
                }
            }
        }
    
        // Conversion to vector_type
        operator vector_type() const { return data; }
    
        // Assignment from vector_type
        bundle& operator=(vector_type v) {
            data = v;
            return *this;
        }
    
        // Lane access
        ValueType operator[](int lane) const {
            if constexpr (std::is_same_v<ValueType, float32_t>) {
                return data.val[lane / 4][lane % 4];
            } else if constexpr (std::is_same_v<ValueType, float16_t>) {
                return data.val[lane / 8][lane % 8];
            } else if constexpr (std::is_same_v<ValueType, int16_t>) {
                return data.val[lane / 8][lane % 8];
            } else if constexpr (std::is_same_v<ValueType, int8_t>) {
                return data.val[0][lane];
            }
        }
    };
    

    Key features:

    1. Uses C++23 features like if constexpr for clean type-specific code paths
    2. Provides extension points for load/store through template specialization
    3. Maintains type safety with static assertions
    4. Clean, macro-free implementation
    5. Proper NEON vector types for each supported data type
    6. Constexpr where applicable (though most operations are runtime operations)
    7. Simple, direct mapping to ARM64 NEON intrinsics

    Example usage with extension point:

    // Custom type with special encoding
    struct MyEncodedFloat {
        static bundle<float32_t, 16> neon_load(const MyEncodedFloat* src) {
            // Custom decoding logic here
            return bundle<float32_t, 16>::broadcast(0.0f); // example
        }
        
        static void neon_store(float32x4x4_t data, MyEncodedFloat* dst) {
            // Custom encoding logic here
        }
    };
    
    void example() {
        // Regular usage
        float32_t array[16];
        auto b = bundle<float32_t, 16>::load(array);
        b.store(array);
        
        // Custom encoded usage
        MyEncodedFloat encoded[16];
        auto b2 = bundle<float32_t, 16>::load(encoded);
        b2.store(encoded);
    }
    

    The implementation is optimized for Apple Silicon while remaining clean and maintainable. The extension points allow for custom encoding/decoding without modifying the core bundle implementation.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.