Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Sun 15 Dec 2019, 20:18
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
unsorted C snippets for small/fast static apps
Post new topic   Reply to topic View previous topic :: View next topic
Page 5 of 5 [61 Posts]   Goto page: Previous 1, 2, 3, 4, 5
Author Message
technosaurus


Joined: 18 May 2008
Posts: 4872
Location: Blue Springs, MO

PostPosted: Mon 23 Sep 2019, 02:54    Post subject:  

Some practice porting C code to use vector extensions (from https://github.com/easyaspi314/xxhash-clean/blob/master/xxhash32-ref.c)
Code:
#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 github repositories. I may eventually get around to updating my blogspot.
Back to top
View user's profile Send private message Visit poster's website 
Display posts from previous:   Sort by:   
Page 5 of 5 [61 Posts]   Goto page: Previous 1, 2, 3, 4, 5
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0532s ][ Queries: 12 (0.0250s) ][ GZIP on ]