File: drm_fixp_test.c

package info (click to toggle)
linux 6.19.2-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,759,612 kB
  • sloc: ansic: 27,004,852; asm: 273,402; sh: 151,313; python: 81,277; makefile: 58,544; perl: 34,311; xml: 21,064; cpp: 5,984; yacc: 4,841; lex: 2,901; awk: 1,707; sed: 30; ruby: 25
file content (71 lines) | stat: -rw-r--r-- 1,840 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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");