From f7925fd4660b09cc47779249ce17d3b8204da1a8 Mon Sep 17 00:00:00 2001 From: "Kasper F. Brandt" Date: Wed, 11 Feb 2015 10:02:49 +0100 Subject: [PATCH] Add abs(__int64) overload for 64-bit targets. Fixes #143 --- src/pal/inc/pal.h | 11 +++- src/pal/src/include/pal/palinternal.h | 2 + src/pal/tests/palsuite/c_runtime/CMakeLists.txt | 1 + .../tests/palsuite/c_runtime/llabs/CMakeLists.txt | 4 ++ .../palsuite/c_runtime/llabs/test1/CMakeLists.txt | 19 +++++++ .../tests/palsuite/c_runtime/llabs/test1/test1.c | 66 ++++++++++++++++++++++ .../palsuite/c_runtime/llabs/test1/testinfo.dat | 14 +++++ src/pal/tests/palsuite/paltestlist.txt | 1 + src/pal/tests/palsuite/palverify.dat | 1 + 9 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 src/pal/tests/palsuite/c_runtime/llabs/CMakeLists.txt create mode 100644 src/pal/tests/palsuite/c_runtime/llabs/test1/CMakeLists.txt create mode 100644 src/pal/tests/palsuite/c_runtime/llabs/test1/test1.c create mode 100644 src/pal/tests/palsuite/c_runtime/llabs/test1/testinfo.dat diff --git a/src/pal/inc/pal.h b/src/pal/inc/pal.h index 063f80d..b5ee386 100644 --- a/src/pal/inc/pal.h +++ b/src/pal/inc/pal.h @@ -5435,6 +5435,8 @@ PALIMPORT unsigned int __cdecl _rotr(unsigned int, int); PALIMPORT int __cdecl abs(int); PALIMPORT double __cdecl fabs(double); PALIMPORT LONG __cdecl labs(LONG); +// clang complains if this is declared with __int64 +PALIMPORT long long __cdecl llabs(long long); PALIMPORT double __cdecl sqrt(double); PALIMPORT double __cdecl log(double); @@ -5466,7 +5468,14 @@ PALIMPORT double __cdecl _copysign(double, double); extern "C++" { inline float fabsf(float _X) { - return ((float)fabs((double)_X)); } + return ((float)fabs((double)_X)); +} + +#ifdef BIT64 +inline __int64 abs(__int64 _X) { + return llabs(_X); +} +#endif } #endif diff --git a/src/pal/src/include/pal/palinternal.h b/src/pal/src/include/pal/palinternal.h index 6723329..74f0d79 100644 --- a/src/pal/src/include/pal/palinternal.h +++ b/src/pal/src/include/pal/palinternal.h @@ -198,6 +198,7 @@ function_name() to call the system's implementation #define time_t PAL_time_t #define va_list DUMMY_va_list #define abs DUMMY_abs +#define llabs DUMMY_llabs #define atan DUMMY_atan #define tan DUMMY_tan #define cos DUMMY_cos @@ -430,6 +431,7 @@ function_name() to call the system's implementation #undef stderr #undef abs #undef labs +#undef llabs #undef acos #undef asin #undef atan2 diff --git a/src/pal/tests/palsuite/c_runtime/CMakeLists.txt b/src/pal/tests/palsuite/c_runtime/CMakeLists.txt index 0fa69d5..5bc2e1e 100644 --- a/src/pal/tests/palsuite/c_runtime/CMakeLists.txt +++ b/src/pal/tests/palsuite/c_runtime/CMakeLists.txt @@ -51,6 +51,7 @@ add_subdirectory(iswupper) add_subdirectory(iswxdigit) add_subdirectory(isxdigit) add_subdirectory(labs) +add_subdirectory(llabs) add_subdirectory(localtime) add_subdirectory(log) add_subdirectory(log10) diff --git a/src/pal/tests/palsuite/c_runtime/llabs/CMakeLists.txt b/src/pal/tests/palsuite/c_runtime/llabs/CMakeLists.txt new file mode 100644 index 0000000..f6aa0cb --- /dev/null +++ b/src/pal/tests/palsuite/c_runtime/llabs/CMakeLists.txt @@ -0,0 +1,4 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +add_subdirectory(test1) + diff --git a/src/pal/tests/palsuite/c_runtime/llabs/test1/CMakeLists.txt b/src/pal/tests/palsuite/c_runtime/llabs/test1/CMakeLists.txt new file mode 100644 index 0000000..e65be7e --- /dev/null +++ b/src/pal/tests/palsuite/c_runtime/llabs/test1/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 2.8.12.2) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(SOURCES + test1.c +) + +add_executable(paltest_llabs_test1 + ${SOURCES} +) + +add_dependencies(paltest_llabs_test1 CoreClrPal) + +target_link_libraries(paltest_llabs_test1 + pthread + m + CoreClrPal +) diff --git a/src/pal/tests/palsuite/c_runtime/llabs/test1/test1.c b/src/pal/tests/palsuite/c_runtime/llabs/test1/test1.c new file mode 100644 index 0000000..0f01fac --- /dev/null +++ b/src/pal/tests/palsuite/c_runtime/llabs/test1/test1.c @@ -0,0 +1,66 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +/*===================================================================== +** +** Source: test1.c +** +** Purpose: Call llabs on a series of values -- negative, positive, +** zero, and the largest negative value of an __int64. Ensure that +** they are all changed properly to their absoulte value. +** +** +**===================================================================*/ + +#include + +struct testCase +{ + __int64 LongLongValue; + __int64 AbsoluteLongLongValue; +}; + +int __cdecl main(int argc, char **argv) +{ + + __int64 result=0; + int i=0; + + struct testCase testCases[] = + { + {1234, 1234}, + {-1234, 1234}, + {0, 0}, + {-9223372036854775807LL, 9223372036854775807LL}, /* Max value to abs */ + {9223372036854775807LL, 9223372036854775807LL} + }; + + if (0 != (PAL_Initialize(argc, argv))) + { + return FAIL; + } + + /* Loop through each case. Call labs on each LONG and ensure that + the resulting value is correct. + */ + + for(i = 0; i < sizeof(testCases) / sizeof(struct testCase); i++) + { + /* Absolute value on a LONG */ + result = llabs(testCases[i].LongLongValue); + + if (testCases[i].AbsoluteLongLongValue != result) + { + Fail("ERROR: labs took the absoulte value of '%d' to be '%d' " + "instead of %d.\n", + testCases[i].LongLongValue, + result, + testCases[i].AbsoluteLongLongValue); + } + } + + PAL_Terminate(); + return PASS; +} diff --git a/src/pal/tests/palsuite/c_runtime/llabs/test1/testinfo.dat b/src/pal/tests/palsuite/c_runtime/llabs/test1/testinfo.dat new file mode 100644 index 0000000..60e053f --- /dev/null +++ b/src/pal/tests/palsuite/c_runtime/llabs/test1/testinfo.dat @@ -0,0 +1,14 @@ +# +# Copyright (c) Microsoft Corporation. All rights reserved. +# + +Version = 1.0 +Section = C Runtime +Function = labs +Name = Series of tests for labs: positive, negative, zero, maximum __int64 value. +TYPE = DEFAULT +EXE1 = test1 +Description += Call llabs on a series of values -- negative, positive, zero, += and the largest negative value of an __int64. Ensure that they are all += changed properly to their absoulte value. diff --git a/src/pal/tests/palsuite/paltestlist.txt b/src/pal/tests/palsuite/paltestlist.txt index 5eab7a4..5af8c87 100644 --- a/src/pal/tests/palsuite/paltestlist.txt +++ b/src/pal/tests/palsuite/paltestlist.txt @@ -90,6 +90,7 @@ c_runtime/iswupper/test1/paltest_iswupper_test1 c_runtime/iswxdigit/test1/paltest_iswxdigit_test1 c_runtime/isxdigit/test1/paltest_isxdigit_test1 c_runtime/labs/test1/paltest_labs_test1 +c_runtime/llabs/test1/paltest_llabs_test1 c_runtime/localtime/test1/paltest_localtime_test1 c_runtime/log/test1/paltest_log_test1 c_runtime/log10/test1/paltest_log10_test1 diff --git a/src/pal/tests/palsuite/palverify.dat b/src/pal/tests/palsuite/palverify.dat index 61b1bdb..c70c6c5 100644 --- a/src/pal/tests/palsuite/palverify.dat +++ b/src/pal/tests/palsuite/palverify.dat @@ -226,6 +226,7 @@ c_runtime/iswupper/test1,1 c_runtime/iswxdigit/test1,1 c_runtime/isxdigit/test1,1 c_runtime/labs/test1,1 +c_runtime/llabs/test1,1 c_runtime/localtime/test1,1 c_runtime/log/test1,1 c_runtime/log10/test1,1 -- 2.7.4