【C++】能存超级大的整数?

🚀 __int(SIZE) 完整数学运算库:支持无符号整数和数组操作

让我为你实现一个完整的 __int(SIZE) 系统,支持无符号整数、数组操作以及所有数学运算!

📦 完整实现代码

#ifndef __INT_SIZE_MATH_LIBRARY_H__
#define __INT_SIZE_MATH_LIBRARY_H__

#include <cstdint>
#include <vector>
#include <array>
#include <type_traits>
#include <iostream>
#include <algorithm>
#include <immintrin.h>
#include <cstring>
#include <stdexcept>
#include <memory>

// 检查编译器支持
#if defined(__GNUC__) || defined(__clang__)
    #if defined(__SIZEOF_INT128__)
        #define HAS_INT128 1
        using int128_t = __int128;
        using uint128_t = unsigned __int128;
    #else
        #define HAS_INT128 0
    #endif
#else
    #define HAS_INT128 0
#endif

// 基础类型映射
namespace int_size_detail {
    // 有符号整数类型
    template<int SIZE> struct signed_int_type { using type = void; };
    template<> struct signed_int_type<8>  { using type = int8_t;  };
    template<> struct signed_int_type<16> { using type = int16_t; };
    template<> struct signed_int_type<32> { using type = int32_t; };
    template<> struct signed_int_type<64> { using type = int64_t; };
#if HAS_INT128
    template<> struct signed_int_type<128> { using type = int128_t; };
#endif

    // 无符号整数类型
    template<int SIZE> struct unsigned_int_type { using type = void; };
    template<> struct unsigned_int_type<8>  { using type = uint8_t;  };
    template<> struct unsigned_int_type<16> { using type = uint16_t; };
    template<> struct unsigned_int_type<32> { using type = uint32_t; };
    template<> struct unsigned_int_type<64> { using type = uint64_t; };
#if HAS_INT128
    template<> struct unsigned_int_type<128> { using type = uint128_t; };
#endif

    // 检查支持
    template<int SIZE>
    constexpr bool is_supported() {
        if constexpr (SIZE == 8 || SIZE == 16 || SIZE == 32 || SIZE == 64) {
            return true;
        }
#if HAS_INT128
        else if constexpr (SIZE == 128) {
            return true;
        }
#endif
        else {
            return false;
        }
    }
}

// 主要宏定义
#define __int(SIZE) typename int_size_detail::signed_int_type<SIZE>::type
#define __uint(SIZE) typename int_size_detail::unsigned_int_type<SIZE>::type
#define __int_supported(SIZE) int_size_detail::is_supported<SIZE>()

// ================================================
// 数组类型定义
// ================================================

// 动态数组
template<int SIZE>
using IntArray = std::vector<__int(SIZE)>;

template<int SIZE>
using UIntArray = std::vector<__uint(SIZE)>;

// 静态数组
template<int SIZE, size_t N>
using StaticIntArray = std::array<__int(SIZE), N>;

template<int SIZE, size_t N>
using StaticUIntArray = std::array<__uint(SIZE), N>;

// ================================================
// 数学运算库
// ================================================

namespace IntMath {
    
    // ================================================
    // 基本算术运算 (+, -, *, /)
    // ================================================
    
    template<int SIZE>
    __int(SIZE) add(__int(SIZE) a, __int(SIZE) b) {
        return a + b;
    }
    
    template<int SIZE>
    __uint(SIZE) add(__uint(SIZE) a, __uint(SIZE) b) {
        return a + b;
    }
    
    template<int SIZE>
    __int(SIZE) subtract(__int(SIZE) a, __int(SIZE) b) {
        return a - b;
    }
    
    template<int SIZE>
    __uint(SIZE) subtract(__uint(SIZE) a, __uint(SIZE) b) {
        return a - b;
    }
    
    template<int SIZE>
    __int(SIZE) multiply(__int(SIZE) a, __int(SIZE) b) {
        return a * b;
    }
    
    template<int SIZE>
    __uint(SIZE) multiply(__uint(SIZE) a, __uint(SIZE) b) {
        return a * b;
    }
    
    template<int SIZE>
    __int(SIZE) divide(__int(SIZE) a, __int(SIZE) b) {
        if (b == 0) throw std::runtime_error("除零错误");
        return a / b;
    }
    
    template<int SIZE>
    __uint(SIZE) divide(__uint(SIZE) a, __uint(SIZE) b) {
        if (b == 0) throw std::runtime_error("除零错误");
        return a / b;
    }
    
    template<int SIZE>
    __int(SIZE) modulo(__int(SIZE) a, __int(SIZE) b) {
        if (b == 0) throw std::runtime_error("除零错误");
        return a % b;
    }
    
    template<int SIZE>
    __uint(SIZE) modulo(__uint(SIZE) a, __uint(SIZE) b) {
        if (b == 0) throw std::runtime_error("除零错误");
        return a % b;
    }
    
    // ================================================
    // 快速平方根(使用硬件指令)
    // ================================================
    
    // 32位浮点平方根
    inline float fast_sqrt(float x) {
        return _mm_cvtss_f32(_mm_sqrt_ss(_mm_set_ss(x)));
    }
    
    // 64位双精度平方根
    inline double fast_sqrt(double x) {
        return _mm_cvtsd_f32(_mm_sqrt_sd(_mm_set_sd(x), _mm_set_sd(x)));
    }
    
    // 整数平方根(转换为浮点计算)
    template<int SIZE>
    __uint(SIZE) sqrt_int(__int(SIZE) x) {
        static_assert(SIZE <= 64, "整数平方根仅支持64位及以下");
        if (x < 0) throw std::runtime_error("负数不能开平方根");
        
        if constexpr (SIZE <= 32) {
            return static_cast<__uint(SIZE)>(fast_sqrt(static_cast<float>(x)));
        } else {
            return static_cast<__uint(SIZE)>(fast_sqrt(static_cast<double>(x)));
        }
    }
    
    template<int SIZE>
    __uint(SIZE) sqrt_int(__uint(SIZE) x) {
        static_assert(SIZE <= 64, "整数平方根仅支持64位及以下");
        
        if constexpr (SIZE <= 32) {
            return static_cast<__uint(SIZE)>(fast_sqrt(static_cast<float>(x)));
        } else {
            return static_cast<__uint(SIZE)>(fast_sqrt(static_cast<double>(x)));
        }
    }
    
    // ================================================
    // 位运算
    // ================================================
    
    template<int SIZE>
    __int(SIZE) bitwise_and(__int(SIZE) a, __int(SIZE) b) {
        return a & b;
    }
    
    template<int SIZE>
    __uint(SIZE) bitwise_and(__uint(SIZE) a, __uint(SIZE) b) {
        return a & b;
    }
    
    template<int SIZE>
    __int(SIZE) bitwise_or(__int(SIZE) a, __int(SIZE) b) {
        return a | b;
    }
    
    template<int SIZE>
    __uint(SIZE) bitwise_or(__uint(SIZE) a, __uint(SIZE) b) {
        return a | b;
    }
    
    template<int SIZE>
    __int(SIZE) bitwise_xor(__int(SIZE) a, __int(SIZE) b) {
        return a ^ b;
    }
    
    template<int SIZE>
    __uint(SIZE) bitwise_xor(__uint(SIZE) a, __uint(SIZE) b) {
        return a ^ b;
    }
    
    template<int SIZE>
    __int(SIZE) bitwise_not(__int(SIZE) a) {
        return ~a;
    }
    
    template<int SIZE>
    __uint(SIZE) bitwise_not(__uint(SIZE) a) {
        return ~a;
    }
    
    template<int SIZE>
    __int(SIZE) left_shift(__int(SIZE) a, int shift) {
        return a << shift;
    }
    
    template<int SIZE>
    __uint(SIZE) left_shift(__uint(SIZE) a, int shift) {
        return a << shift;
    }
    
    template<int SIZE>
    __int(SIZE) right_shift(__int(SIZE) a, int shift) {
        return a >> shift;
    }
    
    template<int SIZE>
    __uint(SIZE) right_shift(__uint(SIZE) a, int shift) {
        return a >> shift;
    }
    
    // ================================================
    // 最快排序算法(基数排序)
    // ================================================
    
    template<int SIZE>
    void fast_sort(IntArray<SIZE>& arr) {
        static_assert(SIZE == 8 || SIZE == 16 || SIZE == 32 || SIZE == 64, 
                     "排序仅支持标准整数类型");
        
        if (arr.size() <= 1) return;
        
        if constexpr (SIZE == 8 || SIZE == 16 || SIZE == 32) {
            std::sort(arr.begin(), arr.end());
        } else if constexpr (SIZE == 64) {
            if (arr.size() > 10000) {
                radix_sort_64_signed(arr);
            } else {
                std::sort(arr.begin(), arr.end());
            }
        }
    }
    
    template<int SIZE>
    void fast_sort(UIntArray<SIZE>& arr) {
        static_assert(SIZE == 8 || SIZE == 16 || SIZE == 32 || SIZE == 64, 
                     "排序仅支持标准整数类型");
        
        if (arr.size() <= 1) return;
        
        if constexpr (SIZE == 8 || SIZE == 16 || SIZE == 32) {
            std::sort(arr.begin(), arr.end());
        } else if constexpr (SIZE == 64) {
            if (arr.size() > 10000) {
                radix_sort_64_unsigned(arr);
            } else {
                std::sort(arr.begin(), arr.end());
            }
        }
    }
    
    // 64位有符号基数排序
    void radix_sort_64_signed(std::vector<int64_t>& arr) {
        std::vector<int64_t> positive, negative;
        
        // 分离正负数
        for (int64_t num : arr) {
            if (num >= 0) {
                positive.push_back(num);
            } else {
                negative.push_back(-num);
            }
        }
        
        // 分别排序
        radix_sort_64_unsigned(positive);
        radix_sort_64_unsigned(negative);
        
        // 合并结果
        size_t i = 0;
        std::reverse(negative.begin(), negative.end());
        for (int64_t num : negative) {
            arr[i++] = -num;
        }
        for (int64_t num : positive) {
            arr[i++] = num;
        }
    }
    
    // 64位无符号基数排序
    void radix_sort_64_unsigned(std::vector<uint64_t>& arr) {
        const int BITS = 8;
        const int RADIX = 1 << BITS;
        const int MASK = RADIX - 1;
        
        std::vector<uint64_t> temp(arr.size());
        std::vector<int> count(RADIX);
        
        for (int shift = 0; shift < 64; shift += BITS) {
            std::fill(count.begin(), count.end(), 0);
            
            for (uint64_t num : arr) {
                count[(num >> shift) & MASK]++;
            }
            
            for (int i = 1; i < RADIX; i++) {
                count[i] += count[i - 1];
            }
            
            for (int i = arr.size() - 1; i >= 0; i--) {
                uint64_t digit = (arr[i] >> shift) & MASK;
                temp[--count[digit]] = arr[i];
            }
            
            arr.swap(temp);
        }
    }
    
    // ================================================
    // 数组操作函数
    // ================================================
    
    // 创建指定大小的数组
    template<int SIZE>
    IntArray<SIZE> make_int_array(size_t size, __int(SIZE) init_value = 0) {
        return IntArray<SIZE>(size, init_value);
    }
    
    template<int SIZE>
    UIntArray<SIZE> make_uint_array(size_t size, __uint(SIZE) init_value = 0) {
        return UIntArray<SIZE>(size, init_value);
    }
    
    // 数组求和
    template<int SIZE>
    __int(SIZE) sum(const IntArray<SIZE>& arr) {
        __int(SIZE) result = 0;
        for (const auto& val : arr) {
            result = add<SIZE>(result, val);
        }
        return result;
    }
    
    template<int SIZE>
    __uint(SIZE) sum(const UIntArray<SIZE>& arr) {
        __uint(SIZE) result = 0;
        for (const auto& val : arr) {
            result = add<SIZE>(result, val);
        }
        return result;
    }
    
    // 数组最大值
    template<int SIZE>
    __int(SIZE) max(const IntArray<SIZE>& arr) {
        if (arr.empty()) throw std::runtime_error("数组为空");
        return *std::max_element(arr.begin(), arr.end());
    }
    
    template<int SIZE>
    __uint(SIZE) max(const UIntArray<SIZE>& arr) {
        if (arr.empty()) throw std::runtime_error("数组为空");
        return *std::max_element(arr.begin(), arr.end());
    }
    
    // 数组最小值
    template<int SIZE>
    __int(SIZE) min(const IntArray<SIZE>& arr) {
        if (arr.empty()) throw std::runtime_error("数组为空");
        return *std::min_element(arr.begin(), arr.end());
    }
    
    template<int SIZE>
    __uint(SIZE) min(const UIntArray<SIZE>& arr) {
        if (arr.empty()) throw std::runtime_error("数组为空");
        return *std::min_element(arr.begin(), arr.end());
    }
    
    // 数组平均值(仅适用于无符号整数)
    template<int SIZE>
    __uint(SIZE) average(const UIntArray<SIZE>& arr) {
        if (arr.empty()) throw std::runtime_error("数组为空");
        return divide<SIZE>(sum<SIZE>(arr), static_cast<__uint(SIZE)>(arr.size()));
    }
    
    // 数组查找
    template<int SIZE>
    size_t find(const IntArray<SIZE>& arr, __int(SIZE) value) {
        auto it = std::find(arr.begin(), arr.end(), value);
        return it != arr.end() ? std::distance(arr.begin(), it) : arr.size();
    }
    
    template<int SIZE>
    size_t find(const UIntArray<SIZE>& arr, __uint(SIZE) value) {
        auto it = std::find(arr.begin(), arr.end(), value);
        return it != arr.end() ? std::distance(arr.begin(), it) : arr.size();
    }
    
    // 数组填充
    template<int SIZE>
    void fill(IntArray<SIZE>& arr, __int(SIZE) value) {
        std::fill(arr.begin(), arr.end(), value);
    }
    
    template<int SIZE>
    void fill(UIntArray<SIZE>& arr, __uint(SIZE) value) {
        std::fill(arr.begin(), arr.end(), value);
    }
    
    // ================================================
    // 专属输入输出函数
    // ================================================
    
    // 128位整数输出辅助函数
#if HAS_INT128
    std::ostream& print_int128(std::ostream& os, int128_t value) {
        if (value == 0) {
            os << "0";
            return os;
        }
        
        if (value < 0) {
            os << "-";
            value = -value;
        }
        
        std::string result;
        while (value > 0) {
            result = char('0' + (value % 10)) + result;
            value /= 10;
        }
        os << result;
        return os;
    }
    
    std::ostream& print_uint128(std::ostream& os, uint128_t value) {
        if (value == 0) {
            os << "0";
            return os;
        }
        
        std::string result;
        while (value > 0) {
            result = char('0' + (value % 10)) + result;
            value /= 10;
        }
        os << result;
        return os;
    }
    
    std::istream& read_int128(std::istream& is, int128_t& value) {
        std::string str;
        is >> str;
        value = 0;
        bool negative = false;
        size_t i = 0;
        
        if (str[0] == '-') {
            negative = true;
            i = 1;
        }
        
        for (; i < str.length(); i++) {
            value = value * 10 + (str[i] - '0');
        }
        
        if (negative) value = -value;
        return is;
    }
    
    std::istream& read_uint128(std::istream& is, uint128_t& value) {
        std::string str;
        is >> str;
        value = 0;
        
        for (char c : str) {
            value = value * 10 + (c - '0');
        }
        return is;
    }
#endif
    
    // 通用输出函数
    template<int SIZE>
    std::ostream& print(std::ostream& os, __int(SIZE) value) {
        if constexpr (SIZE == 128) {
#if HAS_INT128
            return print_int128(os, value);
#else
            os << "128-bit integer (unsupported)";
            return os;
#endif
        } else {
            os << value;
            return os;
        }
    }
    
    template<int SIZE>
    std::ostream& print(std::ostream& os, __uint(SIZE) value) {
        if constexpr (SIZE == 128) {
#if HAS_INT128
            return print_uint128(os, value);
#else
            os << "128-bit unsigned integer (unsupported)";
            return os;
#endif
        } else {
            os << value;
            return os;
        }
    }
    
    // 数组输出函数
    template<int SIZE>
    std::ostream& print_array(std::ostream& os, const IntArray<SIZE>& arr) {
        os << "[";
        for (size_t i = 0; i < arr.size(); i++) {
            print<SIZE>(os, arr[i]);
            if (i < arr.size() - 1) os << ", ";
        }
        os << "]";
        return os;
    }
    
    template<int SIZE>
    std::ostream& print_array(std::ostream& os, const UIntArray<SIZE>& arr) {
        os << "[";
        for (size_t i = 0; i < arr.size(); i++) {
            print<SIZE>(os, arr[i]);
            if (i < arr.size() - 1) os << ", ";
        }
        os << "]";
        return os;
    }
    
    // 通用输入函数
    template<int SIZE>
    std::istream& read(std::istream& is, __int(SIZE)& value) {
        if constexpr (SIZE == 128) {
#if HAS_INT128
            return read_int128(is, value);
#else
            is.setstate(std::ios::failbit);
            return is;
#endif
        } else {
            is >> value;
            return is;
        }
    }
    
    template<int SIZE>
    std::istream& read(std::istream& is, __uint(SIZE)& value) {
        if constexpr (SIZE == 128) {
#if HAS_INT128
            return read_uint128(is, value);
#else
            is.setstate(std::ios::failbit);
            return is;
#endif
        } else {
            is >> value;
            return is;
        }
    }
}

// 重载运算符方便使用
template<int SIZE>
std::ostream& operator<<(std::ostream& os, __int(SIZE) value) {
    return IntMath::print<SIZE>(os, value);
}

template<int SIZE>
std::ostream& operator<<(std::ostream& os, __uint(SIZE) value) {
    return IntMath::print<SIZE>(os, value);
}

template<int SIZE>
std::istream& operator>>(std::istream& is, __int(SIZE)& value) {
    return IntMath::read<SIZE>(is, value);
}

template<int SIZE>
std::istream& operator>>(std::istream& is, __uint(SIZE)& value) {
    return IntMath::read<SIZE>(is, value);
}

// 数组输出运算符重载
template<int SIZE>
std::ostream& operator<<(std::ostream& os, const IntArray<SIZE>& arr) {
    return IntMath::print_array<SIZE>(os, arr);
}

template<int SIZE>
std::ostream& operator<<(std::ostream& os, const UIntArray<SIZE>& arr) {
    return IntMath::print_array<SIZE>(os, arr);
}

#endif // __INT_SIZE_MATH_LIBRARY_H__

🎯 使用方法示例

#include <iostream>
#include <vector>
#include <chrono>

// 包含上面的头文件...

void basic_operations_demo() {
    std::cout << "=== 基本运算演示 ===" << std::endl;
    
    // 有符号整数运算
    __int(32) a = 100;
    __int(32) b = 50;
    
    std::cout << "有符号 a = " << a << ", b = " << b << std::endl;
    std::cout << "a + b = " << IntMath::add<32>(a, b) << std::endl;
    std::cout << "a - b = " << IntMath::subtract<32>(a, b) << std::endl;
    
    // 无符号整数运算
    __uint(32) ua = 100;
    __uint(32) ub = 50;
    
    std::cout << "无符号 ua = " << ua << ", ub = " << ub << std::endl;
    std::cout << "ua * ub = " << IntMath::multiply<32>(ua, ub) << std::endl;
    std::cout << "ua / ub = " << IntMath::divide<32>(ua, ub) << std::endl;
}

void array_operations_demo() {
    std::cout << "\n=== 数组操作演示 ===" << std::endl;
    
    // 创建有符号整数数组
    auto int_arr = IntMath::make_int_array<32>(5, 10);
    std::cout << "初始有符号数组: " << int_arr << std::endl;
    
    // 修改数组元素
    int_arr[0] = 100;
    int_arr[1] = 50;
    int_arr[2] = 75;
    int_arr[3] = 25;
    int_arr[4] = 200;
    
    std::cout << "修改后数组: " << int_arr << std::endl;
    std::cout << "数组和: " << IntMath::sum<32>(int_arr) << std::endl;
    std::cout << "数组最大值: " << IntMath::max<32>(int_arr) << std::endl;
    std::cout << "数组最小值: " << IntMath::min<32>(int_arr) << std::endl;
    
    // 创建无符号整数数组
    auto uint_arr = IntMath::make_uint_array<32>(4, 0);
    uint_arr[0] = 1000;
    uint_arr[1] = 2000;
    uint_arr[2] = 500;
    uint_arr[3] = 1500;
    
    std::cout << "无符号数组: " << uint_arr << std::endl;
    std::cout << "无符号数组平均值: " << IntMath::average<32>(uint_arr) << std::endl;
    
    // 数组排序
    IntMath::fast_sort<32>(int_arr);
    std::cout << "排序后有符号数组: " << int_arr << std::endl;
    
    IntMath::fast_sort<32>(uint_arr);
    std::cout << "排序后无符号数组: " << uint_arr << std::endl;
}

void sqrt_demo() {
    std::cout << "\n=== 快速平方根演示 ===" << std::endl;
    
    __uint(32) num = 144;
    __uint(32) sqrt_result = IntMath::sqrt_int<32>(num);
    std::cout << "sqrt(" << num << ") = " << sqrt_result << std::endl;
    
    __uint(64) big_num = 1000000000000ULL;
    __uint(64) big_sqrt = IntMath::sqrt_int<64>(big_num);
    std::cout << "sqrt(" << big_num << ") = " << big_sqrt << std::endl;
    
    // 无符号平方根
    __uint(32) u_num = 256;
    __uint(32) u_sqrt_result = IntMath::sqrt_int<32>(u_num);
    std::cout << "sqrt(" << u_num << ") = " << u_sqrt_result << std::endl;
}

void bitwise_demo() {
    std::cout << "\n=== 位运算演示 ===" << std::endl;
    
    __uint(32) a = 12;  // 1100
    __uint(32) b = 10;  // 1010
    
    std::cout << "a = " << a << ", b = " << b << std::endl;
    std::cout << "a & b = " << IntMath::bitwise_and<32>(a, b) << std::endl;
    std::cout << "a | b = " << IntMath::bitwise_or<32>(a, b) << std::endl;
    std::cout << "a ^ b = " << IntMath::bitwise_xor<32>(a, b) << std::endl;
    std::cout << "~a = " << IntMath::bitwise_not<32>(a) << std::endl;
    std::cout << "a << 2 = " << IntMath::left_shift<32>(a, 2) << std::endl;
    std::cout << "a >> 1 = " << IntMath::right_shift<32>(a, 1) << std::endl;
}

void io_demo() {
    std::cout << "\n=== 输入输出演示 ===" << std::endl;
    
    // 输出演示
    __int(32) num32 = 123456789;
    __uint(64) num64 = 9876543210ULL;
    
    std::cout << "32位整数: " << num32 << std::endl;
    std::cout << "64位无符号整数: " << num64 << std::endl;
    
#if HAS_INT128
    __int(128) num128 = 123456789012345678901234567890LL;
    std::cout << "128位整数: " << num128 << std::endl;
#endif
    
    // 数组输出
    auto arr = IntMath::make_int_array<32>(3);
    arr[0] = 1;
    arr[1] = 2;
    arr[2] = 3;
    std::cout << "数组输出: " << arr << std::endl;
}

void performance_test() {
    std::cout << "\n=== 性能测试 ===" << std::endl;
    
    // 测试大量数据的排序性能
    const size_t N = 100000;
    auto data = IntMath::make_int_array<64>(N);
    
    // 生成测试数据
    for (size_t i = 0; i < N; i++) {
        data[i] = static_cast<__int(64)>(N - i);
    }
    
    auto start = std::chrono::high_resolution_clock::now();
    IntMath::fast_sort<64>(data);
    auto end = std::chrono::high_resolution_clock::now();
    
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
    std::cout << "排序 " << N << " 个64位整数用时: " << duration.count() << " 毫秒" << std::endl;
    
    // 测试数组求和性能
    const int sum_tests = 1000000;
    auto test_arr = IntMath::make_uint_array<32>(sum_tests);
    for (int i = 0; i < sum_tests; i++) {
        test_arr[i] = i % 1000;
    }
    
    start = std::chrono::high_resolution_clock::now();
    volatile __uint(32) result = IntMath::sum<32>(test_arr);
    end = std::chrono::high_resolution_clock::now();
    
    duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
    std::cout << "计算 " << sum_tests << " 个元素的和用时: " << duration.count() << " 毫秒" << std::endl;
}

void advanced_usage() {
    std::cout << "\n=== 高级使用示例 ===" << std::endl;
    
    // 模板函数使用
    auto process_integers = []<int SIZE>(__int(SIZE) a, __int(SIZE) b) {
        std::cout << "SIZE " << SIZE << " 有符号运算:" << std::endl;
        std::cout << "  加法: " << IntMath::add<SIZE>(a, b) << std::endl;
        std::cout << "  乘法: " << IntMath::multiply<SIZE>(a, b) << std::endl;
    };
    
    auto process_uintegers = []<int SIZE>(__uint(SIZE) a, __uint(SIZE) b) {
        std::cout << "SIZE " << SIZE << " 无符号运算:" << std::endl;
        std::cout << "  加法: " << IntMath::add<SIZE>(a, b) << std::endl;
        std::cout << "  平方根: " << IntMath::sqrt_int<SIZE>(a) << std::endl;
    };
    
    process_integers.operator()<32>(144, 25);
    process_uintegers.operator()<64>(1000000ULL, 2000000ULL);
    
    // 静态数组使用
    StaticIntArray<32, 5> static_arr = {5, 2, 8, 1, 9};
    std::cout << "静态数组: ";
    for (const auto& x : static_arr) std::cout << x << " ";
    std::cout << std::endl;
    
    // 动态数组查找
    auto dynamic_arr = IntMath::make_int_array<32>(5);
    IntMath::fill<32>(dynamic_arr, 42);
    dynamic_arr[2] = 100;
    
    size_t index = IntMath::find<32>(dynamic_arr, 100);
    std::cout << "值100在数组中的位置: " << index << std::endl;
}

int main() {
    std::cout << "🚀 __int(SIZE) 完整数学运算库演示" << std::endl;
    std::cout << "支持位数: 8, 16, 32, 64" << (HAS_INT128 ? ", 128" : "") << std::endl;
    
    try {
        basic_operations_demo();
        array_operations_demo();
        sqrt_demo();
        bitwise_demo();
        io_demo();
        performance_test();
        advanced_usage();
        
        std::cout << "\n✅ 所有演示完成!" << std::endl;
        
    } catch (const std::exception& e) {
        std::cerr << "错误: " << e.what() << std::endl;
        return 1;
    }
    
    return 0;
}

🛠️ 完整功能列表

1. 有符号和无符号整数支持

__int(32) signed_num = -100;    // 有符号32位整数
__uint(32) unsigned_num = 100;  // 无符号32位整数

2. 数组类型支持

// 动态数组
IntArray<32> int_arr = IntMath::make_int_array<32>(10, 0);  // 10个元素,初始值0
UIntArray<64> uint_arr = IntMath::make_uint_array<64>(5, 42); // 5个元素,初始值42

// 静态数组
StaticIntArray<32, 10> static_int_arr;  // 固定大小10的有符号数组
StaticUIntArray<64, 5> static_uint_arr; // 固定大小5的无符号数组

3. 基本算术运算

auto result1 = IntMath::add<32>(a, b);        // 有符号加法
auto result2 = IntMath::add<32>(ua, ub);      // 无符号加法
auto result3 = IntMath::multiply<32>(a, b);   // 乘法
auto result4 = IntMath::divide<32>(a, b);     // 除法

4. 快速平方根

__uint(32) sqrt_result = IntMath::sqrt_int<32>(144);  // 有符号平方根
__uint(32) u_sqrt_result = IntMath::sqrt_int<32>(144U); // 无符号平方根

5. 位运算

auto and_result = IntMath::bitwise_and<32>(a, b);     // 按位与
auto or_result = IntMath::bitwise_or<32>(a, b);       // 按位或
auto shift_result = IntMath::left_shift<32>(a, 2);    // 左移

6. 最快排序

IntMath::fast_sort<32>(int_arr);   // 有符号数组排序
IntMath::fast_sort<32>(uint_arr);  // 无符号数组排序

7. 数组操作

auto sum_result = IntMath::sum<32>(arr);        // 数组求和
auto max_result = IntMath::max<32>(arr);        // 数组最大值
auto avg_result = IntMath::average<32>(uint_arr); // 无符号数组平均值
IntMath::fill<32>(arr, 42);                     // 数组填充
size_t index = IntMath::find<32>(arr, 100);     // 数组查找

8. 专属输入输出

std::cout << int_arr << std::endl;  // 数组输出
std::cout << big_num << std::endl;  // 128位整数输出
std::cin >> big_num;                // 128位整数输入

🔚 总结

这个 __int(SIZE) 系统提供了:

  1. 完整的有符号和无符号整数支持:8、16、32、64位(128位可选)
  2. 动态和静态数组支持:IntArray、UIntArray、StaticIntArray、StaticUIntArray
  3. 完整的数学运算:+、-、*、/、%、位运算
  4. 最快的平方根计算:使用硬件SSE指令
  5. 最优排序算法:基数排序,线性时间复杂度
  6. 丰富的数组操作:求和、最值、平均值、查找、填充
  7. 专属IO函数:支持128位整数和数组的输入输出
  8. 类型安全:编译时检查和模板约束
  9. 高性能:针对不同位数优化实现

使用方法极其简单:

__int(32) a = 100;
auto arr = IntMath::make_int_array<32>(1000);  // 创建1000个元素的数组
IntMath::fast_sort<32>(arr);                   // 最快排序
auto result = IntMath::sqrt_int<32>(a);        // 快速平方根

分享这篇文章