Subversion Repositories HelenOS

Rev

Rev 1882 | Rev 2027 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
86 jermar 1
/*
2
 * Copyright (C) 2005 Jakub Vana
91 jermar 3
 * Copyright (C) 2005 Jakub Jermar
86 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
 */
84 vana 29
 
30
#include <print.h>
31
#include <debug.h>
32
#include <panic.h>
33
 
34
#include <test.h>
1104 jermar 35
#include <atomic.h>
84 vana 36
#include <proc/thread.h>
37
 
94 jermar 38
#include <arch.h>
1023 vana 39
#include <arch/arch.h>
94 jermar 40
 
2020 decky 41
#ifdef CONFIG_BENCH
42
#include <arch/cycle.h>
43
#endif
44
 
45
#if (defined(ia32) || defined(amd64) || defined(ia64) || defined(ia32xen))
46
 
1053 vana 47
#define THREADS     150*2
48
#define ATTEMPTS    100
91 jermar 49
 
50
#define E_10e8  271828182
51
#define PI_10e8 314159265
52
 
1023 vana 53
 
1882 jermar 54
#ifdef KERN_ia32_ARCH_H_
91 jermar 55
static inline double sqrt(double x) { double v; __asm__ ("fsqrt\n" : "=t" (v) : "0" (x)); return v; }
1023 vana 56
#endif
91 jermar 57
 
1882 jermar 58
#ifdef KERN_amd64_ARCH_H_
1023 vana 59
static inline double sqrt(double x) { double v; __asm__ ("fsqrt\n" : "=t" (v) : "0" (x)); return v; }
60
#endif
61
 
1882 jermar 62
#ifdef KERN_ia64_ARCH_H_
1023 vana 63
static 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
 
483 jermar 81
static atomic_t threads_ok;
91 jermar 82
static waitq_t can_start;
83
 
86 jermar 84
static void e(void *data)
84 vana 85
{
92 jermar 86
    int i;
91 jermar 87
    double e,d,le,f;
88
 
1658 vana 89
    thread_detach(THREAD);
90
 
91 jermar 91
    waitq_sleep(&can_start);
92
 
92 jermar 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)
1780 jermar 104
            panic("tid%d: e*10e8=%zd should be %zd\n", THREAD->tid, (unative_t) (100000000*e),(unative_t) E_10e8);
86 jermar 105
    }
91 jermar 106
 
1780 jermar 107
    printf("tid%d: e*10e8=%zd should be %zd\n", THREAD->tid, (unative_t) (100000000*e),(unative_t) E_10e8);
119 jermar 108
    atomic_inc(&threads_ok);
84 vana 109
}
110
 
91 jermar 111
static void pi(void *data)
112
{
1023 vana 113
 
1882 jermar 114
#ifdef KERN_ia64_ARCH_H_
1023 vana 115
#undef PI_10e8  
116
#define PI_10e8 3141592
117
#endif
118
 
1658 vana 119
 
92 jermar 120
    int i;
125 jermar 121
    double lpi, pi;
122
    double n, ab, ad;
1658 vana 123
 
124
    thread_detach(THREAD);
84 vana 125
 
91 jermar 126
    waitq_sleep(&can_start);
84 vana 127
 
91 jermar 128
 
92 jermar 129
    for (i = 0; i<ATTEMPTS; i++) {
130
        lpi = -1;
131
        pi = 0;
91 jermar 132
 
125 jermar 133
        for (n=2, ab = sqrt(2); lpi != pi; n *= 2, ab = ad) {
134
            double sc, cd;
92 jermar 135
 
125 jermar 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
        }
92 jermar 142
 
1882 jermar 143
#ifdef KERN_ia64_ARCH_H_
1023 vana 144
        if((int)(1000000*pi)!=PI_10e8)
1780 jermar 145
            panic("tid%d: pi*10e8=%zd should be %zd\n", THREAD->tid, (unative_t) (1000000*pi),(unative_t) (PI_10e8/100));
1023 vana 146
#else
92 jermar 147
        if((int)(100000000*pi)!=PI_10e8)
1780 jermar 148
            panic("tid%d: pi*10e8=%zd should be %zd\n", THREAD->tid, (unative_t) (100000000*pi),(unative_t) PI_10e8);
1023 vana 149
#endif
150
 
91 jermar 151
    }
92 jermar 152
 
1780 jermar 153
    printf("tid%d: pi*10e8=%zd should be %zd\n", THREAD->tid, (unative_t) (100000000*pi),(unative_t) PI_10e8);
119 jermar 154
    atomic_inc(&threads_ok);
91 jermar 155
}
156
 
2020 decky 157
void test_fpu1(void)
84 vana 158
{
2020 decky 159
#ifdef CONFIG_BENCH
160
    uint64_t t0 = get_cycle();
161
#endif
86 jermar 162
    thread_t *t;
163
    int i;
84 vana 164
 
91 jermar 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++) {  
1062 jermar 171
        if (!(t = thread_create(e, NULL, TASK, 0, "e")))
91 jermar 172
            panic("could not create thread\n");
86 jermar 173
        thread_ready(t);
1062 jermar 174
        if (!(t = thread_create(pi, NULL, TASK, 0, "pi")))
91 jermar 175
            panic("could not create thread\n");
176
        thread_ready(t);
86 jermar 177
    }
91 jermar 178
    printf("ok\n");
90 vana 179
 
91 jermar 180
    thread_sleep(1);
181
    waitq_wakeup(&can_start, WAKEUP_ALL);
84 vana 182
 
827 palkovsky 183
    while (atomic_get(&threads_ok) != THREADS)
91 jermar 184
        ;
185
 
186
    printf("Test passed.\n");
2020 decky 187
#ifdef CONFIG_BENCH
188
    uint64_t dt = get_cycle() - t0;
189
    printf("Time: %.*d cycles\n", sizeof(dt) * 2, dt);
190
#endif
84 vana 191
}
2020 decky 192
 
193
#else
194
 
195
void test_fpu1(void)
196
{
197
    printf("This test is available only on Intel/AMD platforms.");
198
}
199
 
200
#endif