fml  0.1-0
Fused Matrix Library
rand.hh
1 // This file is part of fml which is released under the Boost Software
2 // License, Version 1.0. See accompanying file LICENSE or copy at
3 // https://www.boost.org/LICENSE_1_0.txt
4 
5 #ifndef FML__INTERNALS_RAND_H
6 #define FML__INTERNALS_RAND_H
7 #pragma once
8 
9 
10 #include <ctime>
11 
12 #include "platform.h"
13 
14 #if OS_WINDOWS
15 #include <process.h>
16 #elif OS_NIX
17 #include <sys/types.h>
18 #include <unistd.h>
19 #endif
20 
21 
22 namespace fml
23 {
24  namespace rand
25  {
26  namespace
27  {
28  // Robert Jenkins' 96-bit mix function
29  inline uint32_t mix_96(uint32_t a, uint32_t b, uint32_t c)
30  {
31  a=a-b; a=a-c; a=a^(c >> 13);
32  b=b-c; b=b-a; b=b^(a << 8);
33  c=c-a; c=c-b; c=c^(b >> 13);
34  a=a-b; a=a-c; a=a^(c >> 12);
35  b=b-c; b=b-a; b=b^(a << 16);
36  c=c-a; c=c-b; c=c^(b >> 5);
37  a=a-b; a=a-c; a=a^(c >> 3);
38  b=b-c; b=b-a; b=b^(a << 10);
39  c=c-a; c=c-b; c=c^(b >> 15);
40 
41  return c;
42  }
43  }
44 
45 
46 
47  inline uint32_t get_seed()
48  {
49  uint32_t pid;
50  uint32_t ret;
51 
52  #if OS_WINDOWS
53  pid = _getpid();
54  #elif OS_NIX
55  pid = (uint32_t) getpid();
56  #else
57  #error "Unable to get PID"
58  #endif
59 
60  ret = mix_96((uint32_t) time(NULL), (uint32_t) clock(), pid);
61 
62  return ret;
63  }
64  }
65 }
66 
67 
68 #endif
fml
Core namespace.
Definition: dimops.hh:10