1 /*******************************************************************************
2 * Copyright 2016-2018 Intel Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *******************************************************************************/
34 inline const T abs(const T& a) {
35 return a >= 0 ? a : -a;
39 inline const T& max(const T& a, const T& b) {
44 inline const T& min(const T& a, const T& b) {
48 template<typename T> void swap(T& t1, T& t2) {
54 // Rationale: MKL-DNN needs numeric limits implementation that does not
55 // generate dependencies on C++ run-time libraries.
57 template<typename T> struct numeric_limits;
59 template<> struct numeric_limits<float> {
60 static constexpr float lowest() { return -FLT_MAX; }
61 static constexpr float max() { return FLT_MAX; }
64 template<> struct numeric_limits<int32_t> {
65 static constexpr int lowest() { return INT32_MIN; }
66 static constexpr int max() { return INT32_MAX; }
69 template<> struct numeric_limits<int16_t> {
70 static constexpr int16_t lowest() { return INT16_MIN; }
71 static constexpr int16_t max() { return INT16_MAX; }
74 template<> struct numeric_limits<int8_t> {
75 static constexpr int8_t lowest() { return INT8_MIN; }
76 static constexpr int8_t max() { return INT8_MAX; }
79 template<> struct numeric_limits<uint8_t> {
80 static constexpr uint8_t lowest() { return 0; }
81 static constexpr uint8_t max() { return UINT8_MAX; }
84 template<typename T> struct is_integral
85 { static constexpr bool value = false; };
86 template<> struct is_integral<int32_t> { static constexpr bool value = true; };
87 template<> struct is_integral<int16_t> { static constexpr bool value = true; };
88 template<> struct is_integral<int8_t> { static constexpr bool value = true; };
89 template<> struct is_integral<uint8_t> { static constexpr bool value = true; };
91 template <typename T, typename U> struct is_same
92 { static constexpr bool value = false; };
93 template <typename T> struct is_same<T, T>
94 { static constexpr bool value = true; };
96 // Rationale: MKL-DNN needs container implementations that do not generate
97 // dependencies on C++ run-time libraries.
99 // Implementation philosophy: caller is responsible to check if the operation
100 // is valid. The only functions that have to return status are those that
101 // depend on memory allocation or similar operations.
103 // This means that e.g. an operator [] does not have to check for boundaries.
104 // The caller should have checked the boundaries. If it did not we crash and
105 // burn: this is a bug in MKL-DNN and throwing an exception would not have been
108 // On the other hand, insert() or resize() or a similar operation needs to
109 // return a status because the outcome depends on factors external to the
110 // caller. The situation is probably also not recoverable also, but MKL-DNN
111 // needs to be nice and report "out of memory" to the users.
118 template <typename T> class vector: public c_compatible {
120 std::vector<T> _impl;
122 typedef typename std::vector<T>::iterator iterator;
123 typedef typename std::vector<T>::const_iterator const_iterator;
124 typedef typename std::vector<T>::size_type size_type;
126 vector(size_type n): _impl(n) {}
127 vector(size_type n, const T &value): _impl(n, value) {}
128 template <typename input_iterator>
129 vector(input_iterator first, input_iterator last): _impl(first, last) {}
131 size_type size() const { return _impl.size(); }
132 T& operator[] (size_type i) { return _impl[i]; }
133 const T& operator[] (size_type i) const { return _impl[i]; }
134 iterator begin() { return _impl.begin(); }
135 const_iterator begin() const { return _impl.begin(); }
136 iterator end() { return _impl.end(); }
137 const_iterator end() const { return _impl.end(); }
138 template <typename input_iterator>
139 nstl_status_t insert(iterator pos, input_iterator begin, input_iterator end)
141 _impl.insert(pos, begin, end);
144 void clear() { _impl.clear(); }
145 void push_back(const T& t) { _impl.push_back(t); }
146 void resize(size_type count) { _impl.resize(count); }
147 void reserve(size_type count) { _impl.reserve(count); }
150 template <typename Key, typename T> class map: public c_compatible {
152 std::map<Key, T> _impl;
154 typedef typename std::map<Key, T>::iterator iterator;
155 typedef typename std::map<Key, T>::const_iterator const_iterator;
156 typedef typename std::map<Key, T>::size_type size_type;
159 size_type size() const { return _impl.size(); }
160 T& operator[](const Key &k) { return _impl[k]; }
161 const T& operator[](const Key &k) const { return _impl[k]; }
162 iterator begin() { return _impl.begin(); }
163 const_iterator begin() const { return _impl.begin(); }
164 iterator end() { return _impl.end(); }
165 const_iterator end() const { return _impl.end(); }
166 template <typename input_iterator>
167 void clear() { _impl.clear(); }
176 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s