source: mainline/kernel/test/fpu/fpu1.c@ dff0a94

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since dff0a94 was 50661ab, checked in by Martin Decky <martin@…>, 19 years ago

integrate more tests

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * Copyright (C) 2005 Jakub Vana
3 * Copyright (C) 2005 Jakub Jermar
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <print.h>
31#include <debug.h>
32#include <panic.h>
33
34#include <test.h>
35#include <atomic.h>
36#include <proc/thread.h>
37
38#include <arch.h>
39#include <arch/arch.h>
40
41#ifdef CONFIG_BENCH
42#include <arch/cycle.h>
43#endif
44
45#if (defined(ia32) || defined(amd64) || defined(ia64) || defined(ia32xen))
46
47#define THREADS 150*2
48#define ATTEMPTS 100
49
50#define E_10e8 271828182
51#define PI_10e8 314159265
52
53
54#ifdef KERN_ia32_ARCH_H_
55static inline double sqrt(double x) { double v; __asm__ ("fsqrt\n" : "=t" (v) : "0" (x)); return v; }
56#endif
57
58#ifdef KERN_amd64_ARCH_H_
59static inline double sqrt(double x) { double v; __asm__ ("fsqrt\n" : "=t" (v) : "0" (x)); return v; }
60#endif
61
62#ifdef KERN_ia64_ARCH_H_
63static inline long double sqrt(long double a)
64{
65 long double x = 1;
66 long double lx = 0;
67
68 if(a<0.00000000000000001) return 0;
69
70 while(x!=lx)
71 {
72 lx=x;
73 x=(x+(a/x))/2;
74 }
75 return x;
76}
77#endif
78
79
80
81static atomic_t threads_ok;
82static waitq_t can_start;
83
84static void e(void *data)
85{
86 int i;
87 double e,d,le,f;
88
89 thread_detach(THREAD);
90
91 waitq_sleep(&can_start);
92
93 for (i = 0; i<ATTEMPTS; i++) {
94 le=-1;
95 e=0;
96 f=1;
97
98 for(d=1;e!=le;d*=f,f+=1) {
99 le=e;
100 e=e+1/d;
101 }
102
103 if((int)(100000000*e)!=E_10e8)
104 panic("tid%d: e*10e8=%zd should be %zd\n", THREAD->tid, (unative_t) (100000000*e),(unative_t) E_10e8);
105 }
106
107 printf("tid%d: e*10e8=%zd should be %zd\n", THREAD->tid, (unative_t) (100000000*e),(unative_t) E_10e8);
108 atomic_inc(&threads_ok);
109}
110
111static void pi(void *data)
112{
113
114#ifdef KERN_ia64_ARCH_H_
115#undef PI_10e8
116#define PI_10e8 3141592
117#endif
118
119
120 int i;
121 double lpi, pi;
122 double n, ab, ad;
123
124 thread_detach(THREAD);
125
126 waitq_sleep(&can_start);
127
128
129 for (i = 0; i<ATTEMPTS; i++) {
130 lpi = -1;
131 pi = 0;
132
133 for (n=2, ab = sqrt(2); lpi != pi; n *= 2, ab = ad) {
134 double sc, cd;
135
136 sc = sqrt(1 - (ab*ab/4));
137 cd = 1 - sc;
138 ad = sqrt(ab*ab/4 + cd*cd);
139 lpi = pi;
140 pi = 2 * n * ad;
141 }
142
143#ifdef KERN_ia64_ARCH_H_
144 if((int)(1000000*pi)!=PI_10e8)
145 panic("tid%d: pi*10e8=%zd should be %zd\n", THREAD->tid, (unative_t) (1000000*pi),(unative_t) (PI_10e8/100));
146#else
147 if((int)(100000000*pi)!=PI_10e8)
148 panic("tid%d: pi*10e8=%zd should be %zd\n", THREAD->tid, (unative_t) (100000000*pi),(unative_t) PI_10e8);
149#endif
150
151 }
152
153 printf("tid%d: pi*10e8=%zd should be %zd\n", THREAD->tid, (unative_t) (100000000*pi),(unative_t) PI_10e8);
154 atomic_inc(&threads_ok);
155}
156
157void test_fpu1(void)
158{
159#ifdef CONFIG_BENCH
160 uint64_t t0 = get_cycle();
161#endif
162 thread_t *t;
163 int i;
164
165 waitq_initialize(&can_start);
166
167 printf("FPU test #1\n");
168 printf("Creating %d threads... ", THREADS);
169
170 for (i=0; i<THREADS/2; i++) {
171 if (!(t = thread_create(e, NULL, TASK, 0, "e")))
172 panic("could not create thread\n");
173 thread_ready(t);
174 if (!(t = thread_create(pi, NULL, TASK, 0, "pi")))
175 panic("could not create thread\n");
176 thread_ready(t);
177 }
178 printf("ok\n");
179
180 thread_sleep(1);
181 waitq_wakeup(&can_start, WAKEUP_ALL);
182
183 while (atomic_get(&threads_ok) != THREADS)
184 ;
185
186 printf("Test passed.\n");
187#ifdef CONFIG_BENCH
188 uint64_t dt = get_cycle() - t0;
189 printf("Time: %.*d cycles\n", sizeof(dt) * 2, dt);
190#endif
191}
192
193#else
194
195void test_fpu1(void)
196{
197 printf("This test is available only on Intel/AMD platforms.");
198}
199
200#endif
Note: See TracBrowser for help on using the repository browser.