Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / mkl-dnn / src / common / nstl.hpp
1 /*******************************************************************************
2 * Copyright 2016-2018 Intel Corporation
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 NSTL_HPP
18 #define NSTL_HPP
19
20 #include <stdint.h>
21 #include <limits.h>
22 #include <float.h>
23
24 #include <vector>
25 #include <map>
26
27 #include "utils.hpp"
28
29 namespace mkldnn {
30 namespace impl {
31 namespace nstl {
32
33 template<typename T>
34 inline const T abs(const T& a) {
35     return a >= 0 ? a : -a;
36 }
37
38 template<typename T>
39 inline const T& max(const T& a, const T& b) {
40     return a > b ? a : b;
41 }
42
43 template<typename T>
44 inline const T& min(const T& a, const T& b) {
45     return a < b ? a : b;
46 }
47
48 template<typename T> void swap(T& t1, T& t2) {
49     T tmp(t1);
50     t1 = t2;
51     t2 = tmp;
52 }
53
54 // Rationale: MKL-DNN needs numeric limits implementation that does not
55 // generate dependencies on C++ run-time libraries.
56
57 template<typename T> struct numeric_limits;
58
59 template<> struct numeric_limits<float> {
60     static constexpr float lowest() { return -FLT_MAX; }
61     static constexpr float max() { return FLT_MAX; }
62 };
63
64 template<> struct numeric_limits<int32_t> {
65     static constexpr int lowest() { return INT32_MIN; }
66     static constexpr int max() { return INT32_MAX; }
67 };
68
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; }
72 };
73
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; }
77 };
78
79 template<> struct numeric_limits<uint8_t> {
80     static constexpr uint8_t lowest() { return 0; }
81     static constexpr uint8_t max() { return UINT8_MAX; }
82 };
83
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; };
90
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; };
95
96 // Rationale: MKL-DNN needs container implementations that do not generate
97 // dependencies on C++ run-time libraries.
98 //
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.
102 //
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
106 // recoverable.
107 //
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.
112
113 enum nstl_status_t {
114     success = 0,
115     out_of_memory
116 };
117
118 template <typename T> class vector: public c_compatible {
119 private:
120     std::vector<T> _impl;
121 public:
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;
125     vector() {}
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) {}
130     ~vector() {}
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)
140     {
141         _impl.insert(pos, begin, end);
142         return success;
143     }
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); }
148 };
149
150 template <typename Key, typename T> class map: public c_compatible {
151 private:
152     std::map<Key, T> _impl;
153 public:
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;
157     map() {}
158     ~map() {}
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(); }
168 };
169
170 }
171 }
172 }
173
174 #endif
175
176 // vim: et ts=4 sw=4 cindent cino^=l0,\:0,N-s