Subversion Repositories HelenOS

Rev

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