/*1* Copyright 2011-2015 Samy Al Bahra.2* Copyright 2011 David Joseph.3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627/*28* Several of these are from: http://graphics.stanford.edu/~seander/bithacks.html29*/3031#define CK_INTERNAL_LOG_0 (0xAAAAAAAA)32#define CK_INTERNAL_LOG_1 (0xCCCCCCCC)33#define CK_INTERNAL_LOG_2 (0xF0F0F0F0)34#define CK_INTERNAL_LOG_3 (0xFF00FF00)35#define CK_INTERNAL_LOG_4 (0xFFFF0000)3637CK_CC_INLINE static uint32_t38ck_internal_log(uint32_t v)39{40uint32_t r = (v & CK_INTERNAL_LOG_0) != 0;4142r |= ((v & CK_INTERNAL_LOG_4) != 0) << 4;43r |= ((v & CK_INTERNAL_LOG_3) != 0) << 3;44r |= ((v & CK_INTERNAL_LOG_2) != 0) << 2;45r |= ((v & CK_INTERNAL_LOG_1) != 0) << 1;46return (r);47}4849CK_CC_INLINE static uint32_t50ck_internal_power_2(uint32_t v)51{5253--v;54v |= v >> 1;55v |= v >> 2;56v |= v >> 4;57v |= v >> 8;58v |= v >> 16;59return (++v);60}6162CK_CC_INLINE static unsigned long63ck_internal_max(unsigned long x, unsigned long y)64{6566return x ^ ((x ^ y) & -(x < y));67}6869CK_CC_INLINE static uint64_t70ck_internal_max_64(uint64_t x, uint64_t y)71{7273return x ^ ((x ^ y) & -(x < y));74}7576CK_CC_INLINE static uint32_t77ck_internal_max_32(uint32_t x, uint32_t y)78{7980return x ^ ((x ^ y) & -(x < y));81}828384