imneme / pcg-cpp

PCG — C++ Implementation
Apache License 2.0
744 stars 99 forks source link

Build and run-time failuers on MacOS for PowerPC #93

Open rstub opened 3 months ago

rstub commented 3 months ago

While debugging a build failure in https://github.com/daqana/dqrng/issues/88 together with @barracuda156 we found some issues with using PCG on MacOS for PowerPC:

Consider

#include <iostream>
#include "pcg_random.hpp"

int main(void) {
  pcg64 rng;
  rng.seed(20240409);
  rng.set_stream(90404202);
  std::cout << rng << std::endl;
  return 0;
}

Compiling this gives the error:

36-25% /opt/local/bin/g++-mp-14 -arch ppc -I./pcg-cpp/include -o pcg_test pcg_test.cpp
In file included from pcg_test.cpp:2:
./pcg-cpp/include/pcg_random.hpp: In instantiation of 'void pcg_detail::specific_stream<itype>::set_stream(itype) [with itype = pcg_extras::uint_x4<unsigned int, long long unsigned int>]':
pcg_test.cpp:7:17:   required from here
    7 |   rng.set_stream(90404202);
      |   ~~~~~~~~~~~~~~^~~~~~~~~~
./pcg-cpp/include/pcg_random.hpp:336:37: error: no match for 'operator|' (operand types are 'pcg_extras::uint_x4<unsigned int, long long unsigned int>' and 'in')
  336 |          inc_ = (specific_seq << 1) | 1;
      |                 ~~~~~~~~~~~~~~~~~~~~^~~
./pcg-cpp/include/pcg_random.hpp:336:37: note: candidate: 'operator|(int, int)' (built-in)
./pcg-cpp/include/pcg_random.hpp:336:37: note:   no known conversion for argument 1 from 'pcg_extras::uint_x4<unsigned int, long long unsigned int>' to 'int'
./pcg-cpp/include/pcg_random.hpp:336:37: note: candidate: 'operator|(long long unsigned int, int)' (built-in)
./pcg-cpp/include/pcg_random.hpp:336:37: note:   no known conversion for argument 1 from 'pcg_extras::uint_x4<unsigned int, long long unsigned int>' to 'long long unsigned int'
In file included from ./pcg-cpp/include/pcg_extras.hpp:84,
                 from ./pcg-cpp/include/pcg_random.hpp:114:
./pcg-cpp/include/pcg_uint128.hpp:837:22: note: candidate: 'template<class UInt, class UIntX2> pcg_extras::uint_x4<U, V> pcg_extras::operator|(const uint_x4<U, V>&, const uint_x4<U, V>&)'
  837 | uint_x4<UInt,UIntX2> operator|(const uint_x4<UInt,UIntX2>& a,
      |                      ^~~~~~~~
./pcg-cpp/include/pcg_uint128.hpp:837:22: note:   template argument deduction/substitution failed:
./pcg-cpp/include/pcg_random.hpp:336:37: note:   mismatched types 'const pcg_extras::uint_x4<U, V>' and 'int'

This can be worked around by applying PR #92. However, one then encounters a segmentation fault:

36-25% /opt/local/bin/g++-mp-14 -arch ppc -I./pcg-cpp/include -o pcg_test pcg_test.cpp
36-25% ./pcg_test
zsh: segmentation fault  ./pcg_test

Backtrace:

36-25% sudo /opt/local/bin/gdb-apple ./pcg_test
Password:
GNU gdb 6.3.50.20050815-cvs (Wed Apr 13 04:55:50 UTC 2022)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "--host=powerpc-apple-darwin10.0.0d2 --target="...Reading symbols for shared libraries ..... done

(gdb) run
Starting program: /Users/svacchanda/Dev_tmp/dqrng_test_tmp/pcg_test 
Reading symbols for shared libraries ++++.. done

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x037ffffa
0x00002924 in pcg_extras::operator<< <unsigned int, unsigned long long> ()
(gdb) bt
#0  0x00002924 in pcg_extras::operator<< <unsigned int, unsigned long long> ()
#1  0x000038d4 in pcg_extras::divmod<unsigned int, unsigned long long> ()

Do you need any further information?

barracuda156 commented 3 months ago

This might be relevant for Big-endian (32-bit?) platforms in general, though I cannot test it at the moment elsewhere.

Has anyone tested this on Linux ppc?