MatchLib
nvhls_rand.h
1 /*
2  * Copyright (c) 2016-2019, NVIDIA CORPORATION. All rights reserved.
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 #ifndef __NVHLS_RAND__
18 #define __NVHLS_RAND__
19 
20 #include <nvhls_int.h>
21 #include <nvhls_types.h>
22 #include <mem_array.h>
23 #include <TypeToBits.h>
24 
25 namespace nvhls {
61 inline int set_random_seed() {
62  unsigned int seed = time(NULL);
63 #ifdef RAND_SEED
64  seed = (RAND_SEED);
65 #endif
66  const char* env_rand_seed = std::getenv("RAND_SEED");
67  if (env_rand_seed != NULL) seed = atoi(env_rand_seed);
68  srand(seed);
69  cout << "================================" << endl;
70  cout << dec << "SETTING RANDOM SEED = " << seed << endl;
71  cout << "================================" << endl;
72  return seed;
73 }
74 
93 template < typename Payload>
94 Payload gen_random_payload() {
95  Payload payload;
96  NVUINTW(Payload::width) random = rand();
97  int num = Payload::width/32 + 1;
98  for (int i = 0; i < num; i++) {
99  random = (random << 32) + rand();
100  }
101  sc_lv<Payload::width> payload_lv = TypeToBits<NVUINTW(Payload::width) > (random);
102  payload = BitsToType<Payload> (payload_lv);
103  return payload;
104 }
123 template < int bitwidth>
124 NVUINTW(bitwidth) get_rand() {
125  NVUINTW(bitwidth) random = rand();
126  int num = bitwidth/32 + 1;
127  for (int i = 0; i < num; i++) {
128  random = (random << 32) + rand();
129  }
130  return random;
131 }
132 
133 }
134 #endif
NVUINTW(Wrapped< T >::width) TypeToNVUINT(T in)
Convert Type to NVUINT.
Definition: TypeToBits.h:115
Payload gen_random_payload()
Generate Random payload of any type.
Definition: nvhls_rand.h:94
int set_random_seed()
Set random seed.
Definition: nvhls_rand.h:61