Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers power.h Source File

power.h

Go to the documentation of this file.
00001 ///\file
00002 
00003 /******************************************************************************
00004 The MIT License(MIT)
00005 
00006 Embedded Template Library.
00007 https://github.com/ETLCPP/etl
00008 http://www.etlcpp.com
00009 
00010 Copyright(c) 2014 jwellbelove
00011 
00012 Permission is hereby granted, free of charge, to any person obtaining a copy
00013 of this software and associated documentation files(the "Software"), to deal
00014 in the Software without restriction, including without limitation the rights
00015 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
00016 copies of the Software, and to permit persons to whom the Software is
00017 furnished to do so, subject to the following conditions :
00018 
00019 The above copyright notice and this permission notice shall be included in all
00020 copies or substantial portions of the Software.
00021 
00022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00023 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00024 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
00025 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00026 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00027 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00028 SOFTWARE.
00029 ******************************************************************************/
00030 
00031 #ifndef __ETL_POW__
00032 #define __ETL_POW__
00033 
00034 #include <stddef.h>
00035 #include <stdint.h>
00036 
00037 #include "platform.h "
00038 #include "log.h "
00039 
00040 ///\defgroup power power
00041 /// power<N, POWER> : Calculates N to the power POWER.
00042 ///\ingroup maths
00043 
00044 namespace etl 
00045 {
00046   //***************************************************************************
00047   ///\ingroup power
00048   /// Calculates powers.
00049   ///\note Only supports positive N.
00050   //***************************************************************************
00051   template <const size_t NV, const size_t POWER>
00052   struct power
00053   {
00054     static const uint64_t value = NV * power<NV, POWER - 1>::value;
00055   };
00056 
00057   //***************************************************************************
00058   /// Calculates powers.
00059   ///\note Only supports positive N.
00060   /// Specialisation for POWER == 0.
00061   //***************************************************************************
00062   template <const size_t NV>
00063   struct power<NV, 0>
00064   {
00065     static const uint64_t value = 1;
00066   };
00067 
00068   //***************************************************************************
00069   ///\ingroup power
00070   /// Calculates the rounded up power of 2.
00071   //***************************************************************************
00072   template <const size_t NV>
00073   struct power_of_2_round_up
00074   {
00075     enum value_type
00076     {
00077       value = 1 << (etl::log2<NV - 1>::value + 1)
00078     };
00079   };
00080 
00081   //***************************************************************************
00082   ///\ingroup power
00083   /// Calculates the rounded up power of 2.
00084   /// Specialisation for 0.
00085   //***************************************************************************
00086   template <>
00087   struct power_of_2_round_up<0>
00088   {
00089     enum value_type
00090     {
00091       value = 2
00092     };
00093   };
00094 
00095   //***************************************************************************
00096   ///\ingroup power
00097   /// Calculates the rounded down power of 2.
00098   //***************************************************************************
00099   template <const size_t NV>
00100   struct power_of_2_round_down
00101   {
00102     enum value_type
00103     {
00104       value = 1 << (etl::log2<NV - 1>::value)
00105     };
00106   };
00107 
00108   //***************************************************************************
00109   ///\ingroup power
00110   /// Calculates the rounded down power of 2.
00111   /// Specialisation for 0.
00112   //***************************************************************************
00113   template <>
00114   struct power_of_2_round_down<0>
00115   {
00116     enum value_type
00117     {
00118       value = 2
00119     };
00120   };
00121 
00122   //***************************************************************************
00123   ///\ingroup power
00124   /// Calculates the rounded down power of 2.
00125   /// Specialisation for 1.
00126   //***************************************************************************
00127   template <>
00128   struct power_of_2_round_down<1>
00129   {
00130     enum value_type
00131     {
00132       value = 2
00133     };
00134   };
00135 
00136   //***************************************************************************
00137   ///\ingroup power
00138   /// Calculates the rounded down power of 2.
00139   /// Specialisation for 2.
00140   //***************************************************************************
00141   template <>
00142   struct power_of_2_round_down<2>
00143   {
00144     enum value_type
00145     {
00146       value = 2
00147     };
00148   };
00149 
00150   //***************************************************************************
00151   ///\ingroup power
00152   /// Checks if N is a power of 2.
00153   //***************************************************************************
00154   template <const size_t NV>
00155   struct is_power_of_2
00156   {
00157     static const bool value = (NV & (NV - 1)) == 0;
00158   };
00159 
00160   //***************************************************************************
00161   ///\ingroup power
00162   /// Checks if N is a power of 2.
00163   /// Specialisation for 0.
00164   //***************************************************************************
00165   template <>
00166   struct is_power_of_2<0>
00167   {
00168     static const bool value = false;
00169   };
00170 
00171   //***************************************************************************
00172   ///\ingroup power
00173   /// Checks if N is a power of 2.
00174   /// Specialisation for 1.
00175   //***************************************************************************
00176   template <>
00177   struct is_power_of_2<1>
00178   {
00179     static const bool value = false;
00180   };
00181 }
00182 
00183 #endif
00184