intrin.h (1857B)
1/** 2 * WinPR: Windows Portable Runtime 3 * C Run-Time Library Routines 4 * 5 * Copyright 2015 Thincast Technologies GmbH 6 * Copyright 2015 Bernhard Miklautz <bernhard.miklautz@thincast.com> 7 * 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22#ifndef WINPR_INTRIN_H 23#define WINPR_INTRIN_H 24 25#ifndef _WIN32 26 27/** 28 * __lzcnt16, __lzcnt, __lzcnt64: 29 * http://msdn.microsoft.com/en-us/library/bb384809/ 30 * 31 * Beware: the result of __builtin_clz(0) is undefined 32 */ 33 34#if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2)) 35 36static INLINE UINT32 __lzcnt(UINT32 _val32) 37{ 38 return ((UINT32)__builtin_clz(_val32)); 39} 40 41static INLINE UINT16 __lzcnt16(UINT16 _val16) 42{ 43 return ((UINT16)(__builtin_clz((UINT32)_val16) - 16)); 44} 45 46#else /* (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2) */ 47 48static INLINE UINT32 __lzcnt(UINT32 x) 49{ 50 unsigned y; 51 int n = 32; 52 y = x >> 16; 53 if (y != 0) 54 { 55 n = n - 16; 56 x = y; 57 } 58 y = x >> 8; 59 if (y != 0) 60 { 61 n = n - 8; 62 x = y; 63 } 64 y = x >> 4; 65 if (y != 0) 66 { 67 n = n - 4; 68 x = y; 69 } 70 y = x >> 2; 71 if (y != 0) 72 { 73 n = n - 2; 74 x = y; 75 } 76 y = x >> 1; 77 if (y != 0) 78 return n - 2; 79 return n - x; 80} 81 82static INLINE UINT16 __lzcnt16(UINT16 x) 83{ 84 return ((UINT16)__lzcnt((UINT32)x)); 85} 86 87#endif /* (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2) */ 88 89#endif /* _WIN32 */ 90#endif /* WINPR_INTRIN_H */