<commit_before>#include <immintrin.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include "constants.h"
#include "utilities.h"
#include "prime_field_arithmetic.h"

unsigned int add(uint64_t * const c, uint64_t const * const a, uint64_t const * const b, unsigned int const num_limbs, unsigned int const carry_in) {
    unsigned int carry_out = carry_in;

    for (unsigned int i = 0; i < num_limbs; i++) {
        // we need to temporarily store the output of each operation, because it is
        // possible that c is the same array as a or b.
        uint64_t c_tmp = 0;

#if BASE_2_63_REPRESENTATION
        c_tmp = a[i] + b[i] + carry_out;
        carry_out = (unsigned int) (c_tmp >> BASE_EXPONENT);
        c_tmp &= ~((uint64_t) 1 << BASE_EXPONENT);
        c[i] = c_tmp;
#else
        c_tmp = a[i] + carry_out;
        carry_out = (c_tmp < a[i]);
        c_tmp += b[i];
        carry_out |= (c_tmp < b[i]);
        c[i] = c_tmp;
#endif
    }

    return carry_out;
}

unsigned int add_num_limb(uint64_t * const c, uint64_t const * const a, uint64_t const b, unsigned int const num_limbs, unsigned int const carry_in) {
    // we need to temporarily store the output of each operation, because it is
    // possible that c is the same array as a.
    uint64_t c_tmp = 0;
    unsigned int carry_out = 0;

#if BASE_2_63_REPRESENTATION
    c_tmp = a[0] + b + carry_in;
    carry_out = (unsigned int) (c_tmp >> BASE_EXPONENT);
    c_tmp &= ~((uint64_t) 1 << BASE_EXPONENT);
    c[0] = c_tmp;
#else
    c_tmp = a[0] + carry_in;
    carry_out = (c_tmp < a[0]);
    c_tmp += b;
    carry_out |= (c_tmp < b);
    c[0] = c_tmp;
#endif

    for (unsigned int i = 1; i < num_limbs; i++) {
#if BASE_2_63_REPRESENTATION
        c_tmp = a[i] + carry_out;
        carry_out = (unsigned int) (c_tmp >> BASE_EXPONENT);
        c_tmp &= ~((uint64_t) 1 << BASE_EXPONENT);
        c[i] = c_tmp;
#else
        c_tmp = a[i] + carry_out;
        carry_out = (c_tmp < a[i]);
        c[i] = c_tmp;
#endif
    }

    return carry_out;
}

unsigned int sub(uint64_t * const c, uint64_t const * const a, uint64_t const * const b, unsigned int const num_limbs, unsigned int const borrow_in) {
    unsigned int borrow_out = borrow_in;

    for (unsigned int i = 0; i < num_limbs; i++) {
#if BASE_2_63_REPRESENTATION
        uint64_t c_tmp = 0;
        c_tmp = a[i] - b[i] - borrow_out;
        borrow_out = (unsigned int) (c_tmp >> BASE_EXPONENT);
        c_tmp &= ~((uint64_t) 1 << BASE_EXPONENT);
        c[i] = c_tmp;
#else
        // we need to temporarily store the output of each operation, because it is
        // possible that c is the same array as a or b.
        uint64_t c_tmp = 0;
        uint64_t c_tmp_old = 0;

        c_tmp = a[i] - borrow_out;
        c_tmp_old = c_tmp;
        borrow_out = (c_tmp > a[i]);
        c_tmp -= b[i];
        borrow_out |= (c_tmp > c_tmp_old);
        c[i] = c_tmp;
#endif
    }

    return borrow_out;
}

void mul64_to_128(uint64_t * const c_hi, uint64_t * const c_lo, uint64_t const a, uint64_t const b) {
#if MULX
    unsigned long long x = a;
    unsigned long long y = b;
    unsigned long long hi;
    unsigned long long lo;
    lo = _mulx_u64(x, y, &hi);
    *c_hi = hi;<commit_msg>Sub() common variable refactoring.<commit_after>#include <immintrin.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include "constants.h"
#include "utilities.h"
#include "prime_field_arithmetic.h"

unsigned int add(uint64_t * const c, uint64_t const * const a, uint64_t const * const b, unsigned int const num_limbs, unsigned int const carry_in) {
    unsigned int carry_out = carry_in;

    for (unsigned int i = 0; i < num_limbs; i++) {
        // we need to temporarily store the output of each operation, because it is
        // possible that c is the same array as a or b.
        uint64_t c_tmp = 0;

#if BASE_2_63_REPRESENTATION
        c_tmp = a[i] + b[i] + carry_out;
        carry_out = (unsigned int) (c_tmp >> BASE_EXPONENT);
        c_tmp &= ~((uint64_t) 1 << BASE_EXPONENT);
        c[i] = c_tmp;
#else
        c_tmp = a[i] + carry_out;
        carry_out = (c_tmp < a[i]);
        c_tmp += b[i];
        carry_out |= (c_tmp < b[i]);
        c[i] = c_tmp;
#endif
    }

    return carry_out;
}

unsigned int add_num_limb(uint64_t * const c, uint64_t const * const a, uint64_t const b, unsigned int const num_limbs, unsigned int const carry_in) {
    // we need to temporarily store the output of each operation, because it is
    // possible that c is the same array as a.
    uint64_t c_tmp = 0;
    unsigned int carry_out = 0;

#if BASE_2_63_REPRESENTATION
    c_tmp = a[0] + b + carry_in;
    carry_out = (unsigned int) (c_tmp >> BASE_EXPONENT);
    c_tmp &= ~((uint64_t) 1 << BASE_EXPONENT);
    c[0] = c_tmp;
#else
    c_tmp = a[0] + carry_in;
    carry_out = (c_tmp < a[0]);
    c_tmp += b;
    carry_out |= (c_tmp < b);
    c[0] = c_tmp;
#endif

    for (unsigned int i = 1; i < num_limbs; i++) {
#if BASE_2_63_REPRESENTATION
        c_tmp = a[i] + carry_out;
        carry_out = (unsigned int) (c_tmp >> BASE_EXPONENT);
        c_tmp &= ~((uint64_t) 1 << BASE_EXPONENT);
        c[i] = c_tmp;
#else
        c_tmp = a[i] + carry_out;
        carry_out = (c_tmp < a[i]);
        c[i] = c_tmp;
#endif
    }

    return carry_out;
}

unsigned int sub(uint64_t * const c, uint64_t const * const a, uint64_t const * const b, unsigned int const num_limbs, unsigned int const borrow_in) {
    // we need to temporarily store the output of each operation, because it is
    // possible that c is the same array as a or b.
    uint64_t c_tmp = 0;
    unsigned int borrow_out = borrow_in;

    for (unsigned int i = 0; i < num_limbs; i++) {
#if BASE_2_63_REPRESENTATION
        c_tmp = a[i] - b[i] - borrow_out;
        borrow_out = (unsigned int) (c_tmp >> BASE_EXPONENT);
        c_tmp &= ~((uint64_t) 1 << BASE_EXPONENT);
        c[i] = c_tmp;
#else
        uint64_t c_tmp_old = 0;

        c_tmp = a[i] - borrow_out;
        c_tmp_old = c_tmp;
        borrow_out = (c_tmp > a[i]);
        c_tmp -= b[i];
        borrow_out |= (c_tmp > c_tmp_old);
        c[i] = c_tmp;
#endif
    }

    return borrow_out;
}

void mul64_to_128(uint64_t * const c_hi, uint64_t * const c_lo, uint64_t const a, uint64_t const b) {
#if MULX
    unsigned long long x = a;
    unsigned long long y = b;
    unsigned long long hi;
    unsigned long long lo;
    lo = _mulx_u64(x, y, &hi);
    *c_hi = hi;
{'fraction_non_alphanumeric': 0.11842309483160018, 'fraction_numerical': 0.02467794505665063, 'mean_word_length': 2.602012297372834, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3481555', 'n_tokens_mistral': 2585, 'n_tokens_neox': 2497, 'n_words': 731}