Support packets in flight
[samplevnf.git] / VNFs / DPPD-PROX / random.h
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 /*
18   This pseudorandom number generator is based on ref_xorshift128plus,
19   as implemented by reference_xorshift.h, which has been obtained
20   from https://sourceforge.net/projects/xorshift-cpp/
21
22   The licensing terms for reference_xorshift.h are reproduced below.
23
24   //  Written in 2014 by Ivo Doko (ivo.doko@gmail.com)
25   //  based on code written by Sebastiano Vigna (vigna@acm.org)
26   //  To the extent possible under law, the author has dedicated
27   //  all copyright and related and neighboring rights to this
28   //  software to the public domain worldwide. This software is
29   //  distributed without any warranty.
30   //  See <http://creativecommons.org/publicdomain/zero/1.0/>.
31 */
32
33 #ifndef _RANDOM_H_
34 #define _RANDOM_H_
35
36 #include <rte_cycles.h>
37
38 struct random {
39   uint64_t state[2];
40 };
41
42 static void random_init_seed(struct random *random)
43 {
44   random->state[0] = rte_rdtsc();
45   random->state[1] = rte_rdtsc();
46 }
47
48 static uint64_t random_next(struct random *random)
49 {
50   const uint64_t s0 = random->state[1];
51   const uint64_t s1 = random->state[0] ^ (random->state[0] << 23);
52
53   random->state[0] = random->state[1];
54   random->state[1] = (s1 ^ (s1 >> 18) ^ s0 ^ (s0 >> 5)) + s0;
55   return random->state[1];
56 }
57
58 #endif /* _RANDOM_H_ */