[b312247] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2005 Jakub Vana
|
---|
| 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 |
|
---|
| 30 | #include <print.h>
|
---|
| 31 | #include <debug.h>
|
---|
| 32 |
|
---|
| 33 | #include <test.h>
|
---|
[23684b7] | 34 | #include <atomic.h>
|
---|
[6de2480e] | 35 | #include <proc/thread.h>
|
---|
| 36 |
|
---|
[32a89bf] | 37 | #include <arch.h>
|
---|
[9e1c942] | 38 | #include <arch/arch.h>
|
---|
[32a89bf] | 39 |
|
---|
[50661ab] | 40 |
|
---|
[e45a3b9] | 41 | #define THREADS 150
|
---|
| 42 | #define ATTEMPTS 100
|
---|
[c01bd280] | 43 |
|
---|
[e45a3b9] | 44 | #define E_10e8 271828182
|
---|
| 45 | #define PI_10e8 314159265
|
---|
[c01bd280] | 46 |
|
---|
[34db7fa] | 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 | }
|
---|
[9e1c942] | 59 |
|
---|
[e507afa] | 60 | static atomic_t threads_ok;
|
---|
[34db7fa] | 61 | static atomic_t threads_fault;
|
---|
[c01bd280] | 62 | static waitq_t can_start;
|
---|
[c8410ec9] | 63 | static bool sh_quiet;
|
---|
[c01bd280] | 64 |
|
---|
[b312247] | 65 | static void e(void *data)
|
---|
[6de2480e] | 66 | {
|
---|
[54ca3523] | 67 | int i;
|
---|
[c8410ec9] | 68 | double e, d, le, f;
|
---|
[e45a3b9] | 69 |
|
---|
[8d6d76a] | 70 | thread_detach(THREAD);
|
---|
[e45a3b9] | 71 |
|
---|
[c01bd280] | 72 | waitq_sleep(&can_start);
|
---|
[e45a3b9] | 73 |
|
---|
[54ca3523] | 74 | for (i = 0; i<ATTEMPTS; i++) {
|
---|
[34db7fa] | 75 | le = -1;
|
---|
| 76 | e = 0;
|
---|
| 77 | f = 1;
|
---|
[e45a3b9] | 78 |
|
---|
[34db7fa] | 79 | for (d = 1; e != le; d *= f, f += 1) {
|
---|
| 80 | le = e;
|
---|
| 81 | e = e + 1 / d;
|
---|
[54ca3523] | 82 | }
|
---|
[e45a3b9] | 83 |
|
---|
[34db7fa] | 84 | if ((int) (100000000 * e) != E_10e8) {
|
---|
[c8410ec9] | 85 | if (!sh_quiet)
|
---|
[cd8ad52] | 86 | printf("tid%" PRIu64 ": e*10e8=%zd should be %" PRIun "\n", THREAD->tid, (unative_t) (100000000 * e), (unative_t) E_10e8);
|
---|
[34db7fa] | 87 | atomic_inc(&threads_fault);
|
---|
| 88 | break;
|
---|
| 89 | }
|
---|
[c01bd280] | 90 | }
|
---|
[992bbb97] | 91 | atomic_inc(&threads_ok);
|
---|
[6de2480e] | 92 | }
|
---|
| 93 |
|
---|
[c01bd280] | 94 | static void pi(void *data)
|
---|
| 95 | {
|
---|
[54ca3523] | 96 | int i;
|
---|
[76cec1e] | 97 | double lpi, pi;
|
---|
| 98 | double n, ab, ad;
|
---|
[8d6d76a] | 99 |
|
---|
| 100 | thread_detach(THREAD);
|
---|
[e45a3b9] | 101 |
|
---|
[c01bd280] | 102 | waitq_sleep(&can_start);
|
---|
[e45a3b9] | 103 |
|
---|
[34db7fa] | 104 | for (i = 0; i < ATTEMPTS; i++) {
|
---|
[54ca3523] | 105 | lpi = -1;
|
---|
| 106 | pi = 0;
|
---|
[e45a3b9] | 107 |
|
---|
[34db7fa] | 108 | for (n = 2, ab = sqrt(2); lpi != pi; n *= 2, ab = ad) {
|
---|
[76cec1e] | 109 | double sc, cd;
|
---|
[e45a3b9] | 110 |
|
---|
[34db7fa] | 111 | sc = sqrt(1 - (ab * ab / 4));
|
---|
[76cec1e] | 112 | cd = 1 - sc;
|
---|
[34db7fa] | 113 | ad = sqrt(ab * ab / 4 + cd * cd);
|
---|
[76cec1e] | 114 | lpi = pi;
|
---|
| 115 | pi = 2 * n * ad;
|
---|
| 116 | }
|
---|
[e45a3b9] | 117 |
|
---|
[34db7fa] | 118 | if ((int) (100000000 * pi) != PI_10e8) {
|
---|
[c8410ec9] | 119 | if (!sh_quiet)
|
---|
[cd8ad52] | 120 | printf("tid%" PRIu64 ": pi*10e8=%zd should be %" PRIun "\n", THREAD->tid, (unative_t) (100000000 * pi), (unative_t) PI_10e8);
|
---|
[34db7fa] | 121 | atomic_inc(&threads_fault);
|
---|
| 122 | break;
|
---|
| 123 | }
|
---|
[c01bd280] | 124 | }
|
---|
[992bbb97] | 125 | atomic_inc(&threads_ok);
|
---|
[c01bd280] | 126 | }
|
---|
[6de2480e] | 127 |
|
---|
[95155b0c] | 128 | char * test_fpu1(bool quiet)
|
---|
[6de2480e] | 129 | {
|
---|
[34db7fa] | 130 | unsigned int i, total = 0;
|
---|
[c8410ec9] | 131 | sh_quiet = quiet;
|
---|
[e45a3b9] | 132 |
|
---|
[c01bd280] | 133 | waitq_initialize(&can_start);
|
---|
[34db7fa] | 134 | atomic_set(&threads_ok, 0);
|
---|
| 135 | atomic_set(&threads_fault, 0);
|
---|
[c8410ec9] | 136 |
|
---|
| 137 | if (!quiet)
|
---|
[cd8ad52] | 138 | printf("Creating %u threads... ", 2 * THREADS);
|
---|
[e45a3b9] | 139 |
|
---|
[34db7fa] | 140 | for (i = 0; i < THREADS; i++) {
|
---|
| 141 | thread_t *t;
|
---|
| 142 |
|
---|
[62b6d17] | 143 | if (!(t = thread_create(e, NULL, TASK, 0, "e", false))) {
|
---|
[c8410ec9] | 144 | if (!quiet)
|
---|
[cd8ad52] | 145 | printf("could not create thread %u\n", 2 * i);
|
---|
[34db7fa] | 146 | break;
|
---|
| 147 | }
|
---|
[c01bd280] | 148 | thread_ready(t);
|
---|
[34db7fa] | 149 | total++;
|
---|
| 150 |
|
---|
[62b6d17] | 151 | if (!(t = thread_create(pi, NULL, TASK, 0, "pi", false))) {
|
---|
[c8410ec9] | 152 | if (!quiet)
|
---|
[cd8ad52] | 153 | printf("could not create thread %u\n", 2 * i + 1);
|
---|
[34db7fa] | 154 | break;
|
---|
| 155 | }
|
---|
[b312247] | 156 | thread_ready(t);
|
---|
[34db7fa] | 157 | total++;
|
---|
[b312247] | 158 | }
|
---|
[c8410ec9] | 159 |
|
---|
| 160 | if (!quiet)
|
---|
| 161 | printf("ok\n");
|
---|
[af22f158] | 162 |
|
---|
[c01bd280] | 163 | thread_sleep(1);
|
---|
| 164 | waitq_wakeup(&can_start, WAKEUP_ALL);
|
---|
[34db7fa] | 165 |
|
---|
[6c441cf8] | 166 | while (atomic_get(&threads_ok) != (long) total) {
|
---|
[c8410ec9] | 167 | if (!quiet)
|
---|
| 168 | printf("Threads left: %d\n", total - atomic_get(&threads_ok));
|
---|
[34db7fa] | 169 | thread_sleep(1);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | if (atomic_get(&threads_fault) == 0)
|
---|
| 173 | return NULL;
|
---|
| 174 |
|
---|
| 175 | return "Test failed";
|
---|
[6de2480e] | 176 | }
|
---|