15c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper/*===---- lzcntintrin.h - LZCNT intrinsics ---------------------------------===
25c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper *
35c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * Permission is hereby granted, free of charge, to any person obtaining a copy
45c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * of this software and associated documentation files (the "Software"), to deal
55c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * in the Software without restriction, including without limitation the rights
65c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
75c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * copies of the Software, and to permit persons to whom the Software is
85c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * furnished to do so, subject to the following conditions:
95c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper *
105c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * The above copyright notice and this permission notice shall be included in
115c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * all copies or substantial portions of the Software.
125c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper *
135c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
145c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
155c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
165c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
175c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
185c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
195c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper * THE SOFTWARE.
205c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper *
215c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper *===-----------------------------------------------------------------------===
225c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper */
235c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper
245c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#if !defined __X86INTRIN_H && !defined __IMMINTRIN_H
255c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#error "Never use <lzcntintrin.h> directly; include <x86intrin.h> instead."
265c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#endif
275c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper
285c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#ifndef __LZCNT__
295c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper# error "LZCNT instruction is not enabled"
305c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#endif /* __LZCNT__ */
315c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper
325c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#ifndef __LZCNTINTRIN_H
335c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#define __LZCNTINTRIN_H
345c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper
355c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topperstatic __inline__ unsigned short __attribute__((__always_inline__, __nodebug__))
365c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper__lzcnt16(unsigned short __X)
375c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper{
385c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper  return __builtin_clzs(__X);
395c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper}
405c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper
415c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topperstatic __inline__ unsigned int __attribute__((__always_inline__, __nodebug__))
425c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper__lzcnt32(unsigned int __X)
435c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper{
445c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper  return __builtin_clz(__X);
455c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper}
465c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper
475c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#ifdef __x86_64__
485c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topperstatic __inline__ unsigned long long __attribute__((__always_inline__, __nodebug__))
495c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper__lzcnt64(unsigned long long __X)
505c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper{
515c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper  return __builtin_clzll(__X);
525c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper}
535c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#endif
545c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper
555c75208a5b88c835bce0a1671015c7e22c72f35fCraig Topper#endif /* __LZCNTINTRIN_H */
56