xref: /openbsd-src/regress/sys/kern/signal/fpsig/fpsig.c (revision 5c33ecfc8b074dc221a195406b4caaf4aa7d7fef)
1*5c33ecfcSmiod /*	$OpenBSD: fpsig.c,v 1.4 2022/10/28 16:06:54 miod Exp $	*/
222807561Sotto 
322807561Sotto /*
422807561Sotto  * Public domain.  2005, Otto Moerbeek
522807561Sotto  *
622807561Sotto  * Try to check if fp registers are properly saved and restored while
722807561Sotto  * calling a signal hander.  This is not supposed to catch all that
8ca8ee3dfSotto  * can go wrong, but trashed fp registers will typically get caught.
922807561Sotto  */
1022807561Sotto 
1122807561Sotto #include <err.h>
1222807561Sotto #include <signal.h>
1322807561Sotto #include <unistd.h>
1480ef4626Smiod #include <sys/time.h>
1522807561Sotto 
1680ef4626Smiod #define LIMIT	11.1
1722807561Sotto 
1822807561Sotto volatile sig_atomic_t count;
1922807561Sotto 
20ca8ee3dfSotto volatile double g1;
21ca8ee3dfSotto volatile double g2;
2222807561Sotto 
2322807561Sotto void
handler(int signo)2422807561Sotto handler(int signo)
2522807561Sotto {
2622807561Sotto 	double a, b, c = 0.0;
2722807561Sotto 
2880ef4626Smiod 	for (a = 0.0; a < LIMIT; a += 1.1)
2980ef4626Smiod 		for (b = 0.0; b < LIMIT; b += 1.1)
3022807561Sotto 			c += a * a + b * b;
3122807561Sotto 
3222807561Sotto 	if (signo) {
3322807561Sotto 		g1 = c;
3422807561Sotto 		count++;
3522807561Sotto 	} else
3622807561Sotto 		g2 = c;
3722807561Sotto }
3822807561Sotto 
3922807561Sotto int
main()4022807561Sotto main()
4122807561Sotto {
4280ef4626Smiod 	struct itimerval it = {
4380ef4626Smiod 		.it_interval =  { .tv_sec = 0, .tv_usec = 10000 },
4480ef4626Smiod 		.it_value =  { .tv_sec = 0, .tv_usec = 10000 }
4580ef4626Smiod 	};
4680ef4626Smiod 
4722807561Sotto 	/* initialize global vars */
4822807561Sotto 	handler(0);
4922807561Sotto 	handler(1);
5022807561Sotto 
5180ef4626Smiod 	signal(SIGALRM, handler);
5280ef4626Smiod 	setitimer(ITIMER_REAL, &it, NULL);
5380ef4626Smiod 
5480ef4626Smiod 	while (count < 10000) {
5522807561Sotto 		handler(0);
56*5c33ecfcSmiod 
5780ef4626Smiod 		double a, b, h1 = g1, h2 = g2;
5880ef4626Smiod 
5980ef4626Smiod 		for (a = 0.0; a < LIMIT; a += 1.1)
6080ef4626Smiod 			for (b = 0.0; b < LIMIT; b += 1.1)
6180ef4626Smiod 				h1 += a * a + b * b;
6280ef4626Smiod 		for (a = 0.0; a < LIMIT; a += 1.1)
6380ef4626Smiod 			for (b = 0.0; b < LIMIT; b += 1.1)
6480ef4626Smiod 				h2 += a * a + b * b;
6580ef4626Smiod 
6680ef4626Smiod 		if (h1 != h2)
6722807561Sotto 			errx(1, "%f %f", g1, g2);
6822807561Sotto 	}
6922807561Sotto 	return (0);
7022807561Sotto }
71