Skip to content

Commit b7f5138

Browse files
Harry Wentlandemersion
authored andcommitted
drm/tests: Add a few tests around drm_fixed.h
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
1 parent ea3f6ba commit b7f5138

2 files changed

Lines changed: 73 additions & 1 deletion

File tree

drivers/gpu/drm/tests/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ obj-$(CONFIG_DRM_KUNIT_TEST) += \
2424
drm_plane_helper_test.o \
2525
drm_probe_helper_test.o \
2626
drm_rect_test.o \
27-
drm_sysfb_modeset_test.o
27+
drm_sysfb_modeset_test.o \
28+
drm_fixp_test.o
2829

2930
CFLAGS_drm_mm_test.o := $(DISABLE_STRUCTLEAK_PLUGIN)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// SPDX-License-Identifier: MIT
2+
/*
3+
* Copyright 2022 Advanced Micro Devices, Inc.
4+
*/
5+
6+
#include <kunit/test.h>
7+
#include <drm/drm_fixed.h>
8+
9+
static void drm_test_sm2fixp(struct kunit *test)
10+
{
11+
KUNIT_EXPECT_EQ(test, 0x7fffffffffffffffll, ((1ull << 63) - 1));
12+
13+
/* 1 */
14+
KUNIT_EXPECT_EQ(test, drm_int2fixp(1), drm_sm2fixp(1ull << DRM_FIXED_POINT));
15+
16+
/* -1 */
17+
KUNIT_EXPECT_EQ(test, drm_int2fixp(-1),
18+
drm_sm2fixp((1ull << 63) | (1ull << DRM_FIXED_POINT)));
19+
20+
/* 0.5 */
21+
KUNIT_EXPECT_EQ(test, drm_fixp_from_fraction(1, 2),
22+
drm_sm2fixp(1ull << (DRM_FIXED_POINT - 1)));
23+
24+
/* -0.5 */
25+
KUNIT_EXPECT_EQ(test, drm_fixp_from_fraction(-1, 2),
26+
drm_sm2fixp((1ull << 63) | (1ull << (DRM_FIXED_POINT - 1))));
27+
}
28+
29+
static void drm_test_int2fixp(struct kunit *test)
30+
{
31+
/* 1 */
32+
KUNIT_EXPECT_EQ(test, 1ll << 32, drm_int2fixp(1));
33+
34+
/* -1 */
35+
KUNIT_EXPECT_EQ(test, -(1ll << 32), drm_int2fixp(-1));
36+
37+
/* 1 + (-1) = 0 */
38+
KUNIT_EXPECT_EQ(test, 0, drm_int2fixp(1) + drm_int2fixp(-1));
39+
40+
/* 1 / 2 */
41+
KUNIT_EXPECT_EQ(test, 1ll << 31, drm_fixp_from_fraction(1, 2));
42+
43+
/* -0.5 */
44+
KUNIT_EXPECT_EQ(test, -(1ll << 31), drm_fixp_from_fraction(-1, 2));
45+
46+
/* (1 / 2) + (-1) = 0.5 */
47+
KUNIT_EXPECT_EQ(test, 1ll << 31, drm_fixp_from_fraction(-1, 2) + drm_int2fixp(1));
48+
49+
/* (1 / 2) - 1) = 0.5 */
50+
KUNIT_EXPECT_EQ(test, -(1ll << 31), drm_fixp_from_fraction(1, 2) + drm_int2fixp(-1));
51+
52+
/* (1 / 2) - 1) = 0.5 */
53+
KUNIT_EXPECT_EQ(test, -(1ll << 31), drm_fixp_from_fraction(1, 2) - drm_int2fixp(1));
54+
}
55+
56+
static struct kunit_case drm_fixp_tests[] = {
57+
KUNIT_CASE(drm_test_int2fixp),
58+
KUNIT_CASE(drm_test_sm2fixp),
59+
{ }
60+
};
61+
62+
static struct kunit_suite drm_fixp_test_suite = {
63+
.name = "drm_fixp",
64+
.test_cases = drm_fixp_tests,
65+
};
66+
67+
kunit_test_suite(drm_fixp_test_suite);
68+
69+
MODULE_AUTHOR("AMD");
70+
MODULE_LICENSE("Dual MIT/GPL");
71+
MODULE_DESCRIPTION("Unit tests for drm_fixed.h");

0 commit comments

Comments
 (0)