|
| 1 | +/*---------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * Copyright (C) 2016 - 2020 Antonio Augusto Alves Junior |
| 4 | + * |
| 5 | + * This file is part of Hydra Data Analysis Framework. |
| 6 | + * |
| 7 | + * Hydra is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * Hydra is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with Hydra. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + * |
| 20 | + *---------------------------------------------------------------------------*/ |
| 21 | + |
| 22 | +/* |
| 23 | + * SeedRNG.h |
| 24 | + * |
| 25 | + * Created on: 30/06/2020 |
| 26 | + * Author: Antonio Augusto Alves Junior |
| 27 | + */ |
| 28 | + |
| 29 | +#ifndef SEEDRNG_H_ |
| 30 | +#define SEEDRNG_H_ |
| 31 | + |
| 32 | + |
| 33 | +#include <hydra/detail/Config.h> |
| 34 | +#include <stdint.h> |
| 35 | + |
| 36 | + |
| 37 | +namespace hydra { |
| 38 | + |
| 39 | +class SeedRNG |
| 40 | +{ |
| 41 | +public: |
| 42 | + |
| 43 | + __hydra_host__ __hydra_device__ |
| 44 | + SeedRNG(size_t state=1337 ): |
| 45 | + fState(state) |
| 46 | + {} |
| 47 | + |
| 48 | + __hydra_host__ __hydra_device__ |
| 49 | + SeedRNG(SeedRNG const& other): |
| 50 | + fState(other.GetState()) |
| 51 | + {} |
| 52 | + |
| 53 | + __hydra_host__ __hydra_device__ |
| 54 | + inline SeetRNG& operator=(SeedRNG const& other) |
| 55 | + { |
| 56 | + if(this==&other) return *this; |
| 57 | + |
| 58 | + fState=other.GetState(); |
| 59 | + |
| 60 | + return *this; |
| 61 | + } |
| 62 | + |
| 63 | + __hydra_host__ __hydra_device__ |
| 64 | + size_t GetState() const { |
| 65 | + return fState; |
| 66 | + } |
| 67 | + |
| 68 | + __hydra_host__ __hydra_device__ |
| 69 | + void SetState(size_t state) { |
| 70 | + fState = state; |
| 71 | + } |
| 72 | + |
| 73 | + __hydra_host__ __hydra_device__ |
| 74 | + size_t operator()() |
| 75 | + { |
| 76 | + size_t z = (fState += 0x9e3779b97f4a7c15); |
| 77 | + z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; |
| 78 | + z = (z ^ (z >> 27)) * 0x94d049bb133111eb; |
| 79 | + return z ^ (z >> 31); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +private: |
| 84 | + |
| 85 | + size_t fState; |
| 86 | + |
| 87 | +}; |
| 88 | + |
| 89 | +} // namespace hydra |
| 90 | + |
| 91 | +#endif /* SEEDRNG_H_ */ |
0 commit comments