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
drm_test_sm2fixp(struct kunit * test)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
drm_test_int2fixp(struct kunit * test)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");
72