MatchLib
nvhls_rand.h
1 /*
2  * Copyright (c) 2016-2024, 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 <TypeToBits.h>
23 
24 namespace nvhls {
60 inline int set_random_seed() {
61  unsigned int seed = 0;
62 #ifdef NVHLS_RAND_SEED
63  seed = (NVHLS_RAND_SEED);
64 #endif
65  const char* env_rand_seed = std::getenv("NVHLS_RAND_SEED");
66  if (env_rand_seed != NULL) seed = atoi(env_rand_seed);
67  srand(seed);
68  cout << "================================" << endl;
69  cout << dec << "SETTING RANDOM SEED = " << seed << endl;
70  cout << "================================" << endl;
71  return seed;
72 }
73 
92 template < typename Payload>
93 Payload gen_random_payload() {
94  Payload payload;
95  NVUINTW(Payload::width) random = rand();
96  int num = Payload::width/32 + 1;
97  for (int i = 0; i < num; i++) {
98  random = (random << 32) + rand();
99  }
100  sc_lv<Payload::width> payload_lv = TypeToBits<NVUINTW(Payload::width) > (random);
101  payload = BitsToType<Payload> (payload_lv);
102  return payload;
103 }
122 template < int bitwidth>
123 NVUINTW(bitwidth) get_rand() {
124  NVUINTW(bitwidth) random = rand();
125  int num = bitwidth/32 + 1;
126  for (int i = 0; i < num; i++) {
127  random = (random << 32) + rand();
128  }
129  return random;
130 }
131 
132 }
133 #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:93
int set_random_seed()
Set random seed.
Definition: nvhls_rand.h:60