/*
 * Copyright (C) 2021 Collabora, Ltd.
 * SPDX-License-Identifier: MIT
 */

#include "bi_builder.h"
#include "bi_test.h"
#include "va_compiler.h"

#include <gtest/gtest.h>

static inline void
add_imm(bi_context *ctx)
{
   struct hash_table_u64 *stats = _mesa_hash_table_u64_create(ctx);
   bi_foreach_instr_global(ctx, I) {
      va_lower_constants(ctx, I, stats, UINT32_MAX);
   }
   _mesa_hash_table_u64_destroy(stats);
}

#define CASE(instr, expected) INSTRUCTION_CASE(instr, expected, add_imm)

class LowerConstants : public testing::Test {
 protected:
   LowerConstants()
   {
      mem_ctx = ralloc_context(NULL);
   }

   ~LowerConstants()
   {
      ralloc_free(mem_ctx);
   }

   void *mem_ctx;
};

TEST_F(LowerConstants, Float32)
{
   CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_imm_f32(0.0)),
        bi_fadd_f32_to(b, bi_register(0), bi_register(0), va_lut(0)));

   CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_imm_f32(1.0)),
        bi_fadd_f32_to(b, bi_register(0), bi_register(0), va_lut(16)));

   CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_imm_f32(0.1)),
        bi_fadd_f32_to(b, bi_register(0), bi_register(0), va_lut(17)));
}

TEST_F(LowerConstants, WidenFloat16)
{
   CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_imm_f32(0.5)),
        bi_fadd_f32_to(b, bi_register(0), bi_register(0),
                       bi_half(va_lut(26), 1)));

   CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_imm_f32(255.0)),
        bi_fadd_f32_to(b, bi_register(0), bi_register(0),
                       bi_half(va_lut(23), 0)));

   CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_imm_f32(256.0)),
        bi_fadd_f32_to(b, bi_register(0), bi_register(0),
                       bi_half(va_lut(23), 1)));

   CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_imm_f32(8.0)),
        bi_fadd_f32_to(b, bi_register(0), bi_register(0),
                       bi_half(va_lut(30), 1)));
}

TEST_F(LowerConstants, ReplicateFloat16)
{
   CASE(bi_fadd_v2f16_to(b, bi_register(0), bi_register(0), bi_imm_f16(255.0)),
        bi_fadd_v2f16_to(b, bi_register(0), bi_register(0),
                         bi_half(va_lut(23), 0)));

   CASE(bi_fadd_v2f16_to(b, bi_register(0), bi_register(0), bi_imm_f16(4.0)),
        bi_fadd_v2f16_to(b, bi_register(0), bi_register(0),
                         bi_half(va_lut(29), 1)));
}

TEST_F(LowerConstants, NegateFloat32)
{
   CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_imm_f32(-1.0)),
        bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_neg(va_lut(16))));

   CASE(bi_fadd_f32_to(b, bi_register(0), bi_register(0), bi_imm_f32(-255.0)),
        bi_fadd_f32_to(b, bi_register(0), bi_register(0),
                       bi_neg(bi_half(va_lut(23), 0))));
}

TEST_F(LowerConstants, NegateReplicateFloat16)
{
   CASE(bi_fadd_v2f16_to(b, bi_register(0), bi_register(0), bi_imm_f16(-255.0)),
        bi_fadd_v2f16_to(b, bi_register(0), bi_register(0),
                         bi_neg(bi_half(va_lut(23), 0))));
}

TEST_F(LowerConstants, NegateVec2Float16)
{
   CASE(
      bi_fadd_v2f16_to(b, bi_register(0), bi_register(0),
                       bi_imm_u32(0xBC008000)),
      bi_fadd_v2f16_to(b, bi_register(0), bi_register(0), bi_neg(va_lut(27))));
}

TEST_F(LowerConstants, Int8InInt32)
{
   CASE(bi_lshift_or_i32(b, bi_register(0), bi_imm_u32(0), bi_imm_u8(6)),
        bi_lshift_or_i32(b, bi_register(0), va_lut(0), bi_byte(va_lut(9), 2)));

   CASE(bi_lshift_or_i32(b, bi_register(0), bi_imm_u32(0), bi_imm_u8(-2)),
        bi_lshift_or_i32(b, bi_register(0), va_lut(0), bi_byte(va_lut(3), 0)));
}

TEST_F(LowerConstants, ZeroExtendForUnsigned)
{
   CASE(bi_icmp_and_u32_to(b, bi_register(0), bi_register(0), bi_imm_u32(0xFF),
                           bi_register(0), BI_CMPF_LT, BI_RESULT_TYPE_I1),
        bi_icmp_and_u32_to(b, bi_register(0), bi_register(0),
                           bi_byte(va_lut(1), 0), bi_register(0), BI_CMPF_LT,
                           BI_RESULT_TYPE_I1));

   CASE(
      bi_icmp_and_u32_to(b, bi_register(0), bi_register(0), bi_imm_u32(0xFFFF),
                         bi_register(0), BI_CMPF_LT, BI_RESULT_TYPE_I1),
      bi_icmp_and_u32_to(b, bi_register(0), bi_register(0),
                         bi_half(va_lut(1), 0), bi_register(0), BI_CMPF_LT,
                         BI_RESULT_TYPE_I1));
}

TEST_F(LowerConstants, SignExtendPositiveForSigned)
{
   CASE(bi_icmp_and_s32_to(b, bi_register(0), bi_register(0), bi_imm_u32(0x7F),
                           bi_register(0), BI_CMPF_LT, BI_RESULT_TYPE_I1),
        bi_icmp_and_s32_to(b, bi_register(0), bi_register(0),
                           bi_byte(va_lut(2), 3), bi_register(0), BI_CMPF_LT,
                           BI_RESULT_TYPE_I1));

   CASE(
      bi_icmp_and_s32_to(b, bi_register(0), bi_register(0), bi_imm_u32(0x7FFF),
                         bi_register(0), BI_CMPF_LT, BI_RESULT_TYPE_I1),
      bi_icmp_and_s32_to(b, bi_register(0), bi_register(0),
                         bi_half(va_lut(2), 1), bi_register(0), BI_CMPF_LT,
                         BI_RESULT_TYPE_I1));
}

TEST_F(LowerConstants, SignExtendNegativeForSigned)
{
   CASE(bi_icmp_and_s32_to(b, bi_register(0), bi_register(0),
                           bi_imm_u32(0xFFFFFFF8), bi_register(0), BI_CMPF_LT,
                           BI_RESULT_TYPE_I1),
        bi_icmp_and_s32_to(b, bi_register(0), bi_register(0),
                           bi_byte(va_lut(23), 0), bi_register(0), BI_CMPF_LT,
                           BI_RESULT_TYPE_I1));

   CASE(bi_icmp_and_s32_to(b, bi_register(0), bi_register(0),
                           bi_imm_u32(0xFFFFFAFC), bi_register(0), BI_CMPF_LT,
                           BI_RESULT_TYPE_I1),
        bi_icmp_and_s32_to(b, bi_register(0), bi_register(0),
                           bi_half(va_lut(3), 1), bi_register(0), BI_CMPF_LT,
                           BI_RESULT_TYPE_I1));
}

TEST_F(LowerConstants, DontZeroExtendForSigned)
{
   CASE(bi_icmp_and_s32_to(b, bi_register(0), bi_register(0), bi_imm_u32(0xFF),
                           bi_register(0), BI_CMPF_LT, BI_RESULT_TYPE_I1),
        bi_icmp_and_s32_to(b, bi_register(0), bi_register(0),
                           bi_iadd_imm_i32(b, va_lut(0), 0xFF), bi_register(0),
                           BI_CMPF_LT, BI_RESULT_TYPE_I1));

   CASE(
      bi_icmp_and_s32_to(b, bi_register(0), bi_register(0), bi_imm_u32(0xFFFF),
                         bi_register(0), BI_CMPF_LT, BI_RESULT_TYPE_I1),
      bi_icmp_and_s32_to(b, bi_register(0), bi_register(0),
                         bi_iadd_imm_i32(b, va_lut(0), 0xFFFF), bi_register(0),
                         BI_CMPF_LT, BI_RESULT_TYPE_I1));
}

TEST_F(LowerConstants, DontZeroExtendNegative)
{
   CASE(bi_icmp_and_u32_to(b, bi_register(0), bi_register(0),
                           bi_imm_u32(0xFFFFFFF8), bi_register(0), BI_CMPF_LT,
                           BI_RESULT_TYPE_I1),
        bi_icmp_and_u32_to(b, bi_register(0), bi_register(0),
                           bi_iadd_imm_i32(b, va_lut(0), 0xFFFFFFF8),
                           bi_register(0), BI_CMPF_LT, BI_RESULT_TYPE_I1));

   CASE(bi_icmp_and_u32_to(b, bi_register(0), bi_register(0),
                           bi_imm_u32(0xFFFFFAFC), bi_register(0), BI_CMPF_LT,
                           BI_RESULT_TYPE_I1),
        bi_icmp_and_u32_to(b, bi_register(0), bi_register(0),
                           bi_iadd_imm_i32(b, va_lut(0), 0xFFFFFAFC),
                           bi_register(0), BI_CMPF_LT, BI_RESULT_TYPE_I1));
}

TEST_F(LowerConstants, HandleTrickyNegativesFP16)
{
   CASE(
      bi_fadd_v2f16_to(b, bi_register(0), bi_register(0), bi_imm_f16(-57216.0)),
      bi_fadd_v2f16_to(b, bi_register(0), bi_register(0),
                       bi_half(va_lut(3), 1)));

   CASE(
      bi_fadd_v2f16_to(b, bi_register(0), bi_register(0), bi_imm_f16(57216.0)),
      bi_fadd_v2f16_to(b, bi_register(0), bi_register(0),
                       bi_neg(bi_half(va_lut(3), 1))));
}

TEST_F(LowerConstants, DontWidenConstantsOfVectorOps)
{
   CASE(bi_icmp_or_v2u16_to(b, bi_register(0), bi_register(0),
                            bi_imm_u32(0x00003F80), bi_register(0), BI_CMPF_EQ,
                            BI_RESULT_TYPE_M1),
        bi_icmp_or_v2u16_to(b, bi_register(0), bi_register(0),
                            bi_iadd_imm_i32(b, va_lut(0), 0x00003F80),
                            bi_register(0), BI_CMPF_EQ, BI_RESULT_TYPE_M1));

   CASE(bi_icmp_or_v4u8_to(b, bi_register(0), bi_register(0),
                           bi_imm_u32(0x00000005), bi_register(0), BI_CMPF_EQ,
                           BI_RESULT_TYPE_M1),
        bi_icmp_or_v4u8_to(b, bi_register(0), bi_register(0),
                           bi_iadd_imm_i32(b, va_lut(0), 0x00000005),
                           bi_register(0), BI_CMPF_EQ, BI_RESULT_TYPE_M1));
}

TEST_F(LowerConstants, MaintainMkvecRestrictedSwizzles)
{
   CASE(bi_mkvec_v2i8_to(b, bi_register(0), bi_register(0), bi_imm_u8(0),
                         bi_imm_u32(0)),
        bi_mkvec_v2i8_to(b, bi_register(0), bi_register(0),
                         bi_byte(va_lut(0), 0), va_lut(0)));

   CASE(bi_mkvec_v2i8_to(b, bi_register(0), bi_register(0), bi_imm_u8(14),
                         bi_imm_u32(0)),
        bi_mkvec_v2i8_to(b, bi_register(0), bi_register(0),
                         bi_byte(va_lut(11), 2), va_lut(0)));
}
