Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers log.h Source File

log.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_LOG__
00032 #define __ETL_LOG__
00033 
00034 #include <stddef.h>
00035 
00036 #include "platform.h "
00037 
00038 ///\defgroup log log
00039 /// log<N, BASE> : Calculates logs to any base, rounded down to the nearest integer.<br>
00040 /// log2<N>      : Calculates logs to base 2, rounded down to the nearest integer.<br>
00041 /// log10<N>     : Calculates logs to base 10, rounded down to the nearest integer.<br>
00042 ///\ingroup maths
00043 
00044 namespace etl 
00045 {
00046   //***************************************************************************
00047   ///\ingroup log
00048   /// The base generic log template.
00049   /// Defines <b>value</b> as the log of the number at the specified base.
00050   /// The result is rounded down to the next integer.
00051   ///\tparam NV   The number to find the log of.
00052   ///\tparam BASE The base of the log.
00053   //***************************************************************************
00054   template <const size_t NV, const size_t BASE>
00055   struct log
00056   {
00057     enum value_type
00058     {
00059       // Recursive definition.
00060       value = (NV >= BASE) ? 1 + log<NV / BASE, BASE>::value : 0
00061     };
00062   };
00063 
00064   //***************************************************************************
00065   // Specialisation for N = 1
00066   //***************************************************************************
00067   template <const size_t BASE>
00068   struct log<1, BASE>
00069   {
00070     enum value_type
00071     {
00072       value = 0
00073     };
00074   };
00075 
00076   //***************************************************************************
00077   // Specialisation for N = 0
00078   //***************************************************************************
00079   template <const size_t BASE>
00080   struct log<0, BASE>
00081   {
00082     enum value_type
00083     {
00084       value = 0
00085     };
00086   };
00087 
00088   //***************************************************************************
00089   ///\ingroup log
00090   /// Calculates base 2 logs.
00091   //***************************************************************************
00092   template <const size_t NV>
00093   struct log2
00094   {
00095     enum value_type
00096     {
00097       value = log<NV, 2>::value
00098     };
00099   };
00100 
00101   //***************************************************************************
00102   ///\ingroup log
00103   /// Calculates base 10 logs.
00104   //***************************************************************************
00105   template <const size_t NV>
00106   struct log10
00107   {
00108     enum value_type
00109     {
00110       value = log<NV, 10>::value
00111     };
00112   };
00113 }
00114 
00115 #endif
00116