/* SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Copyright:
 *   2021      Kunwar Maheep Singh <kunwar.maheep@students.iiit.ac.in>
 *   2023      Michael R. Crusoe <crusoe@debian.org>
 */

#define SIMDE_TEST_X86_AVX512_INSN flushsubnormal

#include <test/x86/avx512/test-avx512.h>
#include <simde/x86/avx512/flushsubnormal.h>
#include <simde/x86/avx512/set1.h>

#if !defined(SIMDE_FAST_MATH)
static int
test_simde_x_mm_flushsubnormal_ps (SIMDE_MUNIT_TEST_ARGS) {
  for (size_t i = 0 ; i < 8 ; i++) {
    simde__m128 a, r;
    simde__m128i e;
    if (simde_test_codegen_random_i32() & 1) {
      a = simde_mm_set1_ps(SIMDE_MATH_FLT_MIN / 2.0f);
      e = simde_mm_set1_epi32(0);
    } else {
      a = simde_mm_set1_ps(SIMDE_FLOAT32_C(1.0));
      e = simde_mm_castps_si128(simde_mm_set1_ps(SIMDE_FLOAT32_C(1.0)));
    }
    r = simde_x_mm_flushsubnormal_ps(a);

    simde_test_x86_assert_equal_i32x4(simde_mm_castps_si128(r), e);
  }

  return 0;
}

static int
test_simde_x_mm256_flushsubnormal_ps (SIMDE_MUNIT_TEST_ARGS) {
  for (size_t i = 0 ; i < 8 ; i++) {
    simde__m256 a, r;
    simde__m256i e;
    if (simde_test_codegen_random_i32() & 1) {
      a = simde_mm256_set1_ps(SIMDE_MATH_FLT_MIN / 2.0f);
      e = simde_mm256_set1_epi32(0);
    } else {
      a = simde_mm256_set1_ps(SIMDE_FLOAT32_C(1.0));
      e = simde_mm256_castps_si256(simde_mm256_set1_ps(SIMDE_FLOAT32_C(1.0)));
    }
    r = simde_x_mm256_flushsubnormal_ps(a);

    simde_test_x86_assert_equal_i32x8(simde_mm256_castps_si256(r), e);
  }

  return 0;
}

static int
test_simde_x_mm512_flushsubnormal_ps (SIMDE_MUNIT_TEST_ARGS) {
  for (size_t i = 0 ; i < 8 ; i++) {
    simde__m512 a, r;
    simde__m512i e;
    if (simde_test_codegen_random_i32() & 1) {
      a = simde_mm512_set1_ps(SIMDE_MATH_FLT_MIN / 2.0f);
      e = simde_mm512_set1_epi32(0);
    } else {
      a = simde_mm512_set1_ps(SIMDE_FLOAT32_C(1.0));
      e = simde_mm512_castps_si512(simde_mm512_set1_ps(SIMDE_FLOAT32_C(1.0)));
    }
    r = simde_x_mm512_flushsubnormal_ps(a);

    simde_test_x86_assert_equal_i32x16(simde_mm512_castps_si512(r), e);
  }

  return 0;
}

static int
test_simde_x_mm_flushsubnormal_pd (SIMDE_MUNIT_TEST_ARGS) {
  for (size_t i = 0 ; i < 8 ; i++) {
    simde__m128d a, r;
    simde__m128i e;
    if (simde_test_codegen_random_i32() & 1) {
      a = simde_mm_set1_pd(SIMDE_MATH_DBL_MIN / 2.0);
      e = simde_mm_set1_epi64x(0);
    } else {
      a = simde_mm_set1_pd(SIMDE_FLOAT64_C(1.0));
      e = simde_mm_castpd_si128(simde_mm_set1_pd(SIMDE_FLOAT64_C(1.0)));
    }
    r = simde_x_mm_flushsubnormal_pd(a);

    simde_test_x86_assert_equal_i64x2(simde_mm_castpd_si128(r), e);
  }

  return 0;
}

static int
test_simde_x_mm256_flushsubnormal_pd (SIMDE_MUNIT_TEST_ARGS) {
  for (size_t i = 0 ; i < 8 ; i++) {
    simde__m256d a, r;
    simde__m256i e;
    if (simde_test_codegen_random_i32() & 1) {
      a = simde_mm256_set1_pd(SIMDE_MATH_DBL_MIN / 2.0);
      e = simde_mm256_set1_epi64x(0);
    } else {
      a = simde_mm256_set1_pd(SIMDE_FLOAT64_C(1.0));
      e = simde_mm256_castpd_si256(simde_mm256_set1_pd(SIMDE_FLOAT64_C(1.0)));
    }
    r = simde_x_mm256_flushsubnormal_pd(a);

    simde_test_x86_assert_equal_i64x4(simde_mm256_castpd_si256(r), e);
  }

  return 0;
}

static int
test_simde_x_mm512_flushsubnormal_pd (SIMDE_MUNIT_TEST_ARGS) {
  for (size_t i = 0 ; i < 8 ; i++) {
    simde__m512d a, r;
    simde__m512i e;
    if (simde_test_codegen_random_i32() & 1) {
      a = simde_mm512_set1_pd(SIMDE_MATH_DBL_MIN / 2.0);
      e = simde_mm512_set1_epi64(0);
    } else {
      a = simde_mm512_set1_pd(SIMDE_FLOAT64_C(1.0));
      e = simde_mm512_castpd_si512(simde_mm512_set1_pd(SIMDE_FLOAT64_C(1.0)));
    }
    r = simde_x_mm512_flushsubnormal_pd(a);

    simde_test_x86_assert_equal_i64x8(simde_mm512_castpd_si512(r), e);
  }

  return 0;
}

#else
/* To avoid a warning about expr < 0 always evaluating to false
 * (-Wtype-limits) because there are no functions to test. */

static int
test_simde_dummy (SIMDE_MUNIT_TEST_ARGS) {
  return 0;
}
#endif /* !defined(SIMDE_FAST_MATH) */

SIMDE_TEST_FUNC_LIST_BEGIN
  #if !defined(SIMDE_FAST_MATH)
    SIMDE_TEST_FUNC_LIST_ENTRY(x_mm_flushsubnormal_ps)
    SIMDE_TEST_FUNC_LIST_ENTRY(x_mm256_flushsubnormal_ps)
    SIMDE_TEST_FUNC_LIST_ENTRY(x_mm512_flushsubnormal_ps)
    SIMDE_TEST_FUNC_LIST_ENTRY(x_mm_flushsubnormal_pd)
    SIMDE_TEST_FUNC_LIST_ENTRY(x_mm256_flushsubnormal_pd)
    SIMDE_TEST_FUNC_LIST_ENTRY(x_mm512_flushsubnormal_pd)
  #else
    SIMDE_TEST_FUNC_LIST_ENTRY(dummy)
  #endif
SIMDE_TEST_FUNC_LIST_END

#include <test/x86/avx512/test-avx512-footer.h>
