Raptor Computing Systems Community Forums (BETA)

Software => Applications and Porting => Topic started by: DKnoto on November 03, 2022, 02:37:51 pm

Title: Problem with std::numeric_limits<char>::max()
Post by: DKnoto on November 03, 2022, 02:37:51 pm
I'm porting my software to ppc64le and ran into a problem in the std::numeric_limits<char>::max() function. According to
the documentation, this operation should return the value CHAR_MAX which is 127. This is the case on x86_64 with gcc 11.3.1
compiler. Unfortunately on ppc64le the code compiled with gcc 12.2.1 returns the value UCHAR_MAX which is 255.

Test code:
Code: [Select]
printf("std::numeric_limits<char>::max() == %d\n", static_cast<int>(std::numeric_limits<char>::max()));

Clang 14.0.5 and IBM xlc 16.01.0001.0003 return the same value.

Have any of you encountered such a problem on distributions other than Fedora 36?

 
Title: Re: Problem with std::numeric_limits<char>::max()
Post by: DKnoto on November 03, 2022, 03:02:15 pm
On FreeBSD 14.0 ppc64 with clang 14.0.5 has the same thing.
Title: Re: Problem with std::numeric_limits<char>::max()
Post by: adunsmuir on November 03, 2022, 07:59:35 pm
The data type char maps to either signed char or unsigned char depending on the platform.
It is not a fixed part of the C++ (or C) standards.

Is there any conditional logic in the headers that is getting miss-triggered in gcc 12?  What does gcc 13 do?

Al Dunsmuir
Title: Re: Problem with std::numeric_limits<char>::max()
Post by: DKnoto on November 04, 2022, 01:52:25 am
I checked it out at https://en.cppreference.com/w/cpp/types/climits. Actually in the example it says that these are "possible values". Nevertheless, I am very surprised that C++ defines its portability so poorly.

The same algorithm implemented in Java works equally on both platforms.

The test code:

Code: [Select]
import java.lang.*;

public class JLimits
{
    public static void main(String[] args)
    {
        System.out.printf("byte.MAX_VALUE == %d.\n", Byte.MAX_VALUE);
    }
}

Thanks for the hint, I am implementing a workaround.
Title: Re: Problem with std::numeric_limits<char>::max()
Post by: DKnoto on November 04, 2022, 02:43:14 am
The workaround is trivial:
Code: [Select]
typedef signed char byte;

 ;)