/* This file is part of limb https://lila.oss/limb
* Copyright (C) 2023 Olivier Brunel jjk@jjacky.com */
/* Based on RHash: http://rhash.sourceforge.net/
* Copyright (c) 2008 Aleksey Kravchenko */
/* SPDX-License-Identifier: 0BSD */
#include "sha3/byte_order.h"
#ifndef rhash_ctz
/**
* Returns index of the trailing bit of a 32-bit number.
* This is a plain C equivalent for GCC __builtin_ctz() bit scan.
*
* @param x the number to process
* @return zero-based index of the trailing bit
*/
unsigned rhash_ctz(unsigned x)
{
/* array for conversion to bit position */
static unsigned char bit_pos[32] = {
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
/* The De Bruijn bit-scan was devised in 1997, according to Donald Knuth
* by Martin Lauter. The constant 0x077CB531UL is a De Bruijn sequence,
* which produces a unique pattern of bits into the high 5 bits for each
* possible bit position that it is multiplied against.
* See http://graphics.stanford.edu/~seander/bithacks.html
* and http://chessprogramming.wikispaces.com/BitScan */
return (unsigned)bit_pos[((u32)((x & -x) * 0x077CB531U)) >> 27];
}
#endif /* rhash_ctz */
/**
* Copy a memory block with simultaneous exchanging byte order.
* The byte order is changed from little-endian 32-bit integers
* to big-endian (or vice-versa).
*
* @param to the pointer where to copy memory block
* @param index the index to start writing from
* @param from the source block to copy
* @param length length of the memory block
*/
void rhash_swap_copy_str_to_u32(void *to, int index, const void *from, size_t length)
{
/* if all pointers and length are 32-bits aligned */
if ( 0 == (( (uintptr_t) to | (uintptr_t) from | (uintptr_t) index | length ) & 3) ) {
/* copy memory as 32-bit words */
const u32 *src = (const u32 *) from;
const u32 *end = (const u32 *) ((const char *) src + length);
u32 *dst = (u32 *) ((char *) to + index);
for ( ; src < end; ++dst, ++src)
*dst = bswap_32(*src);
} else {
const char *src = (const char *) from;
for (length += index; (size_t) index < length; ++index)
((char *) to)[index ^ 3] = *(src++);
}
}
/**
* Copy a memory block with changed byte order.
* The byte order is changed from little-endian 64-bit integers
* to big-endian (or vice-versa).
*
* @param to the pointer where to copy memory block
* @param index the index to start writing from
* @param from the source block to copy
* @param length length of the memory block
*/
void rhash_swap_copy_str_to_u64(void *to, int index, const void *from, size_t length)
{
/* if all pointers and length are 64-bits aligned */
if ( 0 == (( (uintptr_t) to | (uintptr_t) from | (uintptr_t) index | length ) & 7) ) {
/* copy aligned memory block as 64-bit integers */
const u64 *src = (const u64 *) from;
const u64 *end = (const u64 *) ((const char* )src + length);
u64 *dst = (u64 *) ((char *) to + index);
while (src < end)
*(dst++) = bswap_64( *(src++) );
} else {
const char *src = (const char *) from;
for (length += index; (size_t) index < length; ++index)
((char *) to)[index ^ 7] = *(src++);
}
}
/**
* Copy data from a sequence of 64-bit words to a binary string of given length,
* while changing byte order.
*
* @param to the binary string to receive data
* @param from the source sequence of 64-bit words
* @param length the size in bytes of the data being copied
*/
void rhash_swap_copy_u64_to_str(void *to, const void *from, size_t length)
{
/* if all pointers and length are 64-bits aligned */
if ( 0 == (( (uintptr_t) to | (uintptr_t) from | length ) & 7) ) {
/* copy aligned memory block as 64-bit integers */
const u64 *src = (const u64 *) from;
const u64 *end = (const u64 *) ((const char *) src + length);
u64 *dst = (u64 *) to;
while (src < end)
*(dst++) = bswap_64( *(src++) );
} else {
size_t index;
char *dst = (char *) to;
for (index = 0; index < length; ++index)
*(dst++) = ((char *) from)[index ^ 7];
}
}
/**
* Exchange byte order in the given array of 32-bit integers.
*
* @param arr the array to process
* @param length array length
*/
void rhash_u32_mem_swap(unsigned *arr, int length)
{
unsigned *end = arr + length;
for ( ; arr < end; ++arr) {
*arr = bswap_32(*arr);
}
}
#ifdef HAS_INTEL_CPUID
#include <cpuid.h>
static u64 get_cpuid_features(void)
{
u32 tmp, edx, ecx;
if (__get_cpuid(1, &tmp, &tmp, &ecx, &edx))
return ((((u64)ecx) << 32) ^ edx);
return 0;
}
int has_cpu_feature(unsigned feature_bit)
{
static u64 features;
const u64 feature = ((u64)1) << feature_bit;
if (!features)
features = (get_cpuid_features() | 1);
return !!(features & feature);
}
#endif