1 1.3 christos /* $NetBSD: t_fpu.c,v 1.3 2017/01/16 16:27:43 christos Exp $ */ 2 1.1 jmmv 3 1.1 jmmv /* 4 1.1 jmmv * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 1.1 jmmv * All rights reserved. 6 1.1 jmmv * 7 1.1 jmmv * Redistribution and use in source and binary forms, with or without 8 1.1 jmmv * modification, are permitted provided that the following conditions 9 1.1 jmmv * are met: 10 1.1 jmmv * 1. Redistributions of source code must retain the above copyright 11 1.1 jmmv * notice, this list of conditions and the following disclaimer. 12 1.1 jmmv * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jmmv * notice, this list of conditions and the following disclaimer in the 14 1.1 jmmv * documentation and/or other materials provided with the distribution. 15 1.1 jmmv * 16 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 jmmv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 jmmv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 jmmv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 jmmv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 jmmv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 jmmv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 jmmv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 jmmv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 jmmv * POSSIBILITY OF SUCH DAMAGE. 27 1.1 jmmv */ 28 1.1 jmmv 29 1.1 jmmv #include <sys/cdefs.h> 30 1.1 jmmv __COPYRIGHT("@(#) Copyright (c) 2008\ 31 1.1 jmmv The NetBSD Foundation, inc. All rights reserved."); 32 1.3 christos __RCSID("$NetBSD: t_fpu.c,v 1.3 2017/01/16 16:27:43 christos Exp $"); 33 1.1 jmmv 34 1.1 jmmv /* 35 1.1 jmmv * This is adapted from part of csw/cstest of the MPD implementation by 36 1.1 jmmv * the University of Arizona CS department (http://www.cs.arizona.edu/sr/) 37 1.1 jmmv * which is in the public domain: 38 1.1 jmmv * 39 1.1 jmmv * "The MPD system is in the public domain and you may use and distribute it 40 1.1 jmmv * as you wish. We ask that you retain credits referencing the University 41 1.1 jmmv * of Arizona and that you identify any changes you make. 42 1.1 jmmv * 43 1.1 jmmv * We can't provide a warranty with MPD; it's up to you to determine its 44 1.1 jmmv * suitability and reliability for your needs. We would like to hear of 45 1.1 jmmv * any problems you encounter but we cannot promise a timely correction." 46 1.1 jmmv * 47 1.1 jmmv * It was changed to use pthread_create() and sched_yield() instead of 48 1.1 jmmv * the internal MPD context switching primitives by Ignatios Souvatzis 49 1.1 jmmv * <is (at) netbsd.org>. 50 1.1 jmmv */ 51 1.1 jmmv 52 1.3 christos #include <errno.h> 53 1.1 jmmv #include <math.h> 54 1.1 jmmv #include <pthread.h> 55 1.1 jmmv #include <sched.h> 56 1.1 jmmv #include <stdio.h> 57 1.3 christos #include <string.h> 58 1.1 jmmv #include <stdlib.h> 59 1.1 jmmv #include <unistd.h> 60 1.1 jmmv 61 1.1 jmmv #include <atf-c.h> 62 1.1 jmmv 63 1.1 jmmv #include "h_common.h" 64 1.1 jmmv 65 1.1 jmmv #define N_RECURSE 10 66 1.1 jmmv 67 1.1 jmmv static void recurse(void); 68 1.1 jmmv 69 1.1 jmmv int recursion_depth = 0; 70 1.1 jmmv pthread_mutex_t recursion_depth_lock; 71 1.1 jmmv 72 1.1 jmmv static void * 73 1.1 jmmv stir(void *p) 74 1.1 jmmv { 75 1.1 jmmv double *q = (double *)p; 76 1.1 jmmv double x = *q++; 77 1.1 jmmv double y = *q++; 78 1.1 jmmv double z = *q++; 79 1.1 jmmv 80 1.1 jmmv for (;;) { 81 1.1 jmmv x = sin ((y = cos (x + y + .4)) - (z = cos (x + z + .6))); 82 1.3 christos ATF_REQUIRE_MSG(sched_yield() == 0, 83 1.3 christos "sched_yield failed: %s", strerror(errno)); 84 1.1 jmmv } 85 1.1 jmmv } 86 1.1 jmmv 87 1.1 jmmv static double 88 1.1 jmmv mul3(double x, double y, double z) 89 1.1 jmmv { 90 1.3 christos ATF_REQUIRE_MSG(sched_yield() == 0, 91 1.3 christos "sched_yield failed: %s", strerror(errno)); 92 1.1 jmmv 93 1.1 jmmv return x * y * z; 94 1.1 jmmv } 95 1.1 jmmv 96 1.1 jmmv static void * 97 1.1 jmmv bar(void *p) 98 1.1 jmmv { 99 1.1 jmmv double d; 100 1.1 jmmv int rc; 101 1.1 jmmv 102 1.1 jmmv d = mul3(mul3(2., 3., 5.), mul3(7., 11., 13.), mul3(17., 19., 23.)); 103 1.1 jmmv ATF_REQUIRE_EQ(d, 223092870.); 104 1.1 jmmv 105 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_lock(&recursion_depth_lock)); 106 1.1 jmmv rc = recursion_depth++; 107 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&recursion_depth_lock)); 108 1.1 jmmv 109 1.1 jmmv if (rc < N_RECURSE) 110 1.1 jmmv recurse(); 111 1.1 jmmv else 112 1.1 jmmv atf_tc_pass(); 113 1.1 jmmv 114 1.1 jmmv /* NOTREACHED */ 115 1.1 jmmv return NULL; 116 1.1 jmmv } 117 1.1 jmmv 118 1.1 jmmv static void 119 1.1 jmmv recurse(void) { 120 1.1 jmmv pthread_t s2; 121 1.3 christos PTHREAD_REQUIRE(pthread_create(&s2, 0, bar, 0)); 122 1.1 jmmv sleep(20); /* XXX must be long enough for our slowest machine */ 123 1.1 jmmv } 124 1.1 jmmv 125 1.1 jmmv ATF_TC(fpu); 126 1.1 jmmv ATF_TC_HEAD(fpu, tc) 127 1.1 jmmv { 128 1.1 jmmv atf_tc_set_md_var(tc, "descr", 129 1.1 jmmv "Checks that thread context switches will leave the " 130 1.1 jmmv "floating point computations unharmed"); 131 1.1 jmmv } 132 1.1 jmmv ATF_TC_BODY(fpu, tc) 133 1.1 jmmv { 134 1.1 jmmv double stirseed[] = { 1.7, 3.2, 2.4 }; 135 1.1 jmmv pthread_t s5; 136 1.1 jmmv 137 1.1 jmmv printf("Testing threaded floating point computations...\n"); 138 1.1 jmmv 139 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_init(&recursion_depth_lock, 0)); 140 1.1 jmmv 141 1.3 christos PTHREAD_REQUIRE(pthread_create(&s5, 0, stir, stirseed)); 142 1.1 jmmv recurse(); 143 1.1 jmmv 144 1.2 mbalmer atf_tc_fail("exiting from main"); 145 1.1 jmmv } 146 1.1 jmmv 147 1.1 jmmv ATF_TP_ADD_TCS(tp) 148 1.1 jmmv { 149 1.1 jmmv ATF_TP_ADD_TC(tp, fpu); 150 1.1 jmmv 151 1.1 jmmv return atf_no_error(); 152 1.1 jmmv } 153