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