InaUtils.hpp 1.05 KB
Newer Older
1 2 3 4 5 6 7
///////////////////////////////////////////////////////////////////////////
// Inastemp - Berenger Bramas MPCDF - 2016
// Under MIT Licence, please you must read the LICENCE file.
///////////////////////////////////////////////////////////////////////////
#ifndef UTILS_HPP
#define UTILS_HPP

8 9 10
// for std::size_t
#include <cstdio>

11 12 13 14 15 16 17 18 19 20 21 22 23 24
namespace InaUtils {

template < class TypeSrc, class TypeDest >
inline TypeDest ConvertBits(TypeSrc src) {
    union InaBits {
        TypeSrc src;
        TypeDest dest;
    };
    InaBits bits;
    bits.src = src;
    return bits.dest;
}

template <class VecType>
25
inline VecType FastPow(VecType base, std::size_t power){
26
    VecType res = VecType(1.);
27 28 29 30 31 32 33 34 35 36 37 38

    while(power){
        if(1 & power){
            res *= base;
        }
        base *= base;
        power >>= 1;
    }

    return res;
}

39 40 41 42 43 44 45 46 47 48 49 50 51 52
inline std::size_t FastPowNbMul(std::size_t power){
    std::size_t nbMul = 0;

    while(power){
        if(1 & power){
            nbMul += 1;
        }
        nbMul += 1;
        power >>= 1;
    }

    return nbMul;
}

53 54 55
}

#endif // UTILS_HPP