<commit_before>#ifndef VXL_QUAT_HPP
# define VXL_QUAT_HPP
# pragma once

#include "matrix.hpp"

namespace vxl
{

template <typename T>
struct quat
{
  typename vector_traits<T, 4>::vector_type data_;

  // conversion operators
  operator decltype(data_) const& () const noexcept { return data_; }
  operator decltype((data_)) () noexcept { return data_; }

  // conversion
  auto& ref() noexcept { return data_; }
  auto& ref() const noexcept { return data_; }

  // element access
  T& operator()(unsigned const i) noexcept { return data_[i]; }
  T operator()(unsigned const i) const noexcept { return data_[i]; }
  void operator()(unsigned const i, T const v) noexcept { data_[i] = v; }
};

template <typename T>
inline constexpr quat<T> operator+(quat<T> const& a,
  quat<T> const& b) noexcept
{
  return { a.data_ + b.data_ };
}

template <typename T>
inline constexpr quat<T> operator-(quat<T> const& a,
  quat<T> const& b) noexcept
{
  return { a.data_ - b.data_ };
}

template <typename T>
//__attribute__ ((noinline))
inline constexpr quat<T> operator*(
  quat<T> const& l, quat<T> const& r) noexcept
{
//  l(0)r(3) + l(1)r(2) - l(2)r(1) + l(3)r(0)
// -l(0)r(2) + l(1)r(3) + l(2)r(0) + l(3)r(1)
//  l(0)r(1) - l(1)r(0) + l(2)r(3) + l(3)r(2)
// -l(0)r(0) - l(1)r(1) - l(2)r(2) + l(3)r(3)
  using int_value_type = typename vector_traits<T, 4>::int_value_type;
  using int_vector_type = typename vector_traits<T, 4>::int_vector_type;

  constexpr int_vector_type const mask{
    0, 0, 0, 1 << (8 * sizeof(int_value_type) - 1)
  };

#if defined(__clang__)
  auto const t1(
    l.data_ *
    __builtin_shufflevector(r.data_, r.data_, 3, 3, 3, 3)
  );
  auto const t2(
    __builtin_shufflevector(l.data_, l.data_, 1, 2, 0, 2) *
    __builtin_shufflevector(r.data_, r.data_, 2, 0, 1, 2)
  );
  auto const t3(
    __builtin_shufflevector(l.data_, l.data_, 3, 3, 3, 1) *
    __builtin_shufflevector(r.data_, r.data_, 0, 1, 2, 1)
  );
  auto const t4(
    __builtin_shufflevector(l.data_, l.data_, 2, 0, 1, 0) *
    __builtin_shufflevector(r.data_, r.data_, 1, 2, 0, 0)
  );
#else
  auto const t1(
    l.data_ *
    __builtin_shuffle(r.data_, int_vector_type{3, 3, 3, 3})
  );
  auto const t2(
    __builtin_shuffle(l.data_, int_vector_type{1, 2, 0, 2}) *
    __builtin_shuffle(r.data_, int_vector_type{2, 0, 1, 2})
  );
  auto const t3(
    __builtin_shuffle(l.data_, int_vector_type{3, 3, 3, 1}) *
    __builtin_shuffle(r.data_, int_vector_type{0, 1, 2, 1})
  );
  auto const t4(
    __builtin_shuffle(l.data_, int_vector_type{2, 0, 1, 0}) *
    __builtin_shuffle(r.data_, int_vector_type{1, 2, 0, 0})
  );
#endif

  return {
    t1 +
    decltype(t2)(int_vector_type(t2 + t3) ^ mask) -
    t4
  };
}

template <typename T>
inline constexpr quat<T>& operator+=(quat<T>& a, quat<T> const& b) noexcept
{
  return a.data_ += b.data_, a;
}

template <typename T>
inline constexpr quat<T>& operator-=(quat<T>& a, quat<T> const& b) noexcept
{
  return a.data_ -= b.data_, a;
}

// comparison
template <typename T><commit_msg>some fixes<commit_after>#ifndef VXL_QUAT_HPP
# define VXL_QUAT_HPP
# pragma once

#include "matrix.hpp"

namespace vxl
{

template <typename T>
struct quat
{
  typename vector_traits<T, 4>::vector_type data_;

  // conversion operators
  operator decltype(data_) const& () const noexcept { return data_; }
  operator decltype((data_)) () noexcept { return data_; }

  // conversion
  auto& ref() noexcept { return data_; }
  auto& ref() const noexcept { return data_; }

  // element access
  T& operator()(unsigned const i) noexcept { return data_[i]; }
  T operator()(unsigned const i) const noexcept { return data_[i]; }
  void operator()(unsigned const i, T const v) noexcept { data_[i] = v; }
};

template <typename T>
inline constexpr quat<T> operator+(quat<T> const& a,
  quat<T> const& b) noexcept
{
  return { a.data_ + b.data_ };
}

template <typename T>
inline constexpr quat<T> operator-(quat<T> const& a,
  quat<T> const& b) noexcept
{
  return { a.data_ - b.data_ };
}

template <typename T>
//__attribute__ ((noinline))
inline constexpr quat<T> operator*(
  quat<T> const& l, quat<T> const& r) noexcept
{
//  l(0)r(3) + l(1)r(2) - l(2)r(1) + l(3)r(0)
// -l(0)r(2) + l(1)r(3) + l(2)r(0) + l(3)r(1)
//  l(0)r(1) - l(1)r(0) + l(2)r(3) + l(3)r(2)
// -l(0)r(0) - l(1)r(1) - l(2)r(2) + l(3)r(3)
  using int_value_type = typename vector_traits<T, 4>::int_value_type;
  using int_vector_type = typename vector_traits<T, 4>::int_vector_type;

#if defined(__clang__)
  auto const t1(
    l.data_ *
    __builtin_shufflevector(r.data_, r.data_, 3, 3, 3, 3)
  );
  auto const t2(
    __builtin_shufflevector(l.data_, l.data_, 1, 2, 0, 2) *
    __builtin_shufflevector(r.data_, r.data_, 2, 0, 1, 2)
  );
  auto const t3(
    __builtin_shufflevector(l.data_, l.data_, 3, 3, 3, 1) *
    __builtin_shufflevector(r.data_, r.data_, 0, 1, 2, 1)
  );
  auto const t4(
    __builtin_shufflevector(l.data_, l.data_, 2, 0, 1, 0) *
    __builtin_shufflevector(r.data_, r.data_, 1, 2, 0, 0)
  );
#else
  auto const t1(
    l.data_ *
    __builtin_shuffle(r.data_, int_vector_type{3, 3, 3, 3})
  );
  auto const t2(
    __builtin_shuffle(l.data_, int_vector_type{1, 2, 0, 2}) *
    __builtin_shuffle(r.data_, int_vector_type{2, 0, 1, 2})
  );
  auto const t3(
    __builtin_shuffle(l.data_, int_vector_type{3, 3, 3, 1}) *
    __builtin_shuffle(r.data_, int_vector_type{0, 1, 2, 1})
  );
  auto const t4(
    __builtin_shuffle(l.data_, int_vector_type{2, 0, 1, 0}) *
    __builtin_shuffle(r.data_, int_vector_type{1, 2, 0, 0})
  );
#endif

  // negate the sign bit
  constexpr int_vector_type mask{
    0, 0, 0, 1 << (8 * sizeof(int_value_type) - 1)
  };

  return {
    t1 +
    decltype(t2)(int_vector_type(t2 + t3) ^ mask) -
    t4
  };
}

template <typename T>
inline constexpr quat<T>& operator+=(quat<T>& a, quat<T> const& b) noexcept
{
  return a.data_ += b.data_, a;
}

template <typename T>
inline constexpr quat<T>& operator-=(quat<T>& a, quat<T> const& b) noexcept
{
  return a.data_ -= b.data_, a;
}

// comparison
template <typename T>
{'fraction_non_alphanumeric': 0.1644649933949802, 'fraction_numerical': 0.03632760898282695, 'mean_word_length': 3.7543171114599687, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6549245', 'n_tokens_mistral': 2673, 'n_tokens_neox': 2547, 'n_words': 615}