Skip to content

Commit 9a3b545

Browse files
authored
Merge pull request #5725 from tautschnig/lzcnt
C library: add __lzcnt (Visual Studio) and test it
2 parents 1619299 + 95d698b commit 9a3b545

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

regression/cbmc/gcc_clz1/main.c renamed to regression/cbmc-library/__builtin_clz-01/main.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#include <assert.h>
22

3-
#ifndef __GNUC__
4-
int __builtin_clz(unsigned int);
5-
int __builtin_clzl(unsigned long);
6-
int __builtin_clzll(unsigned long long);
3+
#ifdef _MSC_VER
4+
# define __builtin_clz(x) __lzcnt(x)
5+
# define __builtin_clzll(x) __lzcnt64(x)
76
#endif
87

98
unsigned int __VERIFIER_nondet_unsigned();
@@ -39,10 +38,16 @@ int nlz2a(unsigned x)
3938

4039
int main()
4140
{
41+
#ifdef _MSC_VER
42+
unsigned short f = 42;
43+
assert(__lzcnt16(f) == 10);
44+
#endif
4245
assert(nlz2a(42) == 26);
4346
assert(__builtin_clz(42) == 26);
47+
#ifndef _MSC_VER
4448
assert(
4549
__builtin_clzl(42) == 26 + (sizeof(unsigned long) - sizeof(unsigned)) * 8);
50+
#endif
4651
assert(
4752
__builtin_clzll(42) ==
4853
26 + (sizeof(unsigned long long) - sizeof(unsigned)) * 8);

regression/cbmc/gcc_clz1/test.desc

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/ansi-c/library/windows.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,46 @@ inline HANDLE CreateThread(
5151
return handle;
5252
}
5353
#endif
54+
55+
/* FUNCTION: __lzcnt16 */
56+
57+
#ifdef _MSC_VER
58+
int __builtin_clz(unsigned int x);
59+
60+
unsigned short __lzcnt16(unsigned short value)
61+
{
62+
if(value == 0)
63+
return 16;
64+
65+
return __builtin_clz(value) -
66+
(sizeof(unsigned int) - sizeof(unsigned short)) * 8;
67+
}
68+
#endif
69+
70+
/* FUNCTION: __lzcnt */
71+
72+
#ifdef _MSC_VER
73+
int __builtin_clz(unsigned int x);
74+
75+
unsigned int __lzcnt(unsigned int value)
76+
{
77+
if(value == 0)
78+
return 32;
79+
80+
return __builtin_clz(value);
81+
}
82+
#endif
83+
84+
/* FUNCTION: __lzcnt64 */
85+
86+
#ifdef _MSC_VER
87+
int __builtin_clzll(unsigned long long x);
88+
89+
unsigned __int64 __lzcnt64(unsigned __int64 value)
90+
{
91+
if(value == 0)
92+
return 64;
93+
94+
return __builtin_clzll(value);
95+
}
96+
#endif

0 commit comments

Comments
 (0)