scc  2022.4.0
SystemC components library
sc_attribute_randomized.h
1 /*******************************************************************************
2  * Copyright 2022 MINRES Technologies GmbH
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 _SCC_SC_ATTRIBUTE_RANDOMIZED_H_
18 #define _SCC_SC_ATTRIBUTE_RANDOMIZED_H_
19 
20 #include "mt19937_rng.h"
21 #include <sysc/kernel/sc_attribute.h>
22 #include <type_traits>
23 
29 namespace scc {
30 template <typename T> class sc_attribute_randomized : public sc_core::sc_attribute<T> {
31 public:
32  sc_attribute_randomized(const std::string& name_)
33  : sc_core::sc_attribute<T>(name_) {}
34 
35  sc_attribute_randomized(const std::string& name_, const T& value_)
36  : sc_core::sc_attribute<T>(name_) {}
37 
38  sc_attribute_randomized(const sc_core::sc_attribute<T>& a)
39  : sc_core::sc_attribute<T>(a.name()) {}
40 
41  // destructor (does nothing)
42 
43  virtual ~sc_attribute_randomized() {}
44 
45  template <bool cond, typename U> using resolvedType = typename std::enable_if<cond, U>::type;
46 
47  template <typename U = T> resolvedType<std::is_signed<T>::value, U> get_value() {
48  if(this->value < 0)
49  return MT19937::uniform(0, -this->value);
50  else
51  return this->value;
52  }
53 
54  template <typename U = T> resolvedType<std::is_unsigned<T>::value, U> get_value() { return this->value; }
55 };
56 } // namespace scc // end of scc-sysc
58 #endif /* _SCC_SC_ATTRIBUTE_RANDOMIZED_H_ */
static uint64_t uniform()
Definition: mt19937_rng.h:60
SCC SystemC utilities.