Skip to content

Commit

Permalink
Fix some warnings and start work on new vector library
Browse files Browse the repository at this point in the history
  • Loading branch information
Pencilcaseman committed Jul 24, 2023
1 parent 9ce45a3 commit db542de
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 49 deletions.
5 changes: 1 addition & 4 deletions librapid/include/librapid/core/warningSuppress.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
# define LIBRAPID_GCC_SUPPRESS(WARNING_)
#endif

#define LIBRAPID_SUPPRESS(WARNING_) \
LIBRAPID_MSVC_SUPPRESS(WARNING_) \
LIBRAPID_GCC_SUPPRESS(WARNING_)

LIBRAPID_MSVC_SUPPRESS(4996) // Disable warnings about unsafe classes
LIBRAPID_MSVC_SUPPRESS(4723) // Disable zero division errors
LIBRAPID_MSVC_SUPPRESS(5245) // unreferenced function with internal linkage has been removed
LIBRAPID_MSVC_SUPPRESS(4146) // unary minus operator applied to unsigned type, result still unsigned

#endif // LIBRAPID_WARNING_SUPPRESS
15 changes: 8 additions & 7 deletions librapid/include/librapid/math/half.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ namespace librapid {
const uint32_t is_e_overflow_msb = (h_e_mask_value - f_e_half_bias);
const uint32_t is_h_inf_msb = (is_e_overflow_msb | is_f_inf_msb);
const uint32_t is_f_nsnan_msb = (f_snan - f_snan_mask);
const uint32_t is_m_norm_overflow_msb = (-f_m_rounded_overflow);
const uint32_t is_m_norm_overflow_msb = (-((int32_t)f_m_rounded_overflow));
const uint32_t is_f_snan_msb = (~is_f_nsnan_msb);
const uint32_t h_em_overflow_result =
uint32Sels(is_m_norm_overflow_msb, h_e_norm_overflow, h_em_norm);
Expand Down Expand Up @@ -221,7 +221,7 @@ namespace librapid {
const uint32_t f_em_denorm = (f_e_denorm | f_m_denorm);
const uint32_t f_em_nan = (f_e_mask | f_m);
const uint32_t is_e_eqz_msb = (h_e - 1);
const uint32_t is_m_nez_msb = (-h_m);
const uint32_t is_m_nez_msb = (-((int32_t)h_m));
const uint32_t is_e_flagged_msb = (h_e_mask_minus_one - h_e);
const uint32_t is_zero_msb = (is_e_eqz_msb & ~is_m_nez_msb);
const uint32_t is_inf_msb = (is_e_flagged_msb & ~is_m_nez_msb);
Expand Down Expand Up @@ -394,20 +394,21 @@ namespace librapid {
const uint32_t c_e_amount_biased = (a_e_amount + b_e_amount);
const uint32_t c_e_amount_unbiased = (c_e_amount_biased - h_e_bias);
const uint32_t is_c_e_unbiased_underflow = (((std::int32_t)c_e_amount_unbiased) >> 31);
const uint32_t c_e_underflow_half_sa = (-c_e_amount_unbiased);
const uint32_t c_e_underflow_half_sa = (-((int32_t)c_e_amount_unbiased));
const uint32_t c_e_underflow_sa = (c_e_underflow_half_sa << one);
const uint32_t c_m_underflow = (c_m_normal >> c_e_underflow_sa);
const uint32_t c_e_underflow_added = (c_e_amount_unbiased & ~is_c_e_unbiased_underflow);
const uint32_t c_m_underflow_added =
uint32Selb(is_c_e_unbiased_underflow, c_m_underflow, c_m_normal);
const uint32_t is_mul_overflow_test = (c_e_underflow_added & m_round_overflow_bit);
const uint32_t is_mul_overflow_msb = (-is_mul_overflow_test);
const uint32_t is_mul_overflow_msb = (-((int32_t)is_mul_overflow_test));
const uint32_t c_e_norm_radix_corrected = (c_e_underflow_added + 1);
const uint32_t c_m_norm_radix_corrected = (c_m_underflow_added >> one);
const uint32_t c_m_norm_hidden_bit = (c_m_norm_radix_corrected & m_hidden_bit);
const uint32_t is_c_m_norm_no_hidden_msb = (c_m_norm_hidden_bit - 1);
const uint32_t c_m_norm_lo = (c_m_norm_radix_corrected >> h_m_bit_half_count);
const uint32_t c_m_norm_lo_nlz = uint16Cntlz(c_m_norm_lo);
const uint32_t c_m_norm_lo = (c_m_norm_radix_corrected >> h_m_bit_half_count);
const uint32_t c_m_norm_lo_nlz =
static_cast<uint32_t>(uint16Cntlz((uint16_t)c_m_norm_lo));
const uint32_t is_c_m_hidden_nunderflow_msb =
(c_m_norm_lo_nlz - c_e_norm_radix_corrected);
const uint32_t is_c_m_hidden_underflow_msb = (~is_c_m_hidden_nunderflow_msb);
Expand All @@ -428,7 +429,7 @@ namespace librapid {
const uint32_t c_m_norm_round_amount = (c_m_normalized & h_m_mask);
const uint32_t c_m_norm_rounded = (c_m_normalized + c_m_norm_round_amount);
const uint32_t is_round_overflow_test = (c_e_normalized & m_round_overflow_bit);
const uint32_t is_round_overflow_msb = (-is_round_overflow_test);
const uint32_t is_round_overflow_msb = (-((int32_t)is_round_overflow_test));
const uint32_t c_m_norm_inplace = (c_m_norm_rounded >> h_m_bit_count);
const uint32_t c_m = (c_m_norm_inplace & h_m_mask);
const uint32_t c_e_norm_inplace = (c_e_normalized << h_e_pos);
Expand Down
4 changes: 2 additions & 2 deletions librapid/include/librapid/math/random.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace librapid {
// Random floating point value in range [lower, upper)

static std::uniform_real_distribution<double> distribution(0., 1.);
static std::mt19937 generator(global::randomSeed);
static std::mt19937 generator((uint32_t) global::randomSeed);

if (global::reseed) {
generator.seed(global::randomSeed);
generator.seed((uint32_t) global::randomSeed);
global::reseed = false;
}

Expand Down
79 changes: 45 additions & 34 deletions librapid/include/librapid/math/vector.hpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,51 @@
#ifndef LIBRAPID_MATH_VECTOR_HPP
#define LIBRAPID_MATH_VECTOR_HPP
#ifndef LIBRAPID_MATH_VECTOR_OLD_HPP
#define LIBRAPID_MATH_VECTOR_OLD_HPP

#include "genericVector.hpp"
#include "simdVector.hpp"
// #include "genericVector.hpp"
// #include "simdVector.hpp"
#include "vectorImpl.hpp"

namespace librapid {
namespace detail {
template<typename Scalar, int64_t Dims>
auto vectorTypeIdentifier() {
if constexpr (typetraits::TypeInfo<Scalar>::packetWidth > 1) {
return SIMDVector<Scalar, Dims> {};
} else {
return GenericVector<Scalar, Dims> {};
}
}
} // namespace detail
// namespace detail {
// template<typename Scalar, int64_t Dims>
// auto vectorTypeIdentifier() {
// if constexpr (typetraits::TypeInfo<Scalar>::packetWidth > 1) {
// return SIMDVector<Scalar, Dims> {};
// } else {
// return GenericVector<Scalar, Dims> {};
// }
// }
// } // namespace detail
//
// /// A simplified interface to the GenericVector class, defaulting to Vc SimdArray storage
// /// \tparam Scalar The scalar type of the vector
// /// \tparam Dims The dimensionality of the vector
// template<typename Scalar, int64_t Dims>
// using Vec = decltype(detail::vectorTypeIdentifier<Scalar, Dims>());
//
// using Vec2i = Vec<int32_t, 2>;
// using Vec3i = Vec<int32_t, 3>;
// using Vec4i = Vec<int32_t, 4>;
// using Vec2f = Vec<float, 2>;
// using Vec3f = Vec<float, 3>;
// using Vec4f = Vec<float, 4>;
// using Vec2d = Vec<double, 2>;
// using Vec3d = Vec<double, 3>;
// using Vec4d = Vec<double, 4>;
//
// using Vec2 = Vec2d;
// using Vec3 = Vec3d;
// using Vec4 = Vec4d;

/// A simplified interface to the GenericVector class, defaulting to Vc SimdArray storage
/// \tparam Scalar The scalar type of the vector
/// \tparam Dims The dimensionality of the vector
template<typename Scalar, int64_t Dims>
using Vec = decltype(detail::vectorTypeIdentifier<Scalar, Dims>());

using Vec2i = Vec<int32_t, 2>;
using Vec3i = Vec<int32_t, 3>;
using Vec4i = Vec<int32_t, 4>;
using Vec2f = Vec<float, 2>;
using Vec3f = Vec<float, 3>;
using Vec4f = Vec<float, 4>;
using Vec2d = Vec<double, 2>;
using Vec3d = Vec<double, 3>;
using Vec4d = Vec<double, 4>;

using Vec2 = Vec2d;
using Vec3 = Vec3d;
using Vec4 = Vec4d;
using Vec2i = Vector<int32_t, 2>;
using Vec3i = Vector<int32_t, 3>;
using Vec4i = Vector<int32_t, 4>;
using Vec2f = Vector<float, 2>;
using Vec3f = Vector<float, 3>;
using Vec4f = Vector<float, 4>;
using Vec2d = Vector<double, 2>;
using Vec3d = Vector<double, 3>;
using Vec4d = Vector<double, 4>;
} // namespace librapid

#endif // LIBRAPID_MATH_VECTOR_HPP
#endif // LIBRAPID_MATH_VECTOR_OLD_HPP
75 changes: 75 additions & 0 deletions librapid/include/librapid/math/vectorImpl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#ifndef LIBRAPID_MATH_VECTOR_HPP
#define LIBRAPID_MATH_VECTOR_HPP

namespace librapid {
namespace detail {
template<typename T, size_t N, typename = void>
struct VectorStorage;

template<typename T, size_t N>
struct VectorStorage<T, N, std::enable_if_t<(typetraits::TypeInfo<T>::packetWidth == 1)>> {
static constexpr uint64_t length = N;
T data[length];

LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE const auto &operator[](int64_t index) const {
return data[index];
}

LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto &operator[](int64_t index) {
return data[index];
}
};

template<typename T, size_t N>
struct VectorStorage<T, N, std::enable_if_t<(typetraits::TypeInfo<T>::packetWidth > 1)>> {
using Packet = typename typetraits::TypeInfo<T>::Packet;
static constexpr uint64_t packetWidth = typetraits::TypeInfo<T>::packetWidth;
static constexpr uint64_t length =
(N + typetraits::TypeInfo<T>::packetWidth - 1) / typetraits::TypeInfo<T>::packetWidth;
Packet data[length];

LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto operator[](int64_t index) const {
const int64_t packetIndex = index / packetWidth;
const int64_t elementIndex = index % packetWidth;
return data[packetIndex][elementIndex];
}

LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto operator[](int64_t index) {
const int64_t packetIndex = index / packetWidth;
const int64_t elementIndex = index % packetWidth;
return data[packetIndex][elementIndex];
}
};

template<typename T, size_t N, size_t... Indices>
void vectorStorageAssigner(VectorStorage<T, N> &dst, const VectorStorage<T, N> &src,
std::index_sequence<Indices...>) {
((dst[Indices] = src[Indices]), ...);
}
} // namespace detail

template<typename ScalarType, size_t NumDims>
class Vector {
public:
using Scalar = ScalarType;
static constexpr uint64_t Dims = NumDims;
using StorageType = detail::VectorStorage<Scalar, NumDims>;
static constexpr uint64_t length = StorageType::length;

Vector() = default;
Vector(const Vector &other) = default;
Vector(Vector &&other) noexcept = default;

auto operator=(const Vector &other) -> Vector & = default;
auto operator=(Vector &&other) noexcept -> Vector & = default;

// template<typename T, size_t N>
// Vector(const Vector<T, N> &other) {
// detail::vectorStorageAssigner(m_data, other.m_data, std::make_index_sequence<length>());
// }

private:
StorageType m_data;
};
} // namespace librapid
#endif // LIBRAPID_MATH_VECTOR_HPP
2 changes: 1 addition & 1 deletion librapid/src/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace librapid {

// MKL threading
#if defined(LIBRAPID_BLAS_MKL)
mkl_set_num_threads(numThreads);
mkl_set_num_threads((int) numThreads);
#endif // LIBRAPID_BLAS_MKL
}

Expand Down
2 changes: 1 addition & 1 deletion librapid/src/preMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace librapid::detail {
#endif // LIBRAPID_HAS_CUDA

// Set the random seed to an initial value
global::randomSeed = now<time::nanosecond>();
global::randomSeed = (size_t) now<time::nanosecond>();
}
}
} // namespace librapid::detail

0 comments on commit db542de

Please sign in to comment.