Skip to content

Commit 92afec4

Browse files
committed
Improve code quality
1 parent 7119e9a commit 92afec4

File tree

5 files changed

+42
-44
lines changed

5 files changed

+42
-44
lines changed

include/xsimd/arch/utils/shifts.hpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "../../config/xsimd_inline.hpp"
1717
#include "../../types/xsimd_batch.hpp"
1818
#include "../../types/xsimd_batch_constant.hpp"
19+
#include "../../types/xsimd_traits.hpp"
1920

2021
namespace xsimd
2122
{
@@ -45,24 +46,17 @@ namespace xsimd
4546
return static_cast<I>((I { 1 } << bit_index) - I { 1 });
4647
}
4748

48-
template <class T, class A, T... Vs>
49-
constexpr bool all_equals(batch_constant<T, A, Vs...> c)
49+
template <class T, class A, T V0, T... Vs>
50+
constexpr bool all_equals(batch_constant<T, A, V0, Vs...> c)
5051
{
51-
static_assert(sizeof...(Vs) > 0, "There must be at least one value");
52-
53-
bool out = true;
54-
for (std::size_t k = 0; k < sizeof...(Vs); ++k)
55-
{
56-
out &= c.get(k) == c.get(0);
57-
}
58-
return out;
52+
return (c == std::integral_constant<T, V0> {}).all();
5953
}
6054

61-
template <class T, class T2, class A, T... Vs>
55+
template <class T, class A, T... Vs>
6256
XSIMD_INLINE batch<T, A> bitwise_lshift_as_twice_larger(
6357
batch<T, A> const& self, batch_constant<T, A, Vs...>) noexcept
6458
{
65-
static_assert(sizeof(T2) == 2 * sizeof(T), "One size must be twice the other");
59+
using T2 = widen_t<T>;
6660

6761
const auto self2 = bitwise_cast<T2>(self);
6862

include/xsimd/arch/xsimd_avx2.hpp

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,12 @@ namespace xsimd
333333
}
334334
}
335335

336-
// bitwise_lshift with constant bitshifts, for 16-bit elements.
337-
// Missing implementations are dispacthed to the `batch` overload in xsimd_api.
338-
template <class T, class A, T... Vs, detail::enable_sized_integral_t<T, 2> = 0>
336+
// bitwise_lshift multiple (constant) specific implementations.
337+
// Missing implementations are dispatched to the `batch` overload in xsimd_api.
338+
// The 1 byte constant implementation calls the 2 bytes constant version, the 2 bytes
339+
// constant version calls into the 4 bytes version which resolves to the dynamic one above.
340+
template <class T, class A, T... Vs,
341+
std::enable_if_t<std::is_integral<T>::value && (sizeof(T) <= 2), int> = 0>
339342
XSIMD_INLINE batch<T, A> bitwise_lshift(
340343
batch<T, A> const& self, batch_constant<T, A, Vs...> shifts, requires_arch<avx2> req) noexcept
341344
{
@@ -348,24 +351,7 @@ namespace xsimd
348351
return bitwise_lshift<shifts.get(0), A>(self, req);
349352
}
350353
return bitwise_cast<T>(
351-
utils::bitwise_lshift_as_twice_larger<uint_t, uint32_t>(
352-
bitwise_cast<uint_t>(self),
353-
batch_constant<uint_t, A, static_cast<uint_t>(Vs)...> {}));
354-
}
355-
356-
// bitwise_lshift with constant bitshifts, for 8-bit elements.
357-
template <class T, class A, T... Vs, detail::enable_sized_integral_t<T, 1> = 0>
358-
XSIMD_INLINE batch<T, A> bitwise_lshift(
359-
batch<T, A> const& self, batch_constant<T, A, Vs...> shifts, requires_arch<avx2> req) noexcept
360-
{
361-
using uint_t = typename std::make_unsigned<T>::type;
362-
363-
XSIMD_IF_CONSTEXPR(utils::all_equals(shifts))
364-
{
365-
return bitwise_lshift<shifts.get(0), A>(self, req);
366-
}
367-
return bitwise_cast<T>(
368-
utils::bitwise_lshift_as_twice_larger<uint_t, uint16_t>(
354+
utils::bitwise_lshift_as_twice_larger<uint_t>(
369355
bitwise_cast<uint_t>(self),
370356
batch_constant<uint_t, A, static_cast<uint_t>(Vs)...> {}));
371357
}

include/xsimd/arch/xsimd_sse2.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ namespace xsimd
352352
return bitwise_lshift<shifts.get(0), A>(self, req);
353353
}
354354
return bitwise_cast<T>(
355-
utils::bitwise_lshift_as_twice_larger<uint_t, uint16_t>(
355+
utils::bitwise_lshift_as_twice_larger<uint_t>(
356356
bitwise_cast<uint_t>(self),
357357
batch_constant<uint_t, A, static_cast<uint_t>(Vs)...> {}));
358358
}

include/xsimd/types/xsimd_api.hpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -361,30 +361,32 @@ namespace xsimd
361361
// Running the detection here is less error prone than to add a fallback to all
362362
// architectures.
363363

364-
template <class A, class B, class C, class = void>
364+
template <class Arch, class Batch, class BatchConstant, class = void>
365365
struct has_bitwise_lshift_batch_const : std::false_type
366366
{
367367
};
368368

369-
template <class A, class B, class C>
370-
struct has_bitwise_lshift_batch_const<A, B, C,
371-
void_t<decltype(kernel::bitwise_lshift<A>(std::declval<B>(), std::declval<C>(), A {}))>>
369+
template <class Arch, class Batch, class BatchConstant>
370+
struct has_bitwise_lshift_batch_const<
371+
Arch, Batch, BatchConstant,
372+
void_t<decltype(kernel::bitwise_lshift<Arch>(
373+
std::declval<Batch>(), std::declval<BatchConstant>(), Arch {}))>>
372374
: std::true_type
373375
{
374376
};
375377

376-
template <class A, class T, T... Values>
377-
XSIMD_INLINE batch<T, A> bitwise_lshift_batch_const(batch<T, A> const& x, batch_constant<T, A, Values...> shift, std::true_type) noexcept
378+
template <class Arch, class T, T... Values>
379+
XSIMD_INLINE batch<T, Arch> bitwise_lshift_batch_const(batch<T, Arch> const& x, batch_constant<T, Arch, Values...> shift, std::true_type) noexcept
378380
{
379381
// Optimized ``batch_constant`` implementation
380-
return kernel::bitwise_lshift<A>(x, shift, A {});
382+
return kernel::bitwise_lshift<Arch>(x, shift, Arch {});
381383
}
382384

383-
template <class A, class T, T... Values>
384-
XSIMD_INLINE batch<T, A> bitwise_lshift_batch_const(batch<T, A> const& x, batch_constant<T, A, Values...> shift, std::false_type) noexcept
385+
template <class Arch, class T, T... Values>
386+
XSIMD_INLINE batch<T, Arch> bitwise_lshift_batch_const(batch<T, Arch> const& x, batch_constant<T, Arch, Values...> shift, std::false_type) noexcept
385387
{
386388
// Fallback to regular run-time implementation
387-
return kernel::bitwise_lshift<A>(x, shift.as_batch(), A {});
389+
return kernel::bitwise_lshift<Arch>(x, shift.as_batch(), Arch {});
388390
}
389391
}
390392

include/xsimd/types/xsimd_traits.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#ifndef XSIMD_TRAITS_HPP
1313
#define XSIMD_TRAITS_HPP
1414

15+
#include <cstdint>
1516
#include <type_traits>
1617

1718
#include "xsimd_batch.hpp"
@@ -421,6 +422,21 @@ namespace xsimd
421422
using type = uint16_t;
422423
};
423424
template <>
425+
struct widen<int32_t>
426+
{
427+
using type = int64_t;
428+
};
429+
template <>
430+
struct widen<int16_t>
431+
{
432+
using type = int32_t;
433+
};
434+
template <>
435+
struct widen<int8_t>
436+
{
437+
using type = int16_t;
438+
};
439+
template <>
424440
struct widen<float>
425441
{
426442
using type = double;

0 commit comments

Comments
 (0)