source: mainline/kernel/test/fpu/fpu1.c@ 201abde

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

make thread ID 64 bit (task ID is 64 bit already)
cleanup thread syscalls

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[b312247]1/*
[df4ed85]2 * Copyright (c) 2005 Jakub Vana
3 * Copyright (c) 2005 Jakub Jermar
[b312247]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 */
[6de2480e]29
[34db7fa]30#if (defined(ia32) || defined(amd64) || defined(ia64) || defined(ia32xen))
31
[6de2480e]32#include <print.h>
33#include <debug.h>
34
35#include <test.h>
[23684b7]36#include <atomic.h>
[6de2480e]37#include <proc/thread.h>
38
[32a89bf]39#include <arch.h>
[9e1c942]40#include <arch/arch.h>
[32a89bf]41
[50661ab]42
[34db7fa]43#define THREADS 150
[41fa6f2]44#define ATTEMPTS 100
[c01bd280]45
46#define E_10e8 271828182
47#define PI_10e8 314159265
48
[9e1c942]49
[6eabb6e6]50#ifdef KERN_ia32_ARCH_H_
[34db7fa]51static inline double sqrt(double x)
52{
53 double v;
54
55 asm (
56 "fsqrt\n"
57 : "=t" (v)
58 : "0" (x)
59 );
60
61 return v;
62}
[9e1c942]63#endif
64
[6eabb6e6]65#ifdef KERN_amd64_ARCH_H_
[34db7fa]66static inline double sqrt(double x)
67{
68 double v;
69
70 asm (
71 "fsqrt\n"
72 : "=t" (v)
73 : "0" (x)
74 );
75
76 return v;
77}
[9e1c942]78#endif
79
[6eabb6e6]80#ifdef KERN_ia64_ARCH_H_
[34db7fa]81
82#undef PI_10e8
83#define PI_10e8 3141592
84
[9e1c942]85static inline long double sqrt(long double a)
86{
87 long double x = 1;
88 long double lx = 0;
89
[34db7fa]90 if (a < 0.00000000000000001)
91 return 0;
[9e1c942]92
[34db7fa]93 while(x != lx) {
94 lx = x;
95 x = (x + (a / x)) / 2;
[9e1c942]96 }
[34db7fa]97
[9e1c942]98 return x;
99}
100#endif
101
102
[e507afa]103static atomic_t threads_ok;
[34db7fa]104static atomic_t threads_fault;
[c01bd280]105static waitq_t can_start;
[c8410ec9]106static bool sh_quiet;
[c01bd280]107
[b312247]108static void e(void *data)
[6de2480e]109{
[54ca3523]110 int i;
[c8410ec9]111 double e, d, le, f;
[c01bd280]112
[8d6d76a]113 thread_detach(THREAD);
114
[c01bd280]115 waitq_sleep(&can_start);
116
[54ca3523]117 for (i = 0; i<ATTEMPTS; i++) {
[34db7fa]118 le = -1;
119 e = 0;
120 f = 1;
[c01bd280]121
[34db7fa]122 for (d = 1; e != le; d *= f, f += 1) {
123 le = e;
124 e = e + 1 / d;
[54ca3523]125 }
126
[34db7fa]127 if ((int) (100000000 * e) != E_10e8) {
[c8410ec9]128 if (!sh_quiet)
[201abde]129 printf("tid%llu: e*10e8=%zd should be %zd\n", THREAD->tid, (unative_t) (100000000 * e), (unative_t) E_10e8);
[34db7fa]130 atomic_inc(&threads_fault);
131 break;
132 }
[c01bd280]133 }
[992bbb97]134 atomic_inc(&threads_ok);
[6de2480e]135}
136
[c01bd280]137static void pi(void *data)
138{
[54ca3523]139 int i;
[76cec1e]140 double lpi, pi;
141 double n, ab, ad;
[8d6d76a]142
143 thread_detach(THREAD);
[c01bd280]144
145 waitq_sleep(&can_start);
146
[34db7fa]147 for (i = 0; i < ATTEMPTS; i++) {
[54ca3523]148 lpi = -1;
149 pi = 0;
150
[34db7fa]151 for (n = 2, ab = sqrt(2); lpi != pi; n *= 2, ab = ad) {
[76cec1e]152 double sc, cd;
[c01bd280]153
[34db7fa]154 sc = sqrt(1 - (ab * ab / 4));
[76cec1e]155 cd = 1 - sc;
[34db7fa]156 ad = sqrt(ab * ab / 4 + cd * cd);
[76cec1e]157 lpi = pi;
158 pi = 2 * n * ad;
159 }
[54ca3523]160
[6eabb6e6]161#ifdef KERN_ia64_ARCH_H_
[34db7fa]162 if ((int) (1000000 * pi) != PI_10e8) {
[c8410ec9]163 if (!sh_quiet)
[201abde]164 printf("tid%llu: pi*10e8=%zd should be %zd\n", THREAD->tid, (unative_t) (1000000 * pi), (unative_t) (PI_10e8 / 100));
[34db7fa]165 atomic_inc(&threads_fault);
166 break;
167 }
[9e1c942]168#else
[34db7fa]169 if ((int) (100000000 * pi) != PI_10e8) {
[c8410ec9]170 if (!sh_quiet)
[201abde]171 printf("tid%llu: pi*10e8=%zd should be %zd\n", THREAD->tid, (unative_t) (100000000 * pi), (unative_t) PI_10e8);
[34db7fa]172 atomic_inc(&threads_fault);
173 break;
174 }
[9e1c942]175#endif
[c01bd280]176 }
[992bbb97]177 atomic_inc(&threads_ok);
[c01bd280]178}
[6de2480e]179
[95155b0c]180char * test_fpu1(bool quiet)
[6de2480e]181{
[34db7fa]182 unsigned int i, total = 0;
[c8410ec9]183 sh_quiet = quiet;
[6de2480e]184
[c01bd280]185 waitq_initialize(&can_start);
[34db7fa]186 atomic_set(&threads_ok, 0);
187 atomic_set(&threads_fault, 0);
[c8410ec9]188
189 if (!quiet)
190 printf("Creating %d threads... ", 2 * THREADS);
[c01bd280]191
[34db7fa]192 for (i = 0; i < THREADS; i++) {
193 thread_t *t;
194
[62b6d17]195 if (!(t = thread_create(e, NULL, TASK, 0, "e", false))) {
[c8410ec9]196 if (!quiet)
197 printf("could not create thread %d\n", 2 * i);
[34db7fa]198 break;
199 }
[c01bd280]200 thread_ready(t);
[34db7fa]201 total++;
202
[62b6d17]203 if (!(t = thread_create(pi, NULL, TASK, 0, "pi", false))) {
[c8410ec9]204 if (!quiet)
205 printf("could not create thread %d\n", 2 * i + 1);
[34db7fa]206 break;
207 }
[b312247]208 thread_ready(t);
[34db7fa]209 total++;
[b312247]210 }
[c8410ec9]211
212 if (!quiet)
213 printf("ok\n");
[af22f158]214
[c01bd280]215 thread_sleep(1);
216 waitq_wakeup(&can_start, WAKEUP_ALL);
[34db7fa]217
218 while (atomic_get(&threads_ok) != total) {
[c8410ec9]219 if (!quiet)
220 printf("Threads left: %d\n", total - atomic_get(&threads_ok));
[34db7fa]221 thread_sleep(1);
222 }
223
224 if (atomic_get(&threads_fault) == 0)
225 return NULL;
226
227 return "Test failed";
[6de2480e]228}
[50661ab]229
230#endif
Note: See TracBrowser for help on using the repository browser.