<commit_before>/*! @file matrix.c
 *  @brief New file description.
 *  @author <NAME> <<EMAIL>>
 *  @version 1.0
 *
 *  @section Notes
 *  This code partially conforms to <a href="http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml">Google C++ Style Guide</a>.
 *
 *  @section Copyright
 *  Copyright 2013 Samsung R&D Institute Russia
 */

#define LIBSIMD_IMPLEMENTATION
#include "inc/simd/matrix.h"
#include <assert.h>
#include "inc/simd/memory.h"
#ifdef __AVX__
#include <immintrin.h>
#elif defined(__ARM_NEON__)
#include <arm_neon.h>
#endif

static void matrix_add_novec(const float *m1, const float *m2,
                      size_t w, size_t h, float *res) {
  int size = (int)w * (int)h;
  for (int i = 0; i < size; i++) {
    res[i] = m1[i] + m2[i];
  }
}

static void matrix_sub_novec(const float *m1, const float *m2,
                      size_t w, size_t h, float *res) {
  int size = (int)w * (int)h;
  for (int i = 0; i < size; i++) {
    res[i] = m1[i] - m2[i];
  }
}

static void matrix_multiply_novec(const float *m1, const float *m2,
                                  size_t w1, size_t h1, size_t w2,
                                  size_t h2 UNUSED, float *res) {
  for (int i = 0; i < (int)w2; i++) {
    for (int j = 0; j < (int)h1; j++) {
      float sum = 0;
      for (int k = 0; k < (int)w1; k++) {
        sum += m1[j * w1 + k] * m2[k * w2 + i];
      }
      res[j * w2 + i] = sum;
    }
  }
}

static void matrix_multiply_transposed_novec(const float *m1, const float *m2,
                                             size_t w1, size_t h1,
                                             size_t w2 UNUSED, size_t h2,
                                             float *res) {
  for (int j = 0; j < (int)h1; j++) {
    for (int i = 0; i < (int)h2; i++) {
      float sum = 0;
      for (int k = 0; k < (int)w1; k++) {
        sum += m1[j * w1 + k] * m2[i * w1 + k];
      }
      res[j * h2 + i] = sum;
    }
  }
}

#ifdef __ARM_NEON__
static void matrix_add_neon(const float *m1, const float *m2,
                            size_t w, size_t h, float *res) {
  int length = (int)w * (int)h;
  int i = 0;
  for (; i < length - 3; i += 4) {
    float32x4_t vec1 = vld1q_f32(m1 + i);
    float32x4_t vec2 = vld1q_f32(m2 + i);
    float32x4_t vec_r = vaddq_f32(vec1, vec2);
    vst1q_f32(res + i, vec_r);
  }
  for (; i < length; i++) {
    res[i] = m1[i] + m2[i];
  }
}

static void matrix_sub_neon(const float *m1, const float *m2,
                            size_t w, size_t h, float *res) {
  int length = (int)w * (int)h;
  int i = 0;
  for (; i < length - 3; i += 4) {
    float32x4_t vec1 = vld1q_f32(m1 + i);
    float32x4_t vec2 = vld1q_f32(m2 + i);
    float32x4_t vec_r = vsubq_f32(vec1, vec2);
    vst1q_f32(res + i, vec_r);
  }
  for (; i < length; i++) {
    res[i] = m1[i] + m2[i];
  }
}

static void matrix_multiply_neon(const float *m1, const float *m2,
                                 size_t w1, size_t h1, size_t w2,
                                 size_t h2 UNUSED, float *res) {
  float col2[w1] __attribute__((aligned(64)));
  for (int i = 0; i < (int)w2; i++) {
    for (int k = 0; k < (int)w1; k++) {<commit_msg>Fixed bug in NEON matrix subtraction<commit_after>/*! @file matrix.c
 *  @brief New file description.
 *  @author <NAME> <<EMAIL>>
 *  @version 1.0
 *
 *  @section Notes
 *  This code partially conforms to <a href="http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml">Google C++ Style Guide</a>.
 *
 *  @section Copyright
 *  Copyright 2013 Samsung R&D Institute Russia
 */

#define LIBSIMD_IMPLEMENTATION
#include "inc/simd/matrix.h"
#include <assert.h>
#include "inc/simd/memory.h"
#ifdef __AVX__
#include <immintrin.h>
#elif defined(__ARM_NEON__)
#include <arm_neon.h>
#endif

static void matrix_add_novec(const float *m1, const float *m2,
                      size_t w, size_t h, float *res) {
  int size = (int)w * (int)h;
  for (int i = 0; i < size; i++) {
    res[i] = m1[i] + m2[i];
  }
}

static void matrix_sub_novec(const float *m1, const float *m2,
                      size_t w, size_t h, float *res) {
  int size = (int)w * (int)h;
  for (int i = 0; i < size; i++) {
    res[i] = m1[i] - m2[i];
  }
}

static void matrix_multiply_novec(const float *m1, const float *m2,
                                  size_t w1, size_t h1, size_t w2,
                                  size_t h2 UNUSED, float *res) {
  for (int i = 0; i < (int)w2; i++) {
    for (int j = 0; j < (int)h1; j++) {
      float sum = 0;
      for (int k = 0; k < (int)w1; k++) {
        sum += m1[j * w1 + k] * m2[k * w2 + i];
      }
      res[j * w2 + i] = sum;
    }
  }
}

static void matrix_multiply_transposed_novec(const float *m1, const float *m2,
                                             size_t w1, size_t h1,
                                             size_t w2 UNUSED, size_t h2,
                                             float *res) {
  for (int j = 0; j < (int)h1; j++) {
    for (int i = 0; i < (int)h2; i++) {
      float sum = 0;
      for (int k = 0; k < (int)w1; k++) {
        sum += m1[j * w1 + k] * m2[i * w1 + k];
      }
      res[j * h2 + i] = sum;
    }
  }
}

#ifdef __ARM_NEON__
static void matrix_add_neon(const float *m1, const float *m2,
                            size_t w, size_t h, float *res) {
  int length = (int)w * (int)h;
  int i = 0;
  for (; i < length - 3; i += 4) {
    float32x4_t vec1 = vld1q_f32(m1 + i);
    float32x4_t vec2 = vld1q_f32(m2 + i);
    float32x4_t vec_r = vaddq_f32(vec1, vec2);
    vst1q_f32(res + i, vec_r);
  }
  for (; i < length; i++) {
    res[i] = m1[i] + m2[i];
  }
}

static void matrix_sub_neon(const float *m1, const float *m2,
                            size_t w, size_t h, float *res) {
  int length = (int)w * (int)h;
  int i = 0;
  for (; i < length - 3; i += 4) {
    float32x4_t vec1 = vld1q_f32(m1 + i);
    float32x4_t vec2 = vld1q_f32(m2 + i);
    float32x4_t vec_r = vsubq_f32(vec1, vec2);
    vst1q_f32(res + i, vec_r);
  }
  for (; i < length; i++) {
    res[i] = m1[i] - m2[i];
  }
}

static void matrix_multiply_neon(const float *m1, const float *m2,
                                 size_t w1, size_t h1, size_t w2,
                                 size_t h2 UNUSED, float *res) {
  float col2[w1] __attribute__((aligned(64)));
  for (int i = 0; i < (int)w2; i++) {
    for (int k = 0; k < (int)w1; k++) {
{'fraction_non_alphanumeric': 0.14952096748861315, 'fraction_numerical': 0.04146379770692634, 'mean_word_length': 1.9359151682803135, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1915287', 'n_tokens_mistral': 2693, 'n_tokens_neox': 2592, 'n_words': 703}