1309260Scognet/* 2309260Scognet * Copyright 2011-2015 Samy Al Bahra. 3309260Scognet * Copyright 2011 David Joseph. 4309260Scognet * All rights reserved. 5309260Scognet * 6309260Scognet * Redistribution and use in source and binary forms, with or without 7309260Scognet * modification, are permitted provided that the following conditions 8309260Scognet * are met: 9309260Scognet * 1. Redistributions of source code must retain the above copyright 10309260Scognet * notice, this list of conditions and the following disclaimer. 11309260Scognet * 2. Redistributions in binary form must reproduce the above copyright 12309260Scognet * notice, this list of conditions and the following disclaimer in the 13309260Scognet * documentation and/or other materials provided with the distribution. 14309260Scognet * 15309260Scognet * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16309260Scognet * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17309260Scognet * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18309260Scognet * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19309260Scognet * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20309260Scognet * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21309260Scognet * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22309260Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23309260Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24309260Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25309260Scognet * SUCH DAMAGE. 26309260Scognet */ 27309260Scognet 28309260Scognet/* 29309260Scognet * Several of these are from: http://graphics.stanford.edu/~seander/bithacks.html 30309260Scognet */ 31309260Scognet 32309260Scognet#define CK_INTERNAL_LOG_0 (0xAAAAAAAA) 33309260Scognet#define CK_INTERNAL_LOG_1 (0xCCCCCCCC) 34309260Scognet#define CK_INTERNAL_LOG_2 (0xF0F0F0F0) 35309260Scognet#define CK_INTERNAL_LOG_3 (0xFF00FF00) 36309260Scognet#define CK_INTERNAL_LOG_4 (0xFFFF0000) 37309260Scognet 38309260ScognetCK_CC_INLINE static uint32_t 39309260Scognetck_internal_log(uint32_t v) 40309260Scognet{ 41309260Scognet uint32_t r = (v & CK_INTERNAL_LOG_0) != 0; 42309260Scognet 43309260Scognet r |= ((v & CK_INTERNAL_LOG_4) != 0) << 4; 44309260Scognet r |= ((v & CK_INTERNAL_LOG_3) != 0) << 3; 45309260Scognet r |= ((v & CK_INTERNAL_LOG_2) != 0) << 2; 46309260Scognet r |= ((v & CK_INTERNAL_LOG_1) != 0) << 1; 47309260Scognet return (r); 48309260Scognet} 49309260Scognet 50309260ScognetCK_CC_INLINE static uint32_t 51309260Scognetck_internal_power_2(uint32_t v) 52309260Scognet{ 53309260Scognet 54309260Scognet --v; 55309260Scognet v |= v >> 1; 56309260Scognet v |= v >> 2; 57309260Scognet v |= v >> 4; 58309260Scognet v |= v >> 8; 59309260Scognet v |= v >> 16; 60309260Scognet return (++v); 61309260Scognet} 62309260Scognet 63309260ScognetCK_CC_INLINE static unsigned long 64309260Scognetck_internal_max(unsigned long x, unsigned long y) 65309260Scognet{ 66309260Scognet 67309260Scognet return x ^ ((x ^ y) & -(x < y)); 68309260Scognet} 69309260Scognet 70309260ScognetCK_CC_INLINE static uint64_t 71309260Scognetck_internal_max_64(uint64_t x, uint64_t y) 72309260Scognet{ 73309260Scognet 74309260Scognet return x ^ ((x ^ y) & -(x < y)); 75309260Scognet} 76309260Scognet 77309260ScognetCK_CC_INLINE static uint32_t 78309260Scognetck_internal_max_32(uint32_t x, uint32_t y) 79309260Scognet{ 80309260Scognet 81309260Scognet return x ^ ((x ^ y) & -(x < y)); 82309260Scognet} 83