Problem with std::numeric_limits<char>::max()

Started by DKnoto, November 03, 2022, 02:37:51 PM

Previous topic - Next topic

DKnoto

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:

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?

 
Desktop: Talos II T2P9S01 REV 1.01 | IBM Power 9/18c DD2.3, 02CY646 | AMD Radeon Pro WX7100 | 64GB RAM | SSD 1TB

DKnoto

On FreeBSD 14.0 ppc64 with clang 14.0.5 has the same thing.
Desktop: Talos II T2P9S01 REV 1.01 | IBM Power 9/18c DD2.3, 02CY646 | AMD Radeon Pro WX7100 | 64GB RAM | SSD 1TB

adunsmuir

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

DKnoto

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:


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.
Desktop: Talos II T2P9S01 REV 1.01 | IBM Power 9/18c DD2.3, 02CY646 | AMD Radeon Pro WX7100 | 64GB RAM | SSD 1TB

DKnoto

The workaround is trivial:

typedef signed char byte;


;)
Desktop: Talos II T2P9S01 REV 1.01 | IBM Power 9/18c DD2.3, 02CY646 | AMD Radeon Pro WX7100 | 64GB RAM | SSD 1TB