vendredi 14 février 2020

xoshiro256p copy-pasted code results in 'invalid operands'

This is probably something very basic, and most probably related to misusing pointers/references, but I can't figure it out, what should I change to make it run? I tried copy-pasting the xoshiro256p random number generator from Wikipedia for a C++ test, but it fails with

xoshiro256p.cpp: In function ‘uint64_t 
xoshiro256p(xoshiro256p_state*)’:
xoshiro256p.cpp:17:32: error: invalid operands of types ‘uint64_t [4]’ {aka ‘long unsigned int [4]’} and ‘uint64_t [4]’ {aka ‘long unsigned int [4]’} to binary ‘operator+’

It's not the only error, there are other invalid operands, but this is the first. I replaced stdint.h with cstdint, no complaints, added iostream for std::cout, and that's about it. This is the full source:

#include <iostream>
#include <cstdint>

uint64_t rol64(uint64_t x, int k)
{
  return (x << k) | (x >> (64 - k));
}

struct xoshiro256p_state
{
  uint64_t s[4];
};

uint64_t xoshiro256p(struct xoshiro256p_state *state)
{
  uint64_t (*s)[4] = &state->s;
  uint64_t const result = s[0] + s[3];
  uint64_t const t = s[1] << 17;

  s[2] ^= s[0];
  s[3] ^= s[1];
  s[1] ^= s[2];
  s[0] ^= s[3];

  s[2] ^= t;
  s[3] = rol64(s[3], 45);

  return result;
}

int main()
{
  xoshiro256p_state *s {new xoshiro256p_state()};
  std::cout << xoshiro256p(s) << '\n';

  return 0;
}

The I tried replacing the C style array with std::array:

struct xoshiro256p_state { std::array<int, 4> s; );
...
std::array<int, 4> *s {&state->s};

but I get the same erros (plus a ton of others).




Aucun commentaire:

Enregistrer un commentaire