Blame winpr/include/winpr/intrin.h

Packit 1fb8d4
/**
Packit 1fb8d4
 * WinPR: Windows Portable Runtime
Packit 1fb8d4
 * C Run-Time Library Routines
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2015 Thincast Technologies GmbH
Packit 1fb8d4
 * Copyright 2015 Bernhard Miklautz <bernhard.miklautz@thincast.com>
Packit Service 5a9772
 *
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifndef WINPR_INTRIN_H
Packit 1fb8d4
#define WINPR_INTRIN_H
Packit 1fb8d4
Packit 1fb8d4
#ifndef _WIN32
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * __lzcnt16, __lzcnt, __lzcnt64:
Packit 1fb8d4
 * http://msdn.microsoft.com/en-us/library/bb384809/
Packit 1fb8d4
 *
Packit 1fb8d4
 * Beware: the result of __builtin_clz(0) is undefined
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2))
Packit 1fb8d4
Packit Service 5a9772
static INLINE UINT32 __lzcnt(UINT32 _val32)
Packit Service 5a9772
{
Packit Service 5a9772
	return ((UINT32)__builtin_clz(_val32));
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
static INLINE UINT16 __lzcnt16(UINT16 _val16)
Packit Service 5a9772
{
Packit Service 5a9772
	return ((UINT16)(__builtin_clz((UINT32)_val16) - 16));
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#else /* (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2) */
Packit 1fb8d4
Packit 1fb8d4
static INLINE UINT32 __lzcnt(UINT32 x)
Packit 1fb8d4
{
Packit 1fb8d4
	unsigned y;
Packit 1fb8d4
	int n = 32;
Packit Service 5a9772
	y = x >> 16;
Packit Service 5a9772
	if (y != 0)
Packit Service 5a9772
	{
Packit Service 5a9772
		n = n - 16;
Packit Service 5a9772
		x = y;
Packit Service 5a9772
	}
Packit Service 5a9772
	y = x >> 8;
Packit Service 5a9772
	if (y != 0)
Packit Service 5a9772
	{
Packit Service 5a9772
		n = n - 8;
Packit Service 5a9772
		x = y;
Packit Service 5a9772
	}
Packit Service 5a9772
	y = x >> 4;
Packit Service 5a9772
	if (y != 0)
Packit Service 5a9772
	{
Packit Service 5a9772
		n = n - 4;
Packit Service 5a9772
		x = y;
Packit Service 5a9772
	}
Packit Service 5a9772
	y = x >> 2;
Packit Service 5a9772
	if (y != 0)
Packit Service 5a9772
	{
Packit Service 5a9772
		n = n - 2;
Packit Service 5a9772
		x = y;
Packit Service 5a9772
	}
Packit Service 5a9772
	y = x >> 1;
Packit Service 5a9772
	if (y != 0)
Packit Service 5a9772
		return n - 2;
Packit 1fb8d4
	return n - x;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static INLINE UINT16 __lzcnt16(UINT16 x)
Packit 1fb8d4
{
Packit Service 5a9772
	return ((UINT16)__lzcnt((UINT32)x));
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#endif /* (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2) */
Packit 1fb8d4
Packit 1fb8d4
#endif /* _WIN32 */
Packit 1fb8d4
#endif /* WINPR_INTRIN_H */