2014-08-03 06:26:05 +04:00
|
|
|
#include <winpr/crt.h>
|
2014-08-12 02:48:42 +04:00
|
|
|
#include <winpr/sysinfo.h>
|
2014-08-03 06:26:05 +04:00
|
|
|
#include <winpr/windows.h>
|
|
|
|
|
2015-05-26 17:41:29 +03:00
|
|
|
#include <winpr/intrin.h>
|
|
|
|
|
2014-08-12 02:48:42 +04:00
|
|
|
static BOOL g_LZCNT = FALSE;
|
|
|
|
|
|
|
|
static INLINE UINT32 lzcnt_s(UINT32 x)
|
2014-08-03 06:26:05 +04:00
|
|
|
{
|
2014-08-12 02:48:42 +04:00
|
|
|
if (!x)
|
|
|
|
return 32;
|
2019-11-06 17:24:51 +03:00
|
|
|
|
2014-08-12 02:48:42 +04:00
|
|
|
if (!g_LZCNT)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
UINT32 y = 0;
|
2014-08-12 02:48:42 +04:00
|
|
|
int n = 32;
|
2019-11-06 17:24:51 +03:00
|
|
|
y = x >> 16;
|
|
|
|
if (y != 0)
|
|
|
|
{
|
|
|
|
n = n - 16;
|
|
|
|
x = y;
|
|
|
|
}
|
|
|
|
y = x >> 8;
|
|
|
|
if (y != 0)
|
|
|
|
{
|
|
|
|
n = n - 8;
|
|
|
|
x = y;
|
|
|
|
}
|
|
|
|
y = x >> 4;
|
|
|
|
if (y != 0)
|
|
|
|
{
|
|
|
|
n = n - 4;
|
|
|
|
x = y;
|
|
|
|
}
|
|
|
|
y = x >> 2;
|
|
|
|
if (y != 0)
|
|
|
|
{
|
|
|
|
n = n - 2;
|
|
|
|
x = y;
|
|
|
|
}
|
|
|
|
y = x >> 1;
|
|
|
|
if (y != 0)
|
|
|
|
return n - 2;
|
2014-08-12 02:48:42 +04:00
|
|
|
return n - x;
|
|
|
|
}
|
|
|
|
|
|
|
|
return __lzcnt(x);
|
|
|
|
}
|
2014-08-03 06:26:05 +04:00
|
|
|
|
2021-07-28 16:18:03 +03:00
|
|
|
static int test_lzcnt(void)
|
2014-08-12 02:48:42 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (lzcnt_s(0x1) != 31)
|
|
|
|
{
|
2024-08-26 16:39:33 +03:00
|
|
|
(void)fprintf(stderr, "__lzcnt(0x1) != 31: %" PRIu32 "\n", __lzcnt(0x1));
|
2014-08-03 06:26:05 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (lzcnt_s(0xFF) != 24)
|
|
|
|
{
|
2024-08-26 16:39:33 +03:00
|
|
|
(void)fprintf(stderr, "__lzcnt(0xFF) != 24\n");
|
2014-08-03 06:26:05 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (lzcnt_s(0xFFFF) != 16)
|
|
|
|
{
|
2024-08-26 16:39:33 +03:00
|
|
|
(void)fprintf(stderr, "__lzcnt(0xFFFF) != 16\n");
|
2014-08-03 06:26:05 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (lzcnt_s(0xFFFFFF) != 8)
|
|
|
|
{
|
2024-08-26 16:39:33 +03:00
|
|
|
(void)fprintf(stderr, "__lzcnt(0xFFFFFF) != 8\n");
|
2014-08-03 06:26:05 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (lzcnt_s(0xFFFFFFFF) != 0)
|
|
|
|
{
|
2024-08-26 16:39:33 +03:00
|
|
|
(void)fprintf(stderr, "__lzcnt(0xFFFFFFFF) != 0\n");
|
2014-08-03 06:26:05 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-27 11:32:08 +03:00
|
|
|
return 0;
|
2014-08-03 06:26:05 +04:00
|
|
|
}
|
|
|
|
|
2021-07-28 16:18:03 +03:00
|
|
|
static int test_lzcnt16(void)
|
2014-08-03 06:26:05 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (__lzcnt16(0x1) != 15)
|
|
|
|
{
|
2024-08-26 16:39:33 +03:00
|
|
|
(void)fprintf(stderr, "__lzcnt16(0x1) != 15\n");
|
2014-08-03 06:26:05 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (__lzcnt16(0xFF) != 8)
|
|
|
|
{
|
2024-08-26 16:39:33 +03:00
|
|
|
(void)fprintf(stderr, "__lzcnt16(0xFF) != 8\n");
|
2014-08-03 06:26:05 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (__lzcnt16(0xFFFF) != 0)
|
|
|
|
{
|
2024-08-26 16:39:33 +03:00
|
|
|
(void)fprintf(stderr, "__lzcnt16(0xFFFF) != 0\n");
|
2014-08-03 06:26:05 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-05-27 11:32:08 +03:00
|
|
|
return 0;
|
2014-08-03 06:26:05 +04:00
|
|
|
}
|
|
|
|
|
2014-08-12 02:48:42 +04:00
|
|
|
int TestIntrinsics(int argc, char* argv[])
|
2014-08-03 06:26:05 +04:00
|
|
|
{
|
2021-07-29 11:18:52 +03:00
|
|
|
|
|
|
|
WINPR_UNUSED(argc);
|
|
|
|
WINPR_UNUSED(argv);
|
|
|
|
|
2014-08-12 02:48:42 +04:00
|
|
|
g_LZCNT = IsProcessorFeaturePresentEx(PF_EX_LZCNT);
|
2014-08-03 06:26:05 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
printf("LZCNT available: %" PRId32 "\n", g_LZCNT);
|
2014-08-03 06:26:05 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
// test_lzcnt16();
|
2015-05-27 11:32:08 +03:00
|
|
|
return test_lzcnt();
|
2014-08-03 06:26:05 +04:00
|
|
|
}
|