<commit_before>#include "sponge.h"
#include "lyra2.h"
#include "static_assert.h"

#include <string.h>
#include <immintrin.h>
#include <assert.h>

#ifdef __WORDSIZE
#define W (__WORDSIZE)
#else
#define W (sizeof(void *) * CHAR_BIT)
#endif

typedef sponge_word_t bword_t;
STATIC_ASSERT(SPONGE_EXTENDED_RATE_SIZE_BYTES % sizeof(bword_t) == 0, L_divides_sponge_extended_rate);
STATIC_ASSERT(sizeof(bword_t) % W, L_is_a_multiple_of_the_word_size);
#define nbwords (SPONGE_EXTENDED_RATE_SIZE_BYTES / sizeof(bword_t))
typedef bword_t block_t[nbwords];

#define GEN_BLOCK_OPERATION(name, expr, ...)                                          \
static inline void                                                                    \
block_##name(block_t bdst, const block_t bsrc1, const block_t bsrc2, ##__VA_ARGS__) { \
    for (unsigned int i = 0; i < nbwords; i++) {                                      \
        expr;                                                                         \
    }                                                                                 \
}

GEN_BLOCK_OPERATION(xor, bdst[i] = bsrc1[i] ^ bsrc2[i])
GEN_BLOCK_OPERATION(wordwise_add, bdst[i] = bsrc1[i] + bsrc2[i]);
GEN_BLOCK_OPERATION(xor_rotL, bdst[i] = bsrc1[i] ^ bsrc2[(i+nbwords-rot) % nbwords], unsigned int rot)

STATIC_ASSERT((W & (W - 1)) == 0, word_size_is_a_power_of_two); // be safe when doing & (W - 1)
STATIC_ASSERT(W <= 64, word_is_no_greater_than_64_bits);

static inline uint64_t
block_get_msw_from_bword(const block_t block, unsigned int bwordidx) {
    unsigned int wordidx = sizeof(bword_t) / sizeof(uint64_t) - 1;
    return *(((uint64_t *) &block[bwordidx]) + wordidx) % (W - 1);
}

static inline uint64_t
block_get_lsw_from_bword(const block_t block, unsigned int bwordidx) {
    return *((uint64_t *) &block[bwordidx]) % (W - 1);
}

static inline void
write_basil(uint8_t *buf, uint32_t keylen, const char *pwd,
            uint32_t pwdlen, const char *salt, uint32_t saltlen,
            uint32_t R, uint32_t C, uint32_t T) {
#define WRITE_PTR(ptr, size) \
    memcpy(buf, ptr, size);  \
    buf += size;

#define WRITE_VALUE(value)              \
    memcpy(buf, &value, sizeof(value)); \
    buf += sizeof(value);<commit_msg>Fix word getters from block_t.<commit_after>#include "sponge.h"
#include "lyra2.h"
#include "static_assert.h"

#include <string.h>
#include <immintrin.h>
#include <assert.h>

#ifdef __WORDSIZE
#define W (__WORDSIZE)
#else
#define W (sizeof(void *) * CHAR_BIT)
#endif

typedef sponge_word_t bword_t;
STATIC_ASSERT(SPONGE_EXTENDED_RATE_SIZE_BYTES % sizeof(bword_t) == 0, L_divides_sponge_extended_rate);
STATIC_ASSERT(sizeof(bword_t) % W, L_is_a_multiple_of_the_word_size);
#define nbwords (SPONGE_EXTENDED_RATE_SIZE_BYTES / sizeof(bword_t))
typedef bword_t block_t[nbwords];

#define GEN_BLOCK_OPERATION(name, expr, ...)                                          \
static inline void                                                                    \
block_##name(block_t bdst, const block_t bsrc1, const block_t bsrc2, ##__VA_ARGS__) { \
    for (unsigned int i = 0; i < nbwords; i++) {                                      \
        expr;                                                                         \
    }                                                                                 \
}

GEN_BLOCK_OPERATION(xor, bdst[i] = bsrc1[i] ^ bsrc2[i])
GEN_BLOCK_OPERATION(wordwise_add, bdst[i] = bsrc1[i] + bsrc2[i]);
GEN_BLOCK_OPERATION(xor_rotL, bdst[i] = bsrc1[i] ^ bsrc2[(i+nbwords-rot) % nbwords], unsigned int rot)

STATIC_ASSERT((W & (W - 1)) == 0, word_size_is_a_power_of_two); // be safe when doing & (W - 1)
STATIC_ASSERT(W <= 64, word_is_no_greater_than_64_bits);
static const uint64_t word_mod_mask = ~0 >> (64 - W);

static inline uint64_t
block_get_msw_from_bword(const block_t block, unsigned int bwordidx) {
    unsigned int wordidx = sizeof(bword_t) / sizeof(uint64_t) - 1;
    return *(((uint64_t *) &block[bwordidx]) + wordidx) & word_mod_mask;
}

static inline uint64_t
block_get_lsw_from_bword(const block_t block, unsigned int bwordidx) {
    return *((uint64_t *) &block[bwordidx]) & word_mod_mask;
}

static inline void
write_basil(uint8_t *buf, uint32_t keylen, const char *pwd,
            uint32_t pwdlen, const char *salt, uint32_t saltlen,
            uint32_t R, uint32_t C, uint32_t T) {
#define WRITE_PTR(ptr, size) \
    memcpy(buf, ptr, size);  \
    buf += size;

#define WRITE_VALUE(value)              \
    memcpy(buf, &value, sizeof(value)); \
    buf += sizeof(value);
{'fraction_non_alphanumeric': 0.11530858774434438, 'fraction_numerical': 0.019986821875686362, 'mean_word_length': 2.5384615384615383, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4838511', 'n_tokens_mistral': 1669, 'n_tokens_neox': 1584, 'n_words': 342}