Author Topic: Problem with std::numeric_limits<char>::max()  (Read 619 times)

DKnoto

  • Jr. Member
  • **
  • Posts: 75
  • Karma: +13/-0
    • View Profile
Problem with std::numeric_limits<char>::max()
« 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?

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

DKnoto

  • Jr. Member
  • **
  • Posts: 75
  • Karma: +13/-0
    • View Profile
Re: Problem with std::numeric_limits<char>::max()
« Reply #1 on: November 03, 2022, 03:02:15 pm »
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

  • Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Re: Problem with std::numeric_limits<char>::max()
« Reply #2 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

DKnoto

  • Jr. Member
  • **
  • Posts: 75
  • Karma: +13/-0
    • View Profile
Re: Problem with std::numeric_limits<char>::max()
« Reply #3 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.
Desktop: Talos II T2P9S01 REV 1.01 | IBM Power 9/18c DD2.3, 02CY646 | AMD Radeon Pro WX7100 | 64GB RAM | SSD 1TB

DKnoto

  • Jr. Member
  • **
  • Posts: 75
  • Karma: +13/-0
    • View Profile
Re: Problem with std::numeric_limits<char>::max()
« Reply #4 on: November 04, 2022, 02:43:14 am »
The workaround is trivial:
Code: [Select]
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