| 1 | // RUN: %clangxx_nsan -O0 -g -mavx %s -o %t |
| 2 | // RUN: env NSAN_OPTIONS=check_nan=true,halt_on_error=0 %run %t 2>&1 | FileCheck %s |
| 3 | // RUN: %clangxx_nsan -O3 -g -mavx %s -o %t |
| 4 | // RUN: env NSAN_OPTIONS=check_nan=true,halt_on_error=0 %run %t 2>&1 | FileCheck %s |
| 5 | |
| 6 | #include <cmath> |
| 7 | #include <immintrin.h> |
| 8 | #include <iostream> |
| 9 | |
| 10 | void simd_sqrt(const float *input, float *output, size_t size) { |
| 11 | size_t i = 0; |
| 12 | for (; i + 7 < size; i += 8) { |
| 13 | __m256 vec = _mm256_loadu_ps(p: &input[i]); |
| 14 | __m256 result = _mm256_sqrt_ps(a: vec); |
| 15 | _mm256_storeu_ps(p: &output[i], a: result); |
| 16 | } |
| 17 | for (; i < size; ++i) { |
| 18 | output[i] = std::sqrt(input[i]); |
| 19 | // CHECK: WARNING: NumericalStabilitySanitizer: NaN detected |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | int main() { |
| 24 | float input[] = {1.0, 2.0, -3.0, 4.0, 5.0, 6.0, 7.0, |
| 25 | 8.0, 9.0, -10.0, 11.0, 12.0, 13.0, 14.0, |
| 26 | 15.0, -16.0, 17.0, -18.0, -19.0, -20.0}; |
| 27 | float output[20]; |
| 28 | simd_sqrt(input, output, size: 20); |
| 29 | for (int i = 0; i < 20; ++i) { |
| 30 | std::cout << output[i] << std::endl; |
| 31 | // CHECK: WARNING: NumericalStabilitySanitizer: NaN detected |
| 32 | } |
| 33 | return 0; |
| 34 | } |
| 35 | |