1*3b0a9131SEnji Cooper /* $NetBSD: t_fpu.c,v 1.3 2017/01/16 16:27:43 christos Exp $ */
257718be8SEnji Cooper
357718be8SEnji Cooper /*
457718be8SEnji Cooper * Copyright (c) 2008 The NetBSD Foundation, Inc.
557718be8SEnji Cooper * All rights reserved.
657718be8SEnji Cooper *
757718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
857718be8SEnji Cooper * modification, are permitted provided that the following conditions
957718be8SEnji Cooper * are met:
1057718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
1157718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
1257718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
1357718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
1457718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
1557718be8SEnji Cooper *
1657718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1757718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1857718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1957718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2057718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2157718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2257718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2357718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2457718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2557718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2657718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
2757718be8SEnji Cooper */
2857718be8SEnji Cooper
2957718be8SEnji Cooper #include <sys/cdefs.h>
3057718be8SEnji Cooper __COPYRIGHT("@(#) Copyright (c) 2008\
3157718be8SEnji Cooper The NetBSD Foundation, inc. All rights reserved.");
32*3b0a9131SEnji Cooper __RCSID("$NetBSD: t_fpu.c,v 1.3 2017/01/16 16:27:43 christos Exp $");
3357718be8SEnji Cooper
3457718be8SEnji Cooper /*
3557718be8SEnji Cooper * This is adapted from part of csw/cstest of the MPD implementation by
3657718be8SEnji Cooper * the University of Arizona CS department (http://www.cs.arizona.edu/sr/)
3757718be8SEnji Cooper * which is in the public domain:
3857718be8SEnji Cooper *
3957718be8SEnji Cooper * "The MPD system is in the public domain and you may use and distribute it
4057718be8SEnji Cooper * as you wish. We ask that you retain credits referencing the University
4157718be8SEnji Cooper * of Arizona and that you identify any changes you make.
4257718be8SEnji Cooper *
4357718be8SEnji Cooper * We can't provide a warranty with MPD; it's up to you to determine its
4457718be8SEnji Cooper * suitability and reliability for your needs. We would like to hear of
4557718be8SEnji Cooper * any problems you encounter but we cannot promise a timely correction."
4657718be8SEnji Cooper *
4757718be8SEnji Cooper * It was changed to use pthread_create() and sched_yield() instead of
4857718be8SEnji Cooper * the internal MPD context switching primitives by Ignatios Souvatzis
4957718be8SEnji Cooper * <is@netbsd.org>.
5057718be8SEnji Cooper */
5157718be8SEnji Cooper
52*3b0a9131SEnji Cooper #include <errno.h>
5357718be8SEnji Cooper #include <math.h>
5457718be8SEnji Cooper #include <pthread.h>
5557718be8SEnji Cooper #include <sched.h>
5657718be8SEnji Cooper #include <stdio.h>
57*3b0a9131SEnji Cooper #include <string.h>
5857718be8SEnji Cooper #include <stdlib.h>
5957718be8SEnji Cooper #include <unistd.h>
6057718be8SEnji Cooper
6157718be8SEnji Cooper #include <atf-c.h>
6257718be8SEnji Cooper
6357718be8SEnji Cooper #include "h_common.h"
6457718be8SEnji Cooper
6557718be8SEnji Cooper #define N_RECURSE 10
6657718be8SEnji Cooper
6757718be8SEnji Cooper static void recurse(void);
6857718be8SEnji Cooper
6957718be8SEnji Cooper int recursion_depth = 0;
7057718be8SEnji Cooper pthread_mutex_t recursion_depth_lock;
7157718be8SEnji Cooper
7257718be8SEnji Cooper static void *
stir(void * p)7357718be8SEnji Cooper stir(void *p)
7457718be8SEnji Cooper {
7557718be8SEnji Cooper double *q = (double *)p;
7657718be8SEnji Cooper double x = *q++;
7757718be8SEnji Cooper double y = *q++;
7857718be8SEnji Cooper double z = *q++;
7957718be8SEnji Cooper
8057718be8SEnji Cooper for (;;) {
8157718be8SEnji Cooper x = sin ((y = cos (x + y + .4)) - (z = cos (x + z + .6)));
8217a0c1ebSEnji Cooper ATF_REQUIRE_MSG(sched_yield() == 0,
8317a0c1ebSEnji Cooper "sched_yield failed: %s", strerror(errno));
8457718be8SEnji Cooper }
8557718be8SEnji Cooper }
8657718be8SEnji Cooper
8757718be8SEnji Cooper static double
mul3(double x,double y,double z)8857718be8SEnji Cooper mul3(double x, double y, double z)
8957718be8SEnji Cooper {
9017a0c1ebSEnji Cooper ATF_REQUIRE_MSG(sched_yield() == 0,
9117a0c1ebSEnji Cooper "sched_yield failed: %s", strerror(errno));
9257718be8SEnji Cooper
9357718be8SEnji Cooper return x * y * z;
9457718be8SEnji Cooper }
9557718be8SEnji Cooper
9657718be8SEnji Cooper static void *
bar(void * p)9757718be8SEnji Cooper bar(void *p)
9857718be8SEnji Cooper {
9957718be8SEnji Cooper double d;
10057718be8SEnji Cooper int rc;
10157718be8SEnji Cooper
10257718be8SEnji Cooper d = mul3(mul3(2., 3., 5.), mul3(7., 11., 13.), mul3(17., 19., 23.));
10357718be8SEnji Cooper ATF_REQUIRE_EQ(d, 223092870.);
10457718be8SEnji Cooper
10557718be8SEnji Cooper PTHREAD_REQUIRE(pthread_mutex_lock(&recursion_depth_lock));
10657718be8SEnji Cooper rc = recursion_depth++;
10757718be8SEnji Cooper PTHREAD_REQUIRE(pthread_mutex_unlock(&recursion_depth_lock));
10857718be8SEnji Cooper
10957718be8SEnji Cooper if (rc < N_RECURSE)
11057718be8SEnji Cooper recurse();
11157718be8SEnji Cooper else
11257718be8SEnji Cooper atf_tc_pass();
11357718be8SEnji Cooper
11457718be8SEnji Cooper /* NOTREACHED */
11557718be8SEnji Cooper return NULL;
11657718be8SEnji Cooper }
11757718be8SEnji Cooper
11857718be8SEnji Cooper static void
recurse(void)11957718be8SEnji Cooper recurse(void) {
12057718be8SEnji Cooper pthread_t s2;
12117a0c1ebSEnji Cooper PTHREAD_REQUIRE(pthread_create(&s2, 0, bar, 0));
12257718be8SEnji Cooper sleep(20); /* XXX must be long enough for our slowest machine */
12357718be8SEnji Cooper }
12457718be8SEnji Cooper
12557718be8SEnji Cooper ATF_TC(fpu);
ATF_TC_HEAD(fpu,tc)12657718be8SEnji Cooper ATF_TC_HEAD(fpu, tc)
12757718be8SEnji Cooper {
12857718be8SEnji Cooper atf_tc_set_md_var(tc, "descr",
12957718be8SEnji Cooper "Checks that thread context switches will leave the "
13057718be8SEnji Cooper "floating point computations unharmed");
13157718be8SEnji Cooper }
ATF_TC_BODY(fpu,tc)13257718be8SEnji Cooper ATF_TC_BODY(fpu, tc)
13357718be8SEnji Cooper {
13457718be8SEnji Cooper double stirseed[] = { 1.7, 3.2, 2.4 };
13557718be8SEnji Cooper pthread_t s5;
13657718be8SEnji Cooper
13757718be8SEnji Cooper printf("Testing threaded floating point computations...\n");
13857718be8SEnji Cooper
13957718be8SEnji Cooper PTHREAD_REQUIRE(pthread_mutex_init(&recursion_depth_lock, 0));
14057718be8SEnji Cooper
14117a0c1ebSEnji Cooper PTHREAD_REQUIRE(pthread_create(&s5, 0, stir, stirseed));
14257718be8SEnji Cooper recurse();
14357718be8SEnji Cooper
14457718be8SEnji Cooper atf_tc_fail("exiting from main");
14557718be8SEnji Cooper }
14657718be8SEnji Cooper
ATF_TP_ADD_TCS(tp)14757718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
14857718be8SEnji Cooper {
14957718be8SEnji Cooper ATF_TP_ADD_TC(tp, fpu);
15057718be8SEnji Cooper
15157718be8SEnji Cooper return atf_no_error();
15257718be8SEnji Cooper }
153