Skip to content

In the Apple M3 environment, executing the log function returns 0 when I use O1,O2,O3 option #1116

@viktorika

Description

@viktorika
#include <cmath>
#include <iostream>
#include "xsimd/xsimd.hpp"

namespace xs = xsimd;

void ListLog10(float* a, float* result, int size) {
  using batch_type = xs::batch<float, xs::default_arch>;  // 自动选择最优指令集架构
  constexpr std::size_t batch_size = batch_type::size;
  // 向量化处理主循环
  std::size_t vec_size = size - (size % batch_size);
  std::cout << "vec_size=" << vec_size << std::endl;
  std::cout << "batch_size=" << batch_size << std::endl;
  for (std::size_t i = 0; i < vec_size; i += batch_size) {
    std::cout << "i=" << i << std::endl;
    auto a_vec = batch_type::load_unaligned(a + i);  // 加载未对齐数据
    auto log_vec = xs::log10(a_vec);                   // SIMD对数计算
    // auto log_vec = a_vec * 2;
    log_vec.store_unaligned(result + i);  // 存储结果

    std::cout << a_vec << std::endl;
    std::cout << log_vec << std::endl;
  }

  // 处理剩余元素
  for (std::size_t i = vec_size; i < size; ++i) {
    result[i] = std::log10(a[i]);
  }
}

int main() {
  std::vector<float> a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  std::vector<float> result(a.size());
  ListLog10(a.data(), result.data(), a.size());
  for (int i = 0; i < a.size(); i++) {
    std::cout << result[i] << " ";
  }
  std::cout << std::endl;

  return 0;
}

This is my code. I found that using arithmetic operations or other trigonometric functions works fine, but using the log function specifically returns 0 when I use O2 option.
The default architecture is neon64, and supported_architectures returns neon64 and neon. I get the same result when switching to neon.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions