Project Ne10
An Open Optimized Software Library Project for the ARM Architecture
Loading...
Searching...
No Matches
NE10_random.h
1/*
2 * Copyright 2012-15 ARM Limited and Contributors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of ARM Limited nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY ARM LIMITED AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL ARM LIMITED AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28/*
29 * NE10 Library : test/include/NE10_random.h
30 */
31
32
33#ifndef __NE10_RANDOM__
34#define __NE10_RANDOM__
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <stdint.h>
39#include <float.h>
40#include <math.h>
41#include <assert.h>
42
43
44// used for creating different instances of random number generators with different seeds and states
45typedef struct
46{
47 // these are used as internal values, please do not change them directly
48 uint32_t _private_m_A ;// = 1103515245L; // a, must be 0 < _A < _M
49 uint32_t _private_m_C ;// = 12345L; // c, must be 0 < _C < _M
50 uint32_t _private_m_X_NM1 ;// = 0; // X(n-1), at first this value is the seed or the start value
52
53typedef struct
54{
55 // these are used as internal values, please do not change them directly
56 // there are three separate seeds for 1) the sign, 2) the exponent, 3) and the fraction bits.
57 NE10_rng_t _private_m_rngs[3];
59
60// a random number generator that generates IEEE 754 float numbers
61// NAN_OR_INF is to check whether the value is a NAN or an INF
62#define NAN_OR_INF (0xFF << 23)
63#define IS_NAN_OR_INF(x) ( ((x & NAN_OR_INF) == NAN_OR_INF)?1:0 )
64
65#define EXPONENT_MASK 0x807FFFFF
66#define IS_SUBNORMAL(x) ( ((x & EXPONENT_MASK) == x)?1:0 )
67
68// generic functions
69extern void NE10_rng_init_g (NE10_rng_t *rng, uint32_t seed);
70
71extern uint32_t NE10_rng_next_g (NE10_rng_t *rng);
72
73extern const uint32_t NE10_rng_max_g (NE10_rng_t *rng);
74
75extern void NE10_rng_init (uint32_t seed);
76
77extern uint32_t NE10_rng_next();
78
79extern const uint32_t NE10_rng_max();
80
81extern void NE10_float_rng_init_g (NE10_float_rng_t* float_rng, uint32_t seed);
82
83extern float NE10_float_rng_next_g (NE10_float_rng_t* float_rng);
84
85extern float NE10_float_rng_max_g (NE10_float_rng_t* float_rng);
86
87extern void NE10_float_rng_init (uint32_t seed);
88
89extern float NE10_float_rng_next();
90
91extern float NE10_float_rng_max();
92
93extern void NE10_float_rng_limit_init (uint32_t seed);
94
95extern float NE10_float_rng_limit_next();
96
97extern float NE10_float_rng_limit_max();
98
99extern void NE10_float_rng_limit_gt1_init (uint32_t seed);
100
101extern float NE10_float_rng_limit_gt1_next();
102
103extern float NE10_float_rng_limit_gt1_max();
104
105#endif // NE10_RANDOM
106