Skip to content

Commit

Permalink
Add abs(__int64) overload for 64-bit targets. Fixes dotnet/coreclr#143
Browse files Browse the repository at this point in the history
Commit migrated from dotnet/coreclr@f7925fd
  • Loading branch information
poizan42 committed Feb 11, 2015
1 parent f8673f8 commit b77fb7f
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/coreclr/src/pal/inc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/src/pal/src/include/pal/palinternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 2.8.12.2)

add_subdirectory(test1)

Original file line number Diff line number Diff line change
@@ -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
)
66 changes: 66 additions & 0 deletions src/coreclr/src/pal/tests/palsuite/c_runtime/llabs/test1/test1.c
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions src/coreclr/src/pal/tests/palsuite/paltestlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/src/pal/tests/palsuite/palverify.dat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b77fb7f

Please sign in to comment.