| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Copyright (C) 2014-2016 Free Software Foundation, Inc. | 
|  | 2 | This file is part of the GNU C Library. | 
|  | 3 |  | 
|  | 4 | The GNU C Library is free software; you can redistribute it and/or | 
|  | 5 | modify it under the terms of the GNU Lesser General Public | 
|  | 6 | License as published by the Free Software Foundation; either | 
|  | 7 | version 2.1 of the License, or (at your option) any later version. | 
|  | 8 |  | 
|  | 9 | The GNU C Library is distributed in the hope that it will be useful, | 
|  | 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|  | 12 | Lesser General Public License for more details. | 
|  | 13 |  | 
|  | 14 | You should have received a copy of the GNU Lesser General Public | 
|  | 15 | License along with the GNU C Library; if not, see | 
|  | 16 | <http://www.gnu.org/licenses/>.  */ | 
|  | 17 |  | 
|  | 18 | #include <errno.h> | 
|  | 19 | #include <pthread.h> | 
|  | 20 | #include <stdio.h> | 
|  | 21 | #include <stdlib.h> | 
|  | 22 | #include <string.h> | 
|  | 23 | #include <unistd.h> | 
|  | 24 | #include <limits.h> | 
|  | 25 | #include <sys/prctl.h> | 
|  | 26 |  | 
|  | 27 | #if __mips_fpr != 0 || _MIPS_SPFPSET != 16 | 
|  | 28 | # error This test requires -mfpxx -mno-odd-spreg | 
|  | 29 | #endif | 
|  | 30 |  | 
|  | 31 | /* This test verifies that mode changes do not clobber register state | 
|  | 32 | in other threads.  */ | 
|  | 33 |  | 
|  | 34 | static volatile int finished; | 
|  | 35 | static int mode[6] = | 
|  | 36 | { | 
|  | 37 | 0, | 
|  | 38 | PR_FP_MODE_FR, | 
|  | 39 | PR_FP_MODE_FR | PR_FP_MODE_FRE, | 
|  | 40 | PR_FP_MODE_FR, | 
|  | 41 | 0, | 
|  | 42 | PR_FP_MODE_FR | PR_FP_MODE_FRE | 
|  | 43 | }; | 
|  | 44 |  | 
|  | 45 | static void * | 
|  | 46 | thread_function (void * arg __attribute__ ((unused))) | 
|  | 47 | { | 
|  | 48 | volatile int i = 0; | 
|  | 49 | volatile float f = 0.0; | 
|  | 50 | volatile double d = 0.0; | 
|  | 51 |  | 
|  | 52 | while (!finished) | 
|  | 53 | { | 
|  | 54 | if ((float) i != f || (double) i != d) | 
|  | 55 | { | 
|  | 56 | printf ("unexpected value: i(%d) f(%f) d(%f)\n", i, f, d); | 
|  | 57 | exit (1); | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | if (i == 100) | 
|  | 61 | { | 
|  | 62 | i = 0; | 
|  | 63 | f = 0.0; | 
|  | 64 | d = 0.0; | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | i++; | 
|  | 68 | f++; | 
|  | 69 | d++; | 
|  | 70 | } | 
|  | 71 | return NULL; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | int | 
|  | 75 | main (void) | 
|  | 76 | { | 
|  | 77 | int count = sysconf (_SC_NPROCESSORS_ONLN); | 
|  | 78 | if (count <= 0) | 
|  | 79 | count = 1; | 
|  | 80 | count *= 4; | 
|  | 81 |  | 
|  | 82 | pthread_t th[count]; | 
|  | 83 | int i; | 
|  | 84 | int result = 0; | 
|  | 85 |  | 
|  | 86 | for (i = 0; i < count; ++i) | 
|  | 87 | if (pthread_create (&th[i], NULL, thread_function, 0) != 0) | 
|  | 88 | { | 
|  | 89 | printf ("creation of thread %d failed\n", i); | 
|  | 90 | exit (1); | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | for (i = 0 ; i < 1000000 ; i++) | 
|  | 94 | { | 
|  | 95 | if (prctl (PR_SET_FP_MODE, mode[i % 6]) != 0 | 
|  | 96 | && errno != ENOTSUP) | 
|  | 97 | { | 
|  | 98 | printf ("prctl PR_SET_FP_MODE failed: %m\n"); | 
|  | 99 | exit (1); | 
|  | 100 | } | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | finished = 1; | 
|  | 104 |  | 
|  | 105 | for (i = 0; i < count; ++i) | 
|  | 106 | { | 
|  | 107 | void *v; | 
|  | 108 | if (pthread_join (th[i], &v) != 0) | 
|  | 109 | { | 
|  | 110 | printf ("join of thread %d failed\n", i); | 
|  | 111 | result = 1; | 
|  | 112 | } | 
|  | 113 | else if (v != NULL) | 
|  | 114 | { | 
|  | 115 | printf ("join %d successful, but child failed\n", i); | 
|  | 116 | result = 1; | 
|  | 117 | } | 
|  | 118 | else | 
|  | 119 | printf ("join %d successful\n", i); | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | return result; | 
|  | 123 | } |