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