]> Gentwo Git Trees - linux/.git/commitdiff
drm/tests: Add a few tests around drm_fixed.h
authorHarry Wentland <harry.wentland@amd.com>
Sat, 15 Nov 2025 00:01:46 +0000 (17:01 -0700)
committerSimon Ser <contact@emersion.fr>
Wed, 26 Nov 2025 22:03:33 +0000 (23:03 +0100)
While working on the CTM implementation of VKMS I had to ascertain
myself of a few assumptions. One of those is whether drm_fixed.h
treats its numbers using signed-magnitude or twos-complement. It is
twos-complement.

In order to make someone else's day easier I am adding the
drm_test_int2fixp test that validates the above assumption.

I am also adding a test for the new sm2fixp function that converts
from a signed-magnitude fixed point to the twos-complement fixed
point.

Reviewed-by: Louis Chauvet <louis.chauvet@bootlin.com>
Signed-off-by: Alex Hung <alex.hung@amd.com>
Signed-off-by: Harry Wentland <harry.wentland@amd.com>
Reviewed-by: Daniel Stone <daniels@collabora.com>
Signed-off-by: Simon Ser <contact@emersion.fr>
Link: https://patch.msgid.link/20251115000237.3561250-22-alex.hung@amd.com
drivers/gpu/drm/tests/Makefile
drivers/gpu/drm/tests/drm_fixp_test.c [new file with mode: 0644]

index c0e952293ad021ba6f7da371ffcf6074734bc7c1..87d5d5f9332acbf730bd28c1de049a6855167c9b 100644 (file)
@@ -24,6 +24,7 @@ obj-$(CONFIG_DRM_KUNIT_TEST) += \
        drm_plane_helper_test.o \
        drm_probe_helper_test.o \
        drm_rect_test.o \
-       drm_sysfb_modeset_test.o
+       drm_sysfb_modeset_test.o \
+       drm_fixp_test.o
 
 CFLAGS_drm_mm_test.o := $(DISABLE_STRUCTLEAK_PLUGIN)
diff --git a/drivers/gpu/drm/tests/drm_fixp_test.c b/drivers/gpu/drm/tests/drm_fixp_test.c
new file mode 100644 (file)
index 0000000..dd77fde
--- /dev/null
@@ -0,0 +1,71 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright 2022 Advanced Micro Devices, Inc.
+ */
+
+#include <kunit/test.h>
+#include <drm/drm_fixed.h>
+
+static void drm_test_sm2fixp(struct kunit *test)
+{
+       KUNIT_EXPECT_EQ(test, 0x7fffffffffffffffll, ((1ull << 63) - 1));
+
+       /* 1 */
+       KUNIT_EXPECT_EQ(test, drm_int2fixp(1), drm_sm2fixp(1ull << DRM_FIXED_POINT));
+
+       /* -1 */
+       KUNIT_EXPECT_EQ(test, drm_int2fixp(-1),
+                       drm_sm2fixp((1ull << 63) | (1ull << DRM_FIXED_POINT)));
+
+       /* 0.5 */
+       KUNIT_EXPECT_EQ(test, drm_fixp_from_fraction(1, 2),
+                       drm_sm2fixp(1ull << (DRM_FIXED_POINT - 1)));
+
+       /* -0.5 */
+       KUNIT_EXPECT_EQ(test, drm_fixp_from_fraction(-1, 2),
+                       drm_sm2fixp((1ull << 63) | (1ull << (DRM_FIXED_POINT - 1))));
+}
+
+static void drm_test_int2fixp(struct kunit *test)
+{
+       /* 1 */
+       KUNIT_EXPECT_EQ(test, 1ll << 32, drm_int2fixp(1));
+
+       /* -1 */
+       KUNIT_EXPECT_EQ(test, -(1ll << 32), drm_int2fixp(-1));
+
+       /* 1 + (-1) = 0 */
+       KUNIT_EXPECT_EQ(test, 0, drm_int2fixp(1) + drm_int2fixp(-1));
+
+       /* 1 / 2 */
+       KUNIT_EXPECT_EQ(test, 1ll << 31, drm_fixp_from_fraction(1, 2));
+
+       /* -0.5 */
+       KUNIT_EXPECT_EQ(test, -(1ll << 31), drm_fixp_from_fraction(-1, 2));
+
+       /* (1 / 2) + (-1) = 0.5 */
+       KUNIT_EXPECT_EQ(test, 1ll << 31, drm_fixp_from_fraction(-1, 2) + drm_int2fixp(1));
+
+       /* (1 / 2) - 1) = 0.5 */
+       KUNIT_EXPECT_EQ(test, -(1ll << 31), drm_fixp_from_fraction(1, 2) + drm_int2fixp(-1));
+
+       /* (1 / 2) - 1) = 0.5 */
+       KUNIT_EXPECT_EQ(test, -(1ll << 31), drm_fixp_from_fraction(1, 2) - drm_int2fixp(1));
+}
+
+static struct kunit_case drm_fixp_tests[] = {
+       KUNIT_CASE(drm_test_int2fixp),
+       KUNIT_CASE(drm_test_sm2fixp),
+       { }
+};
+
+static struct kunit_suite drm_fixp_test_suite = {
+       .name = "drm_fixp",
+       .test_cases = drm_fixp_tests,
+};
+
+kunit_test_suite(drm_fixp_test_suite);
+
+MODULE_AUTHOR("AMD");
+MODULE_LICENSE("Dual MIT/GPL");
+MODULE_DESCRIPTION("Unit tests for drm_fixed.h");