xref: /openbmc/u-boot/post/lib_powerpc/complex.c (revision e3963c09)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2002
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6 
7 #include <common.h>
8 
9 /*
10  * CPU test
11  * Complex calculations
12  *
13  * The calculations in this test are just a combination of simpler
14  * calculations, but probably under different timing conditions, etc.
15  */
16 
17 #include <post.h>
18 #include "cpu_asm.h"
19 
20 #if CONFIG_POST & CONFIG_SYS_POST_CPU
21 
22 extern int cpu_post_complex_1_asm (int a1, int a2, int a3, int a4, int n);
23 extern int cpu_post_complex_2_asm (int x, int n);
24 
25   /*
26    *     n
27    *	SUM (a1 * a2 - a3) / a4 = n * result
28    *    i=1
29    */
30 static int cpu_post_test_complex_1 (void)
31 {
32     int a1 = 666;
33     int a2 = 667;
34     int a3 = 668;
35     int a4 = 66;
36     int n = 100;
37     int result = 6720; /* (a1 * a2 - a3) / a4 */
38 
39     if (cpu_post_complex_1_asm(a1, a2, a3, a4, n) != n * result)
40     {
41 	return -1;
42     }
43 
44     return 0;
45 }
46 
47   /*	(1 + x + x^2 + ... + x^n) * (1 - x) = 1 - x^(n+1)
48    */
49 static int cpu_post_test_complex_2 (void)
50 {
51     int ret = -1;
52     int x;
53     int n;
54     int k;
55     int left;
56     int right;
57 
58     for (x = -8; x <= 8; x ++)
59     {
60 	n = 9;
61 
62 	left = cpu_post_complex_2_asm(x, n);
63 	left *= 1 - x;
64 
65 	right = 1;
66 	for (k = 0; k <= n; k ++)
67 	{
68 	    right *= x;
69 	}
70 	right = 1 - right;
71 
72 	if (left != right)
73 	{
74 	    goto Done;
75 	}
76     }
77 
78     ret = 0;
79     Done:
80 
81     return ret;
82 }
83 
84 int cpu_post_test_complex (void)
85 {
86     int ret = 0;
87     int flag = disable_interrupts();
88 
89     if (ret == 0)
90     {
91 	ret = cpu_post_test_complex_1();
92     }
93 
94     if (ret == 0)
95     {
96 	ret = cpu_post_test_complex_2();
97     }
98 
99     if (ret != 0)
100     {
101 	post_log ("Error at complex test !\n");
102     }
103 
104     if (flag)
105 	enable_interrupts();
106 
107     return ret;
108 }
109 
110 #endif
111