[v3,14/14] DNI: test: libipa: fixedpoint: Validate extra types
diff mbox series

Message ID 20251113122450.287633-15-kieran.bingham@ideasonboard.com
State New
Headers show
Series
  • libipa: Introduce a Quantized type
Related show

Commit Message

Kieran Bingham Nov. 13, 2025, 12:24 p.m. UTC
Check that the Cproc manual types can be directly served by the new
fixed point types.

DNI: This patch is just here to validate that my conversions are using
the right types and work as the original code did. I don't expect this
one to be merged.

Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 test/ipa/libipa/fixedpoint.cpp | 68 ++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)

Patch
diff mbox series

diff --git a/test/ipa/libipa/fixedpoint.cpp b/test/ipa/libipa/fixedpoint.cpp
index 7d1aeebc06da..94930d77668d 100644
--- a/test/ipa/libipa/fixedpoint.cpp
+++ b/test/ipa/libipa/fixedpoint.cpp
@@ -252,6 +252,71 @@  protected:
 		return fails ? TestFail : TestPass;
 	}
 
+	struct BrightnessTraits {
+		using QuantizedType = int8_t;
+		static constexpr QuantizedType fromFloat(float v)
+		{
+			int quantized = std::lround(v * 128.0f);
+			return static_cast<int8_t>(std::clamp<int>(quantized, -128, 127));
+		}
+		static float toFloat(QuantizedType v)
+		{
+			return static_cast<float>(v) / 128.0f;
+		}
+	};
+
+	using BrightnessQOriginal = Quantized<BrightnessTraits>;
+
+	struct ContrastSaturationTraits {
+		using QuantizedType = uint8_t;
+		static constexpr QuantizedType fromFloat(float v)
+		{
+			int quantized = std::lround(v * 128.0f);
+			return static_cast<uint8_t>(std::clamp<int>(quantized, 0, 255));
+		}
+		static float toFloat(QuantizedType v)
+		{
+			return static_cast<float>(v) / 128.0f;
+		}
+	};
+
+	using ContrastQOriginal = Quantized<ContrastSaturationTraits>;
+
+	/* FixedPoint equivalents to validate */
+	using BrightnessQ = Quantized<FixedPointQTraits<1, 7, int8_t>>;
+	using ContrastQ = Quantized<FixedPointQTraits<1, 7, uint8_t>>;
+
+	template<typename T1, typename T2>
+	int testTwoIdenticalTypes(float v)
+	{
+		T1 a(v);
+		T2 b(v);
+
+		std::cout << "  Checking " << a.toString()
+			  << " == " << b.toString()
+			  << std::endl;
+
+		/* 0/false success, 1/true fail */
+		return (a.quantized() != b.quantized());
+	}
+
+	int testCprocFixedPointConversion()
+	{
+		unsigned int fails = 0;
+
+		std::cout << std::endl
+			  << "CPROC Fixed-Point Quantizer conversion tests:"
+			  << std::endl;
+
+		fails += testTwoIdenticalTypes<BrightnessQOriginal, BrightnessQ>(BrightnessQ::TraitsType::min);
+		fails += testTwoIdenticalTypes<BrightnessQOriginal, BrightnessQ>(BrightnessQ::TraitsType::max);
+
+		fails += testTwoIdenticalTypes<ContrastQOriginal, ContrastQ>(ContrastQ::TraitsType::min);
+		fails += testTwoIdenticalTypes<ContrastQOriginal, ContrastQ>(ContrastQ::TraitsType::max);
+
+		return fails ? TestFail : TestPass;
+	}
+
 	int run()
 	{
 		unsigned int fails = 0;
@@ -266,6 +331,9 @@  protected:
 		if (testScaledFixedPointQuantizers() != TestPass)
 			fails++;
 
+		if (testCprocFixedPointConversion() != TestPass)
+			fails++;
+
 		return fails ? TestFail : TestPass;
 	}
 };