Repository: valkey-io/valkey
File: src/util.c (C)

Code before:
    uint32_t next = length - 1;
    dst[next + 1] = '\0';
    while (value >= 100) {
        int const i = (value % 100) * 2;
        value /= 100;
        dst[next] = digits[i + 1];
        dst[next - 1] = digits[i];
        next -= 2;
    }

    /* Handle last 1-2 digits. */
    if (value < 10) {
        dst[next] = '0' + (uint32_t)value;
    } else {
        int i = (uint32_t)value * 2;
        dst[next] = digits[i + 1];
        dst[next - 1] = digits[i];
    }
    return length;
err:
    /* force add Null termination */
    if (dstlen > 0) dst[0] = '\0';
    return 0;
}

#if HAVE_AVX512
#include <immintrin.h>

#define MULTIPLIER_10E8 100000000
#define MULTIPLIER_10E16 10000000000000000ULL

/**
 * Convert a string into an signed 64-bit integer using AVX-512 instructions.
 *
 * This function parses a string of digits and converts it into an signed
 * 64-bit integer. It leverages AVX-512 SIMD instructions for optimized
 * processing and performs strict validation to ensure the input string
 * represents a valid signed integer.
 *
 * Notes:
 * - The input string must not contain leading zeros unless it is "0".
 * - The function checks for overflow and returns 0 if the result exceeds
 *   the maximum value of LLONG_MAX.
 * - This function requires AVX-512 support and will only be compiled if
 *   `HAVE_AVX512` is defined.
 *
 * Example:
 * Input: s = "1234567890", slen = 10
 * Steps:
 * 1. Load the string into SIMD registers and subtract '0' from each character
 *    to convert ASCII digits to integers.

Review comment (suggestion):
This macro is undefined. It was renamed. (`-Wundef` helped me find it.)

Code after:
    uint32_t next = length - 1;
    dst[next + 1] = '\0';
    while (value >= 100) {
        int const i = (value % 100) * 2;
        value /= 100;
        dst[next] = digits[i + 1];
        dst[next - 1] = digits[i];
        next -= 2;
    }

    /* Handle last 1-2 digits. */
    if (value < 10) {
        dst[next] = '0' + (uint32_t)value;
    } else {
        int i = (uint32_t)value * 2;
        dst[next] = digits[i + 1];
        dst[next - 1] = digits[i];
    }
    return length;
err:
    /* force add Null termination */
    if (dstlen > 0) dst[0] = '\0';
    return 0;
}

#if HAVE_X86_SIMD
#include <immintrin.h>

#define MULTIPLIER_10E8 100000000
#define MULTIPLIER_10E16 10000000000000000ULL

/**
 * Convert a string into an signed 64-bit integer using AVX-512 instructions.
 *
 * This function parses a string of digits and converts it into an signed
 * 64-bit integer. It leverages AVX-512 SIMD instructions for optimized
 * processing and performs strict validation to ensure the input string
 * represents a valid signed integer.
 *
 * Notes:
 * - The input string must not contain leading zeros unless it is "0".
 * - The function checks for overflow and returns 0 if the result exceeds
 *   the maximum value of LLONG_MAX.
 * - This function requires AVX-512 support and will only be compiled if
 *   `HAVE_X86_SIMD` is defined.
 *
 * Example:
 * Input: s = "1234567890", slen = 10
 * Steps:
 * 1. Load the string into SIMD registers and subtract '0' from each character
 *    to convert ASCII digits to integers.