Embedded Template Library  1.0
power.h
Go to the documentation of this file.
1 
3 /******************************************************************************
4 The MIT License(MIT)
5 
6 Embedded Template Library.
7 https://github.com/ETLCPP/etl
8 https://www.etlcpp.com
9 
10 Copyright(c) 2014 jwellbelove
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files(the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions :
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29 ******************************************************************************/
30 
31 #ifndef ETL_POW_INCLUDED
32 #define ETL_POW_INCLUDED
33 
34 #include <stddef.h>
35 #include <stdint.h>
36 
37 #include "platform.h"
38 #include "log.h"
39 
43 
44 namespace etl
45 {
46  namespace private_power
47  {
48 #if ETL_NOT_USING_64BIT_TYPES
49  typedef uint32_t type;
50 #else
51  typedef uint64_t type;
52 #endif
53  }
54 
55  //***************************************************************************
59  //***************************************************************************
60  template <const size_t NV, const size_t POWER>
61  struct power
62  {
63  static ETL_CONSTANT private_power::type value = NV * power<NV, POWER - 1>::value;
64  };
65 
66  //***************************************************************************
70  //***************************************************************************
71  template <const size_t NV>
72  struct power<NV, 0>
73  {
74  static ETL_CONSTANT private_power::type value = 1;
75  };
76 
77 #if ETL_CPP17_SUPPORTED
78  template <size_t NV, size_t POWER>
79  inline constexpr size_t power_v = power<NV, POWER>::value;
80 #endif
81 
82  //***************************************************************************
85  //***************************************************************************
86  template <const size_t NV>
88  {
89  enum value_type
90  {
91  value = 1 << (etl::log2<NV - 1>::value + 1)
92  };
93  };
94 
95  //***************************************************************************
99  //***************************************************************************
100  template <>
102  {
103  enum value_type
104  {
105  value = 2
106  };
107  };
108 
109 #if ETL_CPP17_SUPPORTED
110  template <size_t NV>
111  inline constexpr size_t power_of_2_round_up_v = power_of_2_round_up<NV>::value;
112 #endif
113 
114  //***************************************************************************
117  //***************************************************************************
118  template <const size_t NV>
120  {
121  enum value_type
122  {
123  value = 1 << (etl::log2<NV - 1>::value)
124  };
125  };
126 
127  //***************************************************************************
131  //***************************************************************************
132  template <>
134  {
135  enum value_type
136  {
137  value = 2
138  };
139  };
140 
141  //***************************************************************************
145  //***************************************************************************
146  template <>
148  {
149  enum value_type
150  {
151  value = 2
152  };
153  };
154 
155  //***************************************************************************
159  //***************************************************************************
160  template <>
162  {
163  enum value_type
164  {
165  value = 2
166  };
167  };
168 
169 #if ETL_CPP17_SUPPORTED
170  template <size_t NV>
171  inline constexpr size_t power_of_2_round_down_v = power_of_2_round_down<NV>::value;
172 #endif
173 
174  //***************************************************************************
177  //***************************************************************************
178  template <const size_t NV>
180  {
181  static ETL_CONSTANT bool value = (NV & (NV - 1)) == 0;
182  };
183 
184  //***************************************************************************
188  //***************************************************************************
189  template <>
190  struct is_power_of_2<0>
191  {
192  static ETL_CONSTANT bool value = false;
193  };
194 
195  //***************************************************************************
199  //***************************************************************************
200  template <>
201  struct is_power_of_2<1>
202  {
203  static ETL_CONSTANT bool value = false;
204  };
205 
206 #if ETL_CPP17_SUPPORTED
207  template <size_t NV>
208  inline constexpr size_t is_power_of_2_v = is_power_of_2<NV>::value;
209 #endif
210 }
211 
212 #endif
Definition: log.h:99
Definition: power.h:180
Definition: power.h:62
Definition: power.h:120
Definition: power.h:88
Definition: absolute.h:37