Skip to content

Commit

Permalink
Merge pull request #25 from JohanMabille/ldexp
Browse files Browse the repository at this point in the history
ldexp
  • Loading branch information
JohanMabille committed Jun 1, 2017
2 parents 50eda8f + 909014c commit 59b26e4
Show file tree
Hide file tree
Showing 19 changed files with 2,717 additions and 960 deletions.
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ set(XSIMD_HEADERS
${XSIMD_INCLUDE_DIR}/xsimd/config/xsimd_config.hpp
${XSIMD_INCLUDE_DIR}/xsimd/config/xsimd_include.hpp
${XSIMD_INCLUDE_DIR}/xsimd/config/xsimd_instruction_set.hpp
${XSIMD_INCLUDE_DIR}/xsimd/math/xsimd_fp_manipulation.hpp
${XSIMD_INCLUDE_DIR}/xsimd/math/xsimd_fp_sign.hpp
${XSIMD_INCLUDE_DIR}/xsimd/math/xsimd_numerical_constant.hpp
${XSIMD_INCLUDE_DIR}/xsimd/math/xsimd_rounding.hpp
Expand All @@ -45,14 +46,17 @@ set(XSIMD_HEADERS
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_avx_conversion.hpp
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_avx_double.hpp
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_avx_float.hpp
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_avx_int.hpp
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_avx_int32.hpp
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_avx_int64.hpp
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_sse_conversion.hpp
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_sse_double.hpp
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_sse_float.hpp
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_sse_int.hpp
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_sse_int32.hpp
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_sse_int64.hpp
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_base.hpp
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_traits.hpp
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_types_include.hpp
${XSIMD_INCLUDE_DIR}/xsimd/types/xsimd_utils.hpp
)

if(BUILD_TESTS)
Expand Down
36 changes: 36 additions & 0 deletions include/xsimd/math/xsimd_fp_manipulation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/***************************************************************************
* Copyright (c) 2016, Johan Mabille and Sylvain Corlay *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/

#ifndef XSIMD_FP_MANIPULATION_HPP
#define XSIMD_FP_MANIPULATION_HPP

#include "xsimd_numerical_constant.hpp"

namespace xsimd
{

template <class T, std::size_t N>
batch<T, N> ldexp(const batch<T, N>& x, const batch<as_integer_t<T>, N>& e);

/**************************
* Generic implementation *
**************************/

template <class T, std::size_t N>
inline batch<T, N> ldexp(const batch<T, N>& x, const batch<as_integer_t<T>, N>& e)
{
using btype = batch<T, N>;
using itype = batch<as_integer_t<T>, N>;
itype ik = e + itype(maxexponent<T>());
ik = ik << nmb<T>();
return x * bitwise_cast<btype>(ik);
}

}

#endif
38 changes: 38 additions & 0 deletions include/xsimd/math/xsimd_numerical_constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ namespace xsimd
template <class T>
constexpr T maxflint() noexcept;

template <class T>
constexpr int32_t maxexponent() noexcept;

template <class T>
constexpr T minuszero() noexcept;

template <class T>
constexpr int32_t nmb() noexcept;

template <class T>
constexpr T twotonmb() noexcept;

Expand All @@ -44,6 +50,22 @@ namespace xsimd
return 9007199254740992.0;
}

/******************************
* maxexponent implementation *
******************************/

template<>
constexpr int32_t maxexponent<float>() noexcept
{
return 127;
}

template <>
constexpr int32_t maxexponent<double>() noexcept
{
return 1023;
}

/****************************
* minuszero implementation *
****************************/
Expand All @@ -66,6 +88,22 @@ namespace xsimd
return -0.0;
}

/**********************
* nmb implementation *
**********************/

template <>
constexpr int32_t nmb<float>() noexcept
{
return 23;
}

template <>
constexpr int32_t nmb<double>() noexcept
{
return 52;
}

/***************************
* twotonmb implementation *
***************************/
Expand Down
118 changes: 107 additions & 11 deletions include/xsimd/types/xsimd_avx_conversion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,53 @@

#include "xsimd_avx_float.hpp"
#include "xsimd_avx_double.hpp"
#include "xsimd_avx_int.hpp"
#include "xsimd_avx_int32.hpp"
#include "xsimd_avx_int64.hpp"

namespace xsimd
{
batch<int, 8> to_int(const batch<float, 8>& x);
batch<int, 8> to_int(const batch<double, 4>& x);

batch<float, 8> to_float(const batch<int, 8>& x);
/************************
* conversion functions *
************************/

/**************************************
* conversion function implementation *
**************************************/
batch<int32_t, 8> to_int(const batch<float, 8>& x);
batch<int64_t, 4> to_int(const batch<double, 4>& x);

inline batch<int, 8> to_int(const batch<float, 8>& x)
batch<float, 8> to_float(const batch<int32_t, 8>& x);
batch<double, 4> to_float(const batch<int64_t, 4>& x);

/******************
* cast functions *
******************/

template <class B>
B bitwise_cast(const batch<float, 8>& x);

template <class B>
B bitwise_cast(const batch<double, 4>& x);

template <class B>
B bitwise_cast(const batch<int32_t, 8>& x);

template <class B>
B bitwise_cast(const batch<int64_t, 4>& x);

/***************************************
* conversion functions implementation *
***************************************/

inline batch<int32_t, 8> to_int(const batch<float, 8>& x)
{
return _mm256_cvttps_epi32(x);
}

inline batch<int, 8> to_int(const batch<double, 4>& x)
inline batch<int64_t, 4> to_int(const batch<double, 4>& x)
{
#if XSIMD_X86_INSTR_SET >= XSIMD_X86_AVX2_VERSION
return _mm256_cvtepi32_epi64(_mm256_cvttpd_epi32(x));
#else
using batch_int = batch<int, 4>;
using batch_int = batch<int32_t, 4>;
__m128i tmp = _mm256_cvttpd_epi32(x);
__m128i res_low = _mm_unpacklo_epi32(tmp, batch_int(tmp) < batch_int(0));
__m128i res_high = _mm_unpackhi_epi32(tmp, batch_int(tmp) < batch_int(0));
Expand All @@ -43,10 +66,83 @@ namespace xsimd
#endif
}

inline batch<float, 8> to_float(const batch<int, 8>& x)
inline batch<float, 8> to_float(const batch<int32_t, 8>& x)
{
return _mm256_cvtepi32_ps(x);
}

inline batch<double, 4> to_float(const batch<int64_t, 4>& x)
{
return batch<double, 4>(static_cast<double>(x[0]),
static_cast<double>(x[1]),
static_cast<double>(x[2]),
static_cast<double>(x[3]));
}

/*********************************
* cast functions implementation *
*********************************/

template <>
inline batch<double, 4> bitwise_cast(const batch<float, 8>& x)
{
return _mm256_castps_pd(x);
}

template <>
inline batch<int32_t, 8> bitwise_cast(const batch<float, 8>& x)
{
return _mm256_castps_si256(x);
}

template <>
inline batch<int64_t, 4> bitwise_cast(const batch<float, 8>& x)
{
return _mm256_castps_si256(x);
}

template <>
inline batch<float, 8> bitwise_cast(const batch<double, 4>& x)
{
return _mm256_castpd_ps(x);
}

template <>
inline batch<int32_t, 8> bitwise_cast(const batch<double, 4>& x)
{
return _mm256_castpd_si256(x);
}

template <>
inline batch<int64_t, 4> bitwise_cast(const batch<double, 4>& x)
{
return _mm256_castpd_si256(x);
}

template <>
inline batch<float, 8> bitwise_cast(const batch<int32_t, 8>& x)
{
return _mm256_castsi256_ps(x);
}

template <>
inline batch<double, 4> bitwise_cast(const batch<int32_t, 8>& x)
{
return _mm256_castsi256_pd(x);
}

template <>
inline batch<float, 8> bitwise_cast(const batch<int64_t, 4>& x)
{
return _mm256_castsi256_ps(x);
}

template <>
inline batch<double, 4> bitwise_cast(const batch<int64_t, 4>& x)
{
return _mm256_castsi256_pd(x);
}

}

#endif
Expand Down

0 comments on commit 59b26e4

Please sign in to comment.