Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
viktorika opened this issue May 9, 2025 · 6 comments

Comments

@viktorika
Copy link

viktorika commented May 9, 2025

#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.

@viktorika viktorika changed the title In the Apple M3 environment, executing the log function returns 0 In the Apple M3 environment, executing the log function returns 0 when I use O1,O2,O3 option May 12, 2025
@viktorika
Copy link
Author

I found is because i compile with the -ffast-math option, when i remove it, it is ok.

@serge-sans-paille
Copy link
Contributor

Thanls for the report and the -ffast-math information. Can you confirm #1117 fixes the implementation even under -ffast-math ?

@viktorika
Copy link
Author

Thanls for the report and the -ffast-math information. Can you confirm #1117 fixes the implementation even under -ffast-math ?

I merged this modification into my 13.2.0 branch, but it still doesn't run correctly.

@serge-sans-paille
Copy link
Contributor

strange. Did you merge the two commits from that branch and not only the top-of-tree?

@serge-sans-paille
Copy link
Contributor

(I've been able to reproduce the issue on Apple M1 and the patch set fixes the issue)

@viktorika
Copy link
Author

viktorika commented May 12, 2025

now i use master branch to rebuild in my Apple M3,but it still doesn't run correctly.this is my compile command:clang++ -ftree-vectorize -O2 -o input input.cc -ffast-math.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants