| [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;
|
|---|
| 63 |
|
|---|
| [b312247] | 64 | static void e(void *data)
|
|---|
| [6de2480e] | 65 | {
|
|---|
| [54ca3523] | 66 | int i;
|
|---|
| [c8410ec9] | 67 | double e, d, le, f;
|
|---|
| [e45a3b9] | 68 |
|
|---|
| [8d6d76a] | 69 | thread_detach(THREAD);
|
|---|
| [e45a3b9] | 70 |
|
|---|
| [c01bd280] | 71 | waitq_sleep(&can_start);
|
|---|
| [e45a3b9] | 72 |
|
|---|
| [54ca3523] | 73 | for (i = 0; i<ATTEMPTS; i++) {
|
|---|
| [34db7fa] | 74 | le = -1;
|
|---|
| 75 | e = 0;
|
|---|
| 76 | f = 1;
|
|---|
| [e45a3b9] | 77 |
|
|---|
| [34db7fa] | 78 | for (d = 1; e != le; d *= f, f += 1) {
|
|---|
| 79 | le = e;
|
|---|
| 80 | e = e + 1 / d;
|
|---|
| [54ca3523] | 81 | }
|
|---|
| [e45a3b9] | 82 |
|
|---|
| [34db7fa] | 83 | if ((int) (100000000 * e) != E_10e8) {
|
|---|
| [cb01e1e] | 84 | TPRINTF("tid%" PRIu64 ": e*10e8=%zd should be %" PRIun "\n", THREAD->tid, (unative_t) (100000000 * e), (unative_t) E_10e8);
|
|---|
| [34db7fa] | 85 | atomic_inc(&threads_fault);
|
|---|
| 86 | break;
|
|---|
| 87 | }
|
|---|
| [c01bd280] | 88 | }
|
|---|
| [992bbb97] | 89 | atomic_inc(&threads_ok);
|
|---|
| [6de2480e] | 90 | }
|
|---|
| 91 |
|
|---|
| [c01bd280] | 92 | static void pi(void *data)
|
|---|
| 93 | {
|
|---|
| [54ca3523] | 94 | int i;
|
|---|
| [76cec1e] | 95 | double lpi, pi;
|
|---|
| 96 | double n, ab, ad;
|
|---|
| [8d6d76a] | 97 |
|
|---|
| 98 | thread_detach(THREAD);
|
|---|
| [e45a3b9] | 99 |
|
|---|
| [c01bd280] | 100 | waitq_sleep(&can_start);
|
|---|
| [e45a3b9] | 101 |
|
|---|
| [34db7fa] | 102 | for (i = 0; i < ATTEMPTS; i++) {
|
|---|
| [54ca3523] | 103 | lpi = -1;
|
|---|
| 104 | pi = 0;
|
|---|
| [e45a3b9] | 105 |
|
|---|
| [34db7fa] | 106 | for (n = 2, ab = sqrt(2); lpi != pi; n *= 2, ab = ad) {
|
|---|
| [76cec1e] | 107 | double sc, cd;
|
|---|
| [e45a3b9] | 108 |
|
|---|
| [34db7fa] | 109 | sc = sqrt(1 - (ab * ab / 4));
|
|---|
| [76cec1e] | 110 | cd = 1 - sc;
|
|---|
| [34db7fa] | 111 | ad = sqrt(ab * ab / 4 + cd * cd);
|
|---|
| [76cec1e] | 112 | lpi = pi;
|
|---|
| 113 | pi = 2 * n * ad;
|
|---|
| 114 | }
|
|---|
| [e45a3b9] | 115 |
|
|---|
| [34db7fa] | 116 | if ((int) (100000000 * pi) != PI_10e8) {
|
|---|
| [cb01e1e] | 117 | TPRINTF("tid%" PRIu64 ": pi*10e8=%zd should be %" PRIun "\n", THREAD->tid, (unative_t) (100000000 * pi), (unative_t) PI_10e8);
|
|---|
| [34db7fa] | 118 | atomic_inc(&threads_fault);
|
|---|
| 119 | break;
|
|---|
| 120 | }
|
|---|
| [c01bd280] | 121 | }
|
|---|
| [992bbb97] | 122 | atomic_inc(&threads_ok);
|
|---|
| [c01bd280] | 123 | }
|
|---|
| [6de2480e] | 124 |
|
|---|
| [a000878c] | 125 | const char *test_fpu1(void)
|
|---|
| [6de2480e] | 126 | {
|
|---|
| [228666c] | 127 | unsigned int i;
|
|---|
| 128 | atomic_count_t total = 0;
|
|---|
| [e45a3b9] | 129 |
|
|---|
| [c01bd280] | 130 | waitq_initialize(&can_start);
|
|---|
| [34db7fa] | 131 | atomic_set(&threads_ok, 0);
|
|---|
| 132 | atomic_set(&threads_fault, 0);
|
|---|
| [c8410ec9] | 133 |
|
|---|
| [cb01e1e] | 134 | TPRINTF("Creating %u threads... ", 2 * THREADS);
|
|---|
| [e45a3b9] | 135 |
|
|---|
| [cb01e1e] | 136 | for (i = 0; i < THREADS; i++) {
|
|---|
| [34db7fa] | 137 | thread_t *t;
|
|---|
| 138 |
|
|---|
| [62b6d17] | 139 | if (!(t = thread_create(e, NULL, TASK, 0, "e", false))) {
|
|---|
| [cb01e1e] | 140 | TPRINTF("could not create thread %u\n", 2 * i);
|
|---|
| [34db7fa] | 141 | break;
|
|---|
| 142 | }
|
|---|
| [c01bd280] | 143 | thread_ready(t);
|
|---|
| [34db7fa] | 144 | total++;
|
|---|
| 145 |
|
|---|
| [62b6d17] | 146 | if (!(t = thread_create(pi, NULL, TASK, 0, "pi", false))) {
|
|---|
| [cb01e1e] | 147 | TPRINTF("could not create thread %u\n", 2 * i + 1);
|
|---|
| [34db7fa] | 148 | break;
|
|---|
| 149 | }
|
|---|
| [b312247] | 150 | thread_ready(t);
|
|---|
| [34db7fa] | 151 | total++;
|
|---|
| [b312247] | 152 | }
|
|---|
| [c8410ec9] | 153 |
|
|---|
| [cb01e1e] | 154 | TPRINTF("ok\n");
|
|---|
| [af22f158] | 155 |
|
|---|
| [c01bd280] | 156 | thread_sleep(1);
|
|---|
| 157 | waitq_wakeup(&can_start, WAKEUP_ALL);
|
|---|
| [34db7fa] | 158 |
|
|---|
| [228666c] | 159 | while (atomic_get(&threads_ok) != total) {
|
|---|
| [cb01e1e] | 160 | TPRINTF("Threads left: %d\n", total - atomic_get(&threads_ok));
|
|---|
| [34db7fa] | 161 | thread_sleep(1);
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | if (atomic_get(&threads_fault) == 0)
|
|---|
| 165 | return NULL;
|
|---|
| 166 |
|
|---|
| 167 | return "Test failed";
|
|---|
| [6de2480e] | 168 | }
|
|---|