unsorted C snippets for small/fast static apps

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#61 Post by technosaurus »

Some practice porting C code to use vector extensions (from https://github.com/easyaspi314/xxhash-c ... sh32-ref.c)

Code: Select all

#include <stddef.h> /* size_t, NULL */
#include <stdint.h> /* uint8_t, uint32_t */
typedef uint32_t __attribute__ ((__vector_size__ (16))) u32x4t;
#define PRIME32_1 0x9E3779B1U  /* 0b10011110001101110111100110110001 */
#define PRIME32_2 0x85EBCA77U  /* 0b10000101111010111100101001110111 */
#define PRIME32_3 0xC2B2AE3DU  /* 0b11000010101100101010111000111101 */
#define PRIME32_4 0x27D4EB2FU  /* 0b00100111110101001110101100101111 */
#define PRIME32_5 0x165667B1U  /* 0b00010110010101100110011110110001 */
#define XXH_rotl32(v, a) ((v << a) | (v >> (32 - a)))

uint32_t XXH32(void const * input, size_t  length, uint32_t const seed){
	uint8_t const *data = (uint8_t const *) input;
    uint32_t const *udata;
    uint32_t hash = seed + PRIME32_5;
	u32x4t const * u32x4data = (u32x4t const *) input;
	size_t remaining = length;

	if (input != NULL){ /* Don't dereference a null pointer.*/
		if (remaining >= 16) {
			u32x4t lanes = { PRIME32_1+PRIME32_2,PRIME32_2,0,-PRIME32_1 };
			lanes += seed;
			while (remaining >= 16) {
				lanes += *u32x4data++ * PRIME32_2;
				lanes = XXH_rotl32(lanes, 13);
				lanes *= PRIME32_1;
				remaining -= 16;
			}
			lanes = XXH_rotl32(lanes, ((u32x4t){1,7,12,18}));
			hash = lanes[0]+lanes[1]+lanes[2]+lanes[3];
		} else { /* Not enough data for main loop, put something in there instead.*/
			hash = seed + PRIME32_5;
		}
		hash += (uint32_t) length;
		/* Process the remaining data. */
		udata = (uint32_t const *) u32x4data;
		while (remaining >= 4) {
			hash += *udata++ * PRIME32_3;
			hash  = XXH_rotl32(hash, 17);
			hash *= PRIME32_4;
			remaining -= 4;
		}
		data = (uint8_t const *)udata;
		while (remaining != 0) {
			hash += (uint32_t) *data++ * PRIME32_5;
			hash  = XXH_rotl32(hash, 11);
			hash *= PRIME32_1;
			--remaining;
		}
	}
	hash ^= hash >> 15;
	hash *= PRIME32_2;
	hash ^= hash >> 13;
	hash *= PRIME32_3;
	hash ^= hash >> 16;
	return hash;
}

  
/*
 *  xxHash - Fast Hash algorithm
 *  Copyright (C) 2012-2019, Yann Collet
 *  Copyright (C) 2019, easyaspi314 (Devin)
 *
 *  BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are
 *  met:
 *
 *  * Redistributions of source code must retain the above copyright
 *  notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above
 *  copyright notice, this list of conditions and the following disclaimer
 *  in the documentation and/or other materials provided with the
 *  distribution.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *  You can contact the author at :
 *  - xxHash homepage: http://www.xxhash.com
 *  - xxHash source repository : https://github.com/Cyan4973/xxHash */
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

Post Reply