Subversion Repositories HelenOS

Rev

Rev 3069 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
86 jermar 1
/*
2071 jermar 2
 * Copyright (c) 2005 Jakub Vana
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
 
2027 decky 30
#if (defined(ia32) || defined(amd64) || defined(ia64) || defined(ia32xen))
31
 
84 vana 32
#include <print.h>
33
#include <debug.h>
34
 
35
#include <test.h>
1104 jermar 36
#include <atomic.h>
84 vana 37
#include <proc/thread.h>
38
 
94 jermar 39
#include <arch.h>
1023 vana 40
#include <arch/arch.h>
94 jermar 41
 
2020 decky 42
 
2027 decky 43
#define THREADS     150
1053 vana 44
#define ATTEMPTS    100
91 jermar 45
 
46
#define E_10e8  271828182
47
#define PI_10e8 314159265
48
 
1023 vana 49
 
1882 jermar 50
#ifdef KERN_ia32_ARCH_H_
2027 decky 51
static 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
}
1023 vana 63
#endif
91 jermar 64
 
1882 jermar 65
#ifdef KERN_amd64_ARCH_H_
2027 decky 66
static 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
}
1023 vana 78
#endif
79
 
1882 jermar 80
#ifdef KERN_ia64_ARCH_H_
2027 decky 81
 
82
#undef PI_10e8  
83
#define PI_10e8 3141592
84
 
1023 vana 85
static inline long double sqrt(long double a)
86
{  
87
    long double x = 1;
88
    long double lx = 0;
89
 
2027 decky 90
    if (a < 0.00000000000000001)
91
        return 0;
1023 vana 92
 
2027 decky 93
    while(x != lx) {
94
        lx = x;
95
        x = (x + (a / x)) / 2;
1023 vana 96
    }
2027 decky 97
 
1023 vana 98
    return x;
99
}
100
#endif
101
 
102
 
483 jermar 103
static atomic_t threads_ok;
2027 decky 104
static atomic_t threads_fault;
91 jermar 105
static waitq_t can_start;
2051 decky 106
static bool sh_quiet;
91 jermar 107
 
86 jermar 108
static void e(void *data)
84 vana 109
{
92 jermar 110
    int i;
2051 decky 111
    double e, d, le, f;
91 jermar 112
 
1658 vana 113
    thread_detach(THREAD);
114
 
91 jermar 115
    waitq_sleep(&can_start);
116
 
92 jermar 117
    for (i = 0; i<ATTEMPTS; i++) {
2027 decky 118
        le = -1;
119
        e = 0;
120
        f = 1;
92 jermar 121
 
2027 decky 122
        for (d = 1; e != le; d *= f, f += 1) {
123
            le = e;
124
            e = e + 1 / d;
92 jermar 125
        }
126
 
2027 decky 127
        if ((int) (100000000 * e) != E_10e8) {
2051 decky 128
            if (!sh_quiet)
3069 decky 129
                printf("tid%" PRIu64 ": e*10e8=%zd should be %" PRIun "\n", THREAD->tid, (unative_t) (100000000 * e), (unative_t) E_10e8);
2027 decky 130
            atomic_inc(&threads_fault);
131
            break;
132
        }
86 jermar 133
    }
119 jermar 134
    atomic_inc(&threads_ok);
84 vana 135
}
136
 
91 jermar 137
static void pi(void *data)
138
{
92 jermar 139
    int i;
125 jermar 140
    double lpi, pi;
141
    double n, ab, ad;
1658 vana 142
 
143
    thread_detach(THREAD);
84 vana 144
 
91 jermar 145
    waitq_sleep(&can_start);
84 vana 146
 
2027 decky 147
    for (i = 0; i < ATTEMPTS; i++) {
92 jermar 148
        lpi = -1;
149
        pi = 0;
91 jermar 150
 
2027 decky 151
        for (n = 2, ab = sqrt(2); lpi != pi; n *= 2, ab = ad) {
125 jermar 152
            double sc, cd;
92 jermar 153
 
2027 decky 154
            sc = sqrt(1 - (ab * ab / 4));
125 jermar 155
            cd = 1 - sc;
2027 decky 156
            ad = sqrt(ab * ab / 4 + cd * cd);
125 jermar 157
            lpi = pi;
158
            pi = 2 * n * ad;
159
        }
92 jermar 160
 
1882 jermar 161
#ifdef KERN_ia64_ARCH_H_
2027 decky 162
        if ((int) (1000000 * pi) != PI_10e8) {
2051 decky 163
            if (!sh_quiet)
3069 decky 164
                printf("tid%" PRIu64 ": pi*10e8=%zd should be %" PRIun "\n", THREAD->tid, (unative_t) (1000000 * pi), (unative_t) (PI_10e8 / 100));
2027 decky 165
            atomic_inc(&threads_fault);
166
            break;
167
        }
1023 vana 168
#else
2027 decky 169
        if ((int) (100000000 * pi) != PI_10e8) {
2051 decky 170
            if (!sh_quiet)
3069 decky 171
                printf("tid%" PRIu64 ": pi*10e8=%zd should be %" PRIun "\n", THREAD->tid, (unative_t) (100000000 * pi), (unative_t) PI_10e8);
2027 decky 172
            atomic_inc(&threads_fault);
173
            break;
174
        }
1023 vana 175
#endif
91 jermar 176
    }
119 jermar 177
    atomic_inc(&threads_ok);
91 jermar 178
}
179
 
2050 decky 180
char * test_fpu1(bool quiet)
84 vana 181
{
2027 decky 182
    unsigned int i, total = 0;
2051 decky 183
    sh_quiet = quiet;
84 vana 184
 
91 jermar 185
    waitq_initialize(&can_start);
2027 decky 186
    atomic_set(&threads_ok, 0);
187
    atomic_set(&threads_fault, 0);
2051 decky 188
 
189
    if (!quiet)
3069 decky 190
        printf("Creating %u threads... ", 2 * THREADS);
91 jermar 191
 
2027 decky 192
    for (i = 0; i < THREADS; i++) {  
193
        thread_t *t;
194
 
2042 decky 195
        if (!(t = thread_create(e, NULL, TASK, 0, "e", false))) {
2051 decky 196
            if (!quiet)
3069 decky 197
                printf("could not create thread %u\n", 2 * i);
2027 decky 198
            break;
199
        }
86 jermar 200
        thread_ready(t);
2027 decky 201
        total++;
202
 
2042 decky 203
        if (!(t = thread_create(pi, NULL, TASK, 0, "pi", false))) {
2051 decky 204
            if (!quiet)
3069 decky 205
                printf("could not create thread %u\n", 2 * i + 1);
2027 decky 206
            break;
207
        }
91 jermar 208
        thread_ready(t);
2027 decky 209
        total++;
86 jermar 210
    }
90 vana 211
 
2051 decky 212
    if (!quiet)
213
        printf("ok\n");
214
 
91 jermar 215
    thread_sleep(1);
216
    waitq_wakeup(&can_start, WAKEUP_ALL);
2027 decky 217
 
2745 decky 218
    while (atomic_get(&threads_ok) != (long) total) {
2051 decky 219
        if (!quiet)
220
            printf("Threads left: %d\n", total - atomic_get(&threads_ok));
2027 decky 221
        thread_sleep(1);
222
    }
223
 
224
    if (atomic_get(&threads_fault) == 0)
225
        return NULL;
226
 
227
    return "Test failed";
84 vana 228
}
2020 decky 229
 
230
#endif