From f9c7e030441fc4a54bf3bec8eed9e170e152ef1d Mon Sep 17 00:00:00 2001 From: Bernhard Miklautz Date: Tue, 26 May 2015 16:41:29 +0200 Subject: [PATCH] winpr: add intrin.h Exposing lzcnt in crt.h might causes compiler errors (redefinition) with recent versions of gcc (>=4.9) when winpr is included in other projects. As lzcnt isn't part of crt according to MSDN and also shouldn't be exported by default it was moved to intrin.h. The related test was also moved to the top level directory of winpr. --- libfreerdp/codec/rfx_rlgr.c | 1 + winpr/CMakeLists.txt | 4 ++ winpr/include/winpr/crt.h | 43 ------------ winpr/include/winpr/intrin.h | 66 +++++++++++++++++++ winpr/libwinpr/crt/test/CMakeLists.txt | 1 - winpr/test/.gitignore | 1 + winpr/test/CMakeLists.txt | 24 +++++++ .../{libwinpr/crt => }/test/TestIntrinsics.c | 3 +- 8 files changed, 98 insertions(+), 45 deletions(-) create mode 100644 winpr/include/winpr/intrin.h create mode 100644 winpr/test/.gitignore create mode 100644 winpr/test/CMakeLists.txt rename winpr/{libwinpr/crt => }/test/TestIntrinsics.c (98%) diff --git a/libfreerdp/codec/rfx_rlgr.c b/libfreerdp/codec/rfx_rlgr.c index 8135a43f7..3f5b2d40c 100644 --- a/libfreerdp/codec/rfx_rlgr.c +++ b/libfreerdp/codec/rfx_rlgr.c @@ -34,6 +34,7 @@ #include #include #include +#include #include "rfx_bitstream.h" diff --git a/winpr/CMakeLists.txt b/winpr/CMakeLists.txt index 99a6d413c..4ed12bf20 100644 --- a/winpr/CMakeLists.txt +++ b/winpr/CMakeLists.txt @@ -75,6 +75,10 @@ if(NOT ANDROID AND NOT IOS) add_subdirectory(tools) endif() +if(BUILD_TESTING) + add_subdirectory(test) +endif() + # Exporting if(${CMAKE_VERSION} VERSION_GREATER "2.8.10") diff --git a/winpr/include/winpr/crt.h b/winpr/include/winpr/crt.h index 11b33ca93..25e9b7960 100644 --- a/winpr/include/winpr/crt.h +++ b/winpr/include/winpr/crt.h @@ -95,50 +95,7 @@ static INLINE UINT16 _byteswap_ushort(UINT16 _val) { #endif -/** - * __lzcnt16, __lzcnt, __lzcnt64: - * http://msdn.microsoft.com/en-us/library/bb384809/ - */ -#if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2)) - -/** - * __lzcnt16, __lzcnt, __lzcnt64: - * http://msdn.microsoft.com/en-us/library/bb384809/ - * - * Beware: the result of __builtin_clz(0) is undefined - */ - -static INLINE UINT32 __lzcnt(UINT32 _val32) { - return _val32 ? ((UINT32) __builtin_clz(_val32)) : 32; -} - -static INLINE UINT16 __lzcnt16(UINT16 _val16) { - return _val16 ? ((UINT16) (__builtin_clz((UINT32) _val16) - 16)) : 16; -} - -#else - -static INLINE UINT32 __lzcnt(UINT32 x) { - unsigned y; - int n = 32; - 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; - return n - x; -} - -static INLINE UINT16 __lzcnt16(UINT16 x) { - return ((UINT16) __lzcnt((UINT32) x)); -} - -#endif - -#endif - -#ifndef _WIN32 #define CopyMemory(Destination, Source, Length) memcpy((Destination), (Source), (Length)) #define MoveMemory(Destination, Source, Length) memmove((Destination), (Source), (Length)) diff --git a/winpr/include/winpr/intrin.h b/winpr/include/winpr/intrin.h new file mode 100644 index 000000000..c06a1f02e --- /dev/null +++ b/winpr/include/winpr/intrin.h @@ -0,0 +1,66 @@ +/** + * WinPR: Windows Portable Runtime + * C Run-Time Library Routines + * + * Copyright 2015 Thincast Technologies GmbH + * Copyright 2015 Bernhard Miklautz + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef WINPR_INTRIN_H +#define WINPR_INTRIN_H + +#ifndef _WIN32 + +/** + * __lzcnt16, __lzcnt, __lzcnt64: + * http://msdn.microsoft.com/en-us/library/bb384809/ + * + * Beware: the result of __builtin_clz(0) is undefined + */ + +#if (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2)) + +static INLINE UINT32 __lzcnt(UINT32 _val32) { + return ((UINT32) __builtin_clz(_val32)); +} + +static INLINE UINT16 __lzcnt16(UINT16 _val16) { + return ((UINT16) (__builtin_clz((UINT32) _val16) - 16)); +} + +#else /* (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2) */ + +static INLINE UINT32 __lzcnt(UINT32 x) +{ + unsigned y; + int n = 32; + 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; + return n - x; +} + +static INLINE UINT16 __lzcnt16(UINT16 x) +{ + return ((UINT16) __lzcnt((UINT32) x)); +} + +#endif /* (__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 2) */ + +#endif /* _WIN32 */ +#endif /* WINPR_INTRIN_H */ diff --git a/winpr/libwinpr/crt/test/CMakeLists.txt b/winpr/libwinpr/crt/test/CMakeLists.txt index 4bccde151..4f1f9d3fd 100644 --- a/winpr/libwinpr/crt/test/CMakeLists.txt +++ b/winpr/libwinpr/crt/test/CMakeLists.txt @@ -8,7 +8,6 @@ set(${MODULE_PREFIX}_TESTS TestTypes.c TestAlignment.c TestString.c - TestIntrinsics.c TestUnicodeConversion.c) create_test_sourcelist(${MODULE_PREFIX}_SRCS diff --git a/winpr/test/.gitignore b/winpr/test/.gitignore new file mode 100644 index 000000000..f89aa1c86 --- /dev/null +++ b/winpr/test/.gitignore @@ -0,0 +1 @@ +TestWinpr.c diff --git a/winpr/test/CMakeLists.txt b/winpr/test/CMakeLists.txt new file mode 100644 index 000000000..ec3722695 --- /dev/null +++ b/winpr/test/CMakeLists.txt @@ -0,0 +1,24 @@ + +set(MODULE_NAME "TestWinpr") +set(MODULE_PREFIX "TEST_WINPR") + +set(${MODULE_PREFIX}_DRIVER ${MODULE_NAME}.c) + +set(${MODULE_PREFIX}_TESTS TestIntrinsics.c) + +create_test_sourcelist(${MODULE_PREFIX}_SRCS + ${${MODULE_PREFIX}_DRIVER} + ${${MODULE_PREFIX}_TESTS}) + +add_executable(${MODULE_NAME} ${${MODULE_PREFIX}_SRCS}) + +target_link_libraries(${MODULE_NAME} winpr) + +set_target_properties(${MODULE_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${TESTING_OUTPUT_DIRECTORY}") + +foreach(test ${${MODULE_PREFIX}_TESTS}) + get_filename_component(TestName ${test} NAME_WE) + add_test(${TestName} ${TESTING_OUTPUT_DIRECTORY}/${MODULE_NAME} ${TestName}) +endforeach() + +set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER "WinPR/Test") diff --git a/winpr/libwinpr/crt/test/TestIntrinsics.c b/winpr/test/TestIntrinsics.c similarity index 98% rename from winpr/libwinpr/crt/test/TestIntrinsics.c rename to winpr/test/TestIntrinsics.c index 5bbf915f9..235c078b5 100644 --- a/winpr/libwinpr/crt/test/TestIntrinsics.c +++ b/winpr/test/TestIntrinsics.c @@ -1,8 +1,9 @@ - #include #include #include +#include + static BOOL g_LZCNT = FALSE; static INLINE UINT32 lzcnt_s(UINT32 x)