Blame winpr/include/winpr/intrin.h

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