host-utils.h revision 81db31727ed27322c3f5e3bc40e71fe7fc54bf91
/*
* Utility compute operations used by translated code.
*
* Copyright (c) 2007 Thiemo Seufer
* Copyright (c) 2007 Jocelyn Mayer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "osdep.h"
#if defined(__x86_64__)
#define __HAVE_FAST_MULU64__
{
__asm__ ("mul %0\n\t"
: "a" (a), "0" (b));
}
#define __HAVE_FAST_MULS64__
{
__asm__ ("imul %0\n\t"
: "a" (a), "0" (b));
}
#else
#endif
/* Binary search for leading zeros. */
{
if (val)
return __builtin_clz(val);
else
return 32;
#else
int cnt = 0;
if (!(val & 0xFFFF0000U)) {
cnt += 16;
val <<= 16;
}
if (!(val & 0xFF000000U)) {
cnt += 8;
val <<= 8;
}
if (!(val & 0xF0000000U)) {
cnt += 4;
val <<= 4;
}
if (!(val & 0xC0000000U)) {
cnt += 2;
val <<= 2;
}
if (!(val & 0x80000000U)) {
cnt++;
val <<= 1;
}
if (!(val & 0x80000000U)) {
cnt++;
}
return cnt;
#endif
}
{
}
{
if (val)
return __builtin_clzll(val);
else
return 64;
#else
int cnt = 0;
if (!(val >> 32)) {
cnt += 32;
} else {
val >>= 32;
}
#endif
}
{
}
{
if (val)
return __builtin_ctz(val);
else
return 32;
#else
int cnt;
cnt = 0;
if (!(val & 0x0000FFFFUL)) {
cnt += 16;
val >>= 16;
}
if (!(val & 0x000000FFUL)) {
cnt += 8;
val >>= 8;
}
if (!(val & 0x0000000FUL)) {
cnt += 4;
val >>= 4;
}
if (!(val & 0x00000003UL)) {
cnt += 2;
val >>= 2;
}
if (!(val & 0x00000001UL)) {
cnt++;
val >>= 1;
}
if (!(val & 0x00000001UL)) {
cnt++;
}
return cnt;
#endif
}
{
}
{
if (val)
return __builtin_ctzll(val);
else
return 64;
#else
int cnt;
cnt = 0;
cnt += 32;
val >>= 32;
}
#endif
}
{
}
{
return val;
}
{
return val;
}
{
return __builtin_popcount(val);
#else
return val;
#endif
}
{
return __builtin_popcountll(val);
#else
return val;
#endif
}