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