cleanup specfile for packaging
[profile/ivi/gpsd.git] / test_float.c
1 /*
2  * This file is Copyright (c) 2010 by the GPSD project
3  * BSD terms apply: see the file COPYING in the distribution root for details.
4  */
5 #include <stdio.h>
6
7 /*
8  * Copyright (c) 2006 Chris Kuethe <chris.kuethe@gmail.com>
9  *
10  * Permission to use, copy, modify, and distribute this software for any
11  * purpose with or without fee is hereby granted, provided that the above
12  * copyright notice and this permission notice appear in all copies.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 /*
24  * this simple program tests to see whether your system can do proper
25  * single and double precision floating point. This is apparently Very
26  * Hard To Do(tm) on embedded systems, judging by the number of broken
27  * ARM toolchains I've seen... :(
28  *
29  * compile with: gcc -O -o test_float test_float.c
30  *     (use whatever -O level you like)
31  */
32
33 int main( void );
34 int test_single( void );
35 int test_double( void );
36
37 int main() {
38         int i, j;
39
40         if ((i = test_single()))
41                 printf("WARNING: Single-precision "
42                         "floating point math might be broken\n");
43
44         if ((j = test_double()))
45                 printf("WARNING: Double-precision "
46                         "floating point math might be broken\n");
47
48         i += j;
49         if (i == 0)
50                 printf("floating point math appears to work\n");
51         return i;
52 }
53
54 int test_single() {
55         static float f;
56         static int i;
57         static int e = 0;
58
59         /* addition test */
60         f = 1.0;
61         for(i = 0; i < 10; i++)
62                 f += (1<<i);
63         if (f != 1024.0) {
64                 printf("s1 ");
65                 e++;
66         }
67
68         /* subtraction test */
69         f = 1024.0;
70         for(i = 0; i < 10; i++)
71                 f -= (1<<i);
72         if (f != 1.0) {
73                 printf("s2 ");
74                 e++;
75         }
76
77         /* multiplication test */
78         f = 1.0;
79         for(i = 1; i < 10; i++)
80                 f *= i;
81         if (f != 362880.0) {
82                 printf("s3 ");
83                 e++;
84         }
85
86         /* division test */
87         f = 362880.0;
88         for(i = 1; i < 10; i++)
89                 f /= i;
90         if (f != 1.0) {
91                 printf("s4 ");
92                 e++;
93         }
94
95         /* multiply-accumulate test */
96         f = 0.5;
97         for(i = 1; i < 1000000; i++) {
98                 f += 2.0;
99                 f *= 0.5;
100         }
101         if (f != 2.0) {
102                 printf("s5 ");
103                 e++;
104         }
105
106         /* divide-subtract test */
107         f = 2.0;
108         for(i = 1; i < 1000000; i++) {
109                 f /= 0.5;
110                 f -= 2.0;
111         }
112         if (f != 2.0) {
113                 printf("s6 ");
114                 e++;
115         }
116
117         /* add-multiply-subtract-divide test */
118         f = 1000000.0;
119         for(i = 1; i < 1000000; i++)
120                 f = ((((f + 1.5) * 0.5) - 1.25) / 0.5);
121         if (f != 1.0) {
122                 printf("s7 ");
123                 e++;
124         }
125
126         /* multiply-add-divide-subtract test */
127         f = 1.0;
128         for(i = 1; i < 1000000; i++)
129                 f = ((((f * 5.0) + 3.0) / 2.0) - 3.0);
130         if (f != 1.0)
131                 printf("s8 ");
132
133         /* subtract-divide-add-multiply test */
134         f = 8.0;
135         for(i = 1; i < 1000000; i++)
136                 f = ((((f - 5.0) / 2.0) + 2.5) * 2.0);
137         if (f != 8.0) {
138                 printf("s9 ");
139                 e++;
140         }
141
142         /* divide-subtract-multiply-add test */
143         f = 42.0;
144         for(i = 1; i < 1000000; i++)
145                 f = ((((f / 6.0) - 5.0) * 19.75 ) + 2.5);
146         if (f != 42.0) {
147                 printf("s10 ");
148                 e++;
149         }
150         if (e) {
151                 printf("\n");
152                 return 1;
153         }
154         return 0;
155 }
156
157
158 int test_double() {
159         static double f;
160         static int i;
161         static int e = 0;
162
163         /* addition test */
164         f = 1.0;
165         for(i = 0; i < 10; i++)
166                 f += (1<<i);
167         if (f != 1024.0) {
168                 printf("d1 ");
169                 e++;
170         }
171
172         /* subtraction test */
173         f = 1024.0;
174         for(i = 0; i < 10; i++)
175                 f -= (1<<i);
176         if (f != 1.0) {
177                 printf("d2 ");
178                 e++;
179         }
180
181         /* multiplication test */
182         f = 1.0;
183         for(i = 1; i < 10; i++)
184                 f *= i;
185         if (f != 362880.0) {
186                 printf("d3 ");
187                 e++;
188         }
189
190         /* division test */
191         f = 362880.0;
192         for(i = 1; i < 10; i++)
193                 f /= i;
194         if (f != 1.0) {
195                 printf("d4 ");
196                 e++;
197         }
198
199         /* multiply-accumulate test */
200         f = 0.5;
201         for(i = 1; i < 1000000; i++) {
202                 f += 2.0;
203                 f *= 0.5;
204         }
205         if (f != 2.0) {
206                 printf("d5 ");
207                 e++;
208         }
209
210         /* divide-subtract test */
211         f = 2.0;
212         for(i = 1; i < 1000000; i++) {
213                 f /= 0.5;
214                 f -= 2.0;
215         }
216         if (f != 2.0) {
217                 printf("d6 ");
218                 e++;
219         }
220
221         /* add-multiply-subtract-divide test */
222         f = 1000000.0;
223         for(i = 1; i < 1000000; i++)
224                 f = ((((f + 1.5) * 0.5) - 1.25) / 0.5);
225         if (f != 1.0) {
226                 printf("d7 ");
227                 e++;
228         }
229
230         /* multiply-add-divide-subtract test */
231         f = 1.0;
232         for(i = 1; i < 1000000; i++)
233                 f = ((((f * 5.0) + 3.0) / 2.0) - 3.0);
234         if (f != 1.0)
235                 printf("d8 ");
236
237         /* subtract-divide-add-multiply test */
238         f = 8.0;
239         for(i = 1; i < 1000000; i++)
240                 f = ((((f - 5.0) / 2.0) + 2.5) * 2.0);
241         if (f != 8.0) {
242                 printf("d9 ");
243                 e++;
244         }
245
246         /* divide-subtract-multiply-add test */
247         f = 42.0;
248         for(i = 1; i < 1000000; i++)
249                 f = ((((f / 6.0) - 5.0) * 19.75 ) + 2.5);
250         if (f != 42.0) {
251                 printf("d10 ");
252                 e++;
253         }
254         if (e) {
255                 printf("\n");
256                 return 1;
257         }
258         return 0;
259 }