Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-3-5-haiku-20241022· AnthropicHugging 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 DeepSeekdeepseek-v3-0324’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. Claude Assistant

    Here's a modern C++23 implementation for your ARM64 Neon SIMD vector abstraction:

    #include <arm_neon.h>
    #include <cstddef>
    #include <type_traits>
    #include <bit>
    
    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>)
    struct bundle {
        // Type selection logic
        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>,
                    int8x16x1_t,
                    float16x8x2_t
                >
            >
        >;
    
        vector_type data;
    
        // Broadcast constructor
        static constexpr bundle broadcast(ValueType value) {
            bundle result;
            if constexpr (std::is_same_v<ValueType, float>) {
                result.data = vdupq_n_f32x4(value);
            } else if constexpr (std::is_same_v<ValueType, int16_t>) {
                result.data = vdupq_n_s16x8(value);
            } else if constexpr (std::is_same_v<ValueType, int8_t>) {
                result.data = vdupq_n_s8x16(value);
            } else if constexpr (std::is_same_v<ValueType, float16_t>) {
                result.data = vdupq_n_f16x8(value);
            }
            return result;
        }
    
        // Generic load with extension point
        template<typename T = ValueType>
        static bundle load(const void* src) {
            bundle result;
            if constexpr (std::is_same_v<T, ValueType>) {
                if constexpr (std::is_same_v<ValueType, float>) {
                    result.data = vld1q_f32x4(static_cast<const float*>(src));
                } else if constexpr (std::is_same_v<ValueType, int16_t>) {
                    result.data = vld1q_s16x8(static_cast<const int16_t*>(src));
                } else if constexpr (std::is_same_v<ValueType, int8_t>) {
                    result.data = vld1q_s8x16(static_cast<const int8_t*>(src));
                } else if constexpr (std::is_same_v<ValueType, float16_t>) {
                    result.data = vld1q_f16x8(static_cast<const float16_t*>(src));
                }
            } else {
                // Extension point for custom type loading
                result = T::template load<ValueType>(src);
            }
            return result;
        }
    
        // Generic store with 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_f32x4(static_cast<float*>(dst), data);
                } else if constexpr (std::is_same_v<ValueType, int16_t>) {
                    vst1q_s16x8(static_cast<int16_t*>(dst), data);
                } else if constexpr (std::is_same_v<ValueType, int8_t>) {
                    vst1q_s8x16(static_cast<int8_t*>(dst), data);
                } else if constexpr (std::is_same_v<ValueType, float16_t>) {
                    vst1q_f16x8(static_cast<float16_t*>(dst), data);
                }
            } else {
                // Extension point for custom type storing
                T::template store<ValueType>(*this, dst);
            }
        }
    
        // Conversion operator
        constexpr operator vector_type() const { return data; }
    
        // Lane access
        ValueType operator[](size_t lane) const {
            // Implement safe lane access based on type
            if constexpr (std::is_same_v<ValueType, float>) {
                return vgetq_lane_f32(data.val[lane / 4], lane % 4);
            } else if constexpr (std::is_same_v<ValueType, int16_t>) {
                return vgetq_lane_s16(data.val[lane / 8], lane % 8);
            } else if constexpr (std::is_same_v<ValueType, int8_t>) {
                return vgetq_lane_s8(data.val[0], lane);
            } else if constexpr (std::is_same_v<ValueType, float16_t>) {
                return vgetq_lane_f16(data.val[lane / 8], lane % 8);
            }
        }
    };
    

    Key design points:

    • Uses C++23 requires for compile-time type constraints
    • Leverages std::conditional_t for type selection
    • Constexpr where possible
    • Provides extension points for load and store
    • Supports safe lane access
    • Handles different Neon vector types based on ValueType
    • Designed specifically for Apple Silicon ARM64

    Note: This assumes you're using a compiler with good ARM Neon intrinsics support (Clang/Apple Clang). You might need to adjust intrinsic names slightly depending on your exact compiler version.

    Would you like me to elaborate on any part of the implementation?

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