Add abs(__int64) overload for 64-bit targets. Fixes #143
authorKasper F. Brandt <poizan@poizan.dk>
Wed, 11 Feb 2015 09:02:49 +0000 (10:02 +0100)
committerKasper F. Brandt <poizan@poizan.dk>
Wed, 11 Feb 2015 22:50:09 +0000 (23:50 +0100)
src/pal/inc/pal.h
src/pal/src/include/pal/palinternal.h
src/pal/tests/palsuite/c_runtime/CMakeLists.txt
src/pal/tests/palsuite/c_runtime/llabs/CMakeLists.txt [new file with mode: 0644]
src/pal/tests/palsuite/c_runtime/llabs/test1/CMakeLists.txt [new file with mode: 0644]
src/pal/tests/palsuite/c_runtime/llabs/test1/test1.c [new file with mode: 0644]
src/pal/tests/palsuite/c_runtime/llabs/test1/testinfo.dat [new file with mode: 0644]
src/pal/tests/palsuite/paltestlist.txt
src/pal/tests/palsuite/palverify.dat

index 063f80d..b5ee386 100644 (file)
@@ -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
 
index 6723329..74f0d79 100644 (file)
@@ -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
index 0fa69d5..5bc2e1e 100644 (file)
@@ -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 (file)
index 0000000..f6aa0cb
--- /dev/null
@@ -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 (file)
index 0000000..e65be7e
--- /dev/null
@@ -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 (file)
index 0000000..0f01fac
--- /dev/null
@@ -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 <palsuite.h>
+
+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 (file)
index 0000000..60e053f
--- /dev/null
@@ -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. 
index 5eab7a4..5af8c87 100644 (file)
@@ -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
index 61b1bdb..c70c6c5 100644 (file)
@@ -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