Subversion Repositories HelenOS

Rev

Rev 4055 | 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
 
3918 decky 41
#define THREADS   150
42
#define ATTEMPTS  100
91 jermar 43
 
3918 decky 44
#define E_10e8   271828182
45
#define PI_10e8  314159265
91 jermar 46
 
2027 decky 47
static inline double sqrt(double x)
48
{
49
    double v;
50
 
51
    asm (
52
        "fsqrt\n"
53
        : "=t" (v)
54
        : "0" (x)
55
    );
56
 
57
    return v;
58
}
1023 vana 59
 
483 jermar 60
static atomic_t threads_ok;
2027 decky 61
static atomic_t threads_fault;
91 jermar 62
static waitq_t can_start;
63
 
86 jermar 64
static void e(void *data)
84 vana 65
{
92 jermar 66
    int i;
2051 decky 67
    double e, d, le, f;
3918 decky 68
 
1658 vana 69
    thread_detach(THREAD);
3918 decky 70
 
91 jermar 71
    waitq_sleep(&can_start);
3918 decky 72
 
92 jermar 73
    for (i = 0; i<ATTEMPTS; i++) {
2027 decky 74
        le = -1;
75
        e = 0;
76
        f = 1;
3918 decky 77
 
2027 decky 78
        for (d = 1; e != le; d *= f, f += 1) {
79
            le = e;
80
            e = e + 1 / d;
92 jermar 81
        }
3918 decky 82
 
2027 decky 83
        if ((int) (100000000 * e) != E_10e8) {
4296 trochtova 84
            TPRINTF("tid%" PRIu64 ": e*10e8=%zd should be %" PRIun "\n", THREAD->tid, (unative_t) (100000000 * e), (unative_t) E_10e8);
2027 decky 85
            atomic_inc(&threads_fault);
86
            break;
87
        }
86 jermar 88
    }
119 jermar 89
    atomic_inc(&threads_ok);
84 vana 90
}
91
 
91 jermar 92
static void pi(void *data)
93
{
92 jermar 94
    int i;
125 jermar 95
    double lpi, pi;
96
    double n, ab, ad;
1658 vana 97
 
98
    thread_detach(THREAD);
3918 decky 99
 
91 jermar 100
    waitq_sleep(&can_start);
3918 decky 101
 
2027 decky 102
    for (i = 0; i < ATTEMPTS; i++) {
92 jermar 103
        lpi = -1;
104
        pi = 0;
3918 decky 105
 
2027 decky 106
        for (n = 2, ab = sqrt(2); lpi != pi; n *= 2, ab = ad) {
125 jermar 107
            double sc, cd;
3918 decky 108
 
2027 decky 109
            sc = sqrt(1 - (ab * ab / 4));
125 jermar 110
            cd = 1 - sc;
2027 decky 111
            ad = sqrt(ab * ab / 4 + cd * cd);
125 jermar 112
            lpi = pi;
113
            pi = 2 * n * ad;
114
        }
3918 decky 115
 
2027 decky 116
        if ((int) (100000000 * pi) != PI_10e8) {
4296 trochtova 117
            TPRINTF("tid%" PRIu64 ": pi*10e8=%zd should be %" PRIun "\n", THREAD->tid, (unative_t) (100000000 * pi), (unative_t) PI_10e8);
2027 decky 118
            atomic_inc(&threads_fault);
119
            break;
120
        }
91 jermar 121
    }
119 jermar 122
    atomic_inc(&threads_ok);
91 jermar 123
}
124
 
4296 trochtova 125
char *test_fpu1(void)
84 vana 126
{
2027 decky 127
    unsigned int i, total = 0;
3918 decky 128
 
91 jermar 129
    waitq_initialize(&can_start);
2027 decky 130
    atomic_set(&threads_ok, 0);
131
    atomic_set(&threads_fault, 0);
2051 decky 132
 
4296 trochtova 133
    TPRINTF("Creating %u threads... ", 2 * THREADS);
3918 decky 134
 
4296 trochtova 135
    for (i = 0; i < THREADS; i++) {
2027 decky 136
        thread_t *t;
137
 
2042 decky 138
        if (!(t = thread_create(e, NULL, TASK, 0, "e", false))) {
4296 trochtova 139
            TPRINTF("could not create thread %u\n", 2 * i);
2027 decky 140
            break;
141
        }
86 jermar 142
        thread_ready(t);
2027 decky 143
        total++;
144
 
2042 decky 145
        if (!(t = thread_create(pi, NULL, TASK, 0, "pi", false))) {
4296 trochtova 146
            TPRINTF("could not create thread %u\n", 2 * i + 1);
2027 decky 147
            break;
148
        }
91 jermar 149
        thread_ready(t);
2027 decky 150
        total++;
86 jermar 151
    }
90 vana 152
 
4296 trochtova 153
    TPRINTF("ok\n");
2051 decky 154
 
91 jermar 155
    thread_sleep(1);
156
    waitq_wakeup(&can_start, WAKEUP_ALL);
2027 decky 157
 
2745 decky 158
    while (atomic_get(&threads_ok) != (long) total) {
4296 trochtova 159
        TPRINTF("Threads left: %d\n", total - atomic_get(&threads_ok));
2027 decky 160
        thread_sleep(1);
161
    }
162
 
163
    if (atomic_get(&threads_fault) == 0)
164
        return NULL;
165
 
166
    return "Test failed";
84 vana 167
}