Skip to content

C library: add __lzcnt (Visual Studio) and test it #5725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include <assert.h>

#ifndef __GNUC__
int __builtin_clz(unsigned int);
int __builtin_clzl(unsigned long);
int __builtin_clzll(unsigned long long);
#ifdef _MSC_VER
# define __builtin_clz(x) __lzcnt(x)
# define __builtin_clzll(x) __lzcnt64(x)
#endif

unsigned int __VERIFIER_nondet_unsigned();
Expand Down Expand Up @@ -39,10 +38,16 @@ int nlz2a(unsigned x)

int main()
{
#ifdef _MSC_VER
unsigned short f = 42;
assert(__lzcnt16(f) == 10);
#endif
assert(nlz2a(42) == 26);
assert(__builtin_clz(42) == 26);
#ifndef _MSC_VER
assert(
__builtin_clzl(42) == 26 + (sizeof(unsigned long) - sizeof(unsigned)) * 8);
#endif
assert(
__builtin_clzll(42) ==
26 + (sizeof(unsigned long long) - sizeof(unsigned)) * 8);
Expand Down
8 changes: 0 additions & 8 deletions regression/cbmc/gcc_clz1/test.desc

This file was deleted.

43 changes: 43 additions & 0 deletions src/ansi-c/library/windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,46 @@ inline HANDLE CreateThread(
return handle;
}
#endif

/* FUNCTION: __lzcnt16 */

#ifdef _MSC_VER
int __builtin_clz(unsigned int x);

unsigned short __lzcnt16(unsigned short value)
{
if(value == 0)
return 16;

return __builtin_clz(value) -
(sizeof(unsigned int) - sizeof(unsigned short)) * 8;
}
#endif

/* FUNCTION: __lzcnt */

#ifdef _MSC_VER
int __builtin_clz(unsigned int x);

unsigned int __lzcnt(unsigned int value)
{
if(value == 0)
return 32;

return __builtin_clz(value);
}
#endif

/* FUNCTION: __lzcnt64 */

#ifdef _MSC_VER
int __builtin_clzll(unsigned long long x);

unsigned __int64 __lzcnt64(unsigned __int64 value)
{
if(value == 0)
return 64;

return __builtin_clzll(value);
}
#endif