source: mainline/test/fpu/fpu1/test.c@ 50de918

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 50de918 was 280a27e, checked in by Josef Cejka <malyzelenyhnus@…>, 19 years ago

Printf ported back from uspace to kernel.
Printf calls changed to match new conventions.

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[b312247]1/*
2 * Copyright (C) 2005 Jakub Vana
[c01bd280]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
30#include <print.h>
31#include <debug.h>
32#include <panic.h>
33
34#include <test.h>
[23684b7]35#include <atomic.h>
[6de2480e]36#include <proc/thread.h>
37
[32a89bf]38#include <arch.h>
[9e1c942]39#include <arch/arch.h>
[32a89bf]40
[41fa6f2]41#define THREADS 150*2
42#define ATTEMPTS 100
[c01bd280]43
44#define E_10e8 271828182
45#define PI_10e8 314159265
46
[9e1c942]47
48#ifdef __ia32_ARCH_H__
49static inline double sqrt(double x) { double v; __asm__ ("fsqrt\n" : "=t" (v) : "0" (x)); return v; }
50#endif
51
52#ifdef __amd64_ARCH_H__
[c01bd280]53static inline double sqrt(double x) { double v; __asm__ ("fsqrt\n" : "=t" (v) : "0" (x)); return v; }
[9e1c942]54#endif
55
56#ifdef __ia64_ARCH_H__
57static inline long double sqrt(long double a)
58{
59 long double x = 1;
60 long double lx = 0;
61
62 if(a<0.00000000000000001) return 0;
63
64 while(x!=lx)
65 {
66 lx=x;
67 x=(x+(a/x))/2;
68 }
69 return x;
70}
71#endif
72
73
[c01bd280]74
[e507afa]75static atomic_t threads_ok;
[c01bd280]76static waitq_t can_start;
77
[b312247]78static void e(void *data)
[6de2480e]79{
[54ca3523]80 int i;
[c01bd280]81 double e,d,le,f;
82
83 waitq_sleep(&can_start);
84
[54ca3523]85 for (i = 0; i<ATTEMPTS; i++) {
86 le=-1;
87 e=0;
88 f=1;
[c01bd280]89
[54ca3523]90 for(d=1;e!=le;d*=f,f+=1) {
91 le=e;
92 e=e+1/d;
93 }
94
95 if((int)(100000000*e)!=E_10e8)
[280a27e]96 panic("tid%d: e*10e8=%zd should be %zd\n", THREAD->tid, (__native) (100000000*e),(__native) E_10e8);
[c01bd280]97 }
[54ca3523]98
[280a27e]99 printf("tid%d: e*10e8=%zd should be %zd\n", THREAD->tid, (__native) (100000000*e),(__native) E_10e8);
[992bbb97]100 atomic_inc(&threads_ok);
[6de2480e]101}
102
[c01bd280]103static void pi(void *data)
104{
[9e1c942]105
106#ifdef __ia64_ARCH_H__
107#undef PI_10e8
108#define PI_10e8 3141592
109#endif
110
[54ca3523]111 int i;
[76cec1e]112 double lpi, pi;
113 double n, ab, ad;
[c01bd280]114
115 waitq_sleep(&can_start);
116
117
[54ca3523]118 for (i = 0; i<ATTEMPTS; i++) {
119 lpi = -1;
120 pi = 0;
121
[76cec1e]122 for (n=2, ab = sqrt(2); lpi != pi; n *= 2, ab = ad) {
123 double sc, cd;
[c01bd280]124
[76cec1e]125 sc = sqrt(1 - (ab*ab/4));
126 cd = 1 - sc;
127 ad = sqrt(ab*ab/4 + cd*cd);
128 lpi = pi;
129 pi = 2 * n * ad;
130 }
[54ca3523]131
[9e1c942]132#ifdef __ia64_ARCH_H__
133 if((int)(1000000*pi)!=PI_10e8)
[280a27e]134 panic("tid%d: pi*10e8=%zd should be %zd\n", THREAD->tid, (__native) (1000000*pi),(__native) (PI_10e8/100));
[9e1c942]135#else
[54ca3523]136 if((int)(100000000*pi)!=PI_10e8)
[280a27e]137 panic("tid%d: pi*10e8=%zd should be %zd\n", THREAD->tid, (__native) (100000000*pi),(__native) PI_10e8);
[9e1c942]138#endif
139
[c01bd280]140 }
[54ca3523]141
[280a27e]142 printf("tid%d: pi*10e8=%zd should be %zd\n", THREAD->tid, (__native) (100000000*pi),(__native) PI_10e8);
[992bbb97]143 atomic_inc(&threads_ok);
[c01bd280]144}
[6de2480e]145
146void test(void)
147{
[b312247]148 thread_t *t;
149 int i;
[6de2480e]150
[c01bd280]151 waitq_initialize(&can_start);
152
153 printf("FPU test #1\n");
154 printf("Creating %d threads... ", THREADS);
155
156 for (i=0; i<THREADS/2; i++) {
[ff14c520]157 if (!(t = thread_create(e, NULL, TASK, 0, "e")))
[c01bd280]158 panic("could not create thread\n");
159 thread_ready(t);
[ff14c520]160 if (!(t = thread_create(pi, NULL, TASK, 0, "pi")))
[c01bd280]161 panic("could not create thread\n");
[b312247]162 thread_ready(t);
163 }
[c01bd280]164 printf("ok\n");
[af22f158]165
[c01bd280]166 thread_sleep(1);
167 waitq_wakeup(&can_start, WAKEUP_ALL);
[6de2480e]168
[a3eeceb6]169 while (atomic_get(&threads_ok) != THREADS)
[c01bd280]170 ;
171
172 printf("Test passed.\n");
[6de2480e]173}
Note: See TracBrowser for help on using the repository browser.