1 /* $OpenBSD: rint.c,v 1.10 2011/04/10 17:06:44 martynas Exp $ */
2
3 /* Written by Michael Shalayeff, 2003, Public domain. */
4
5 #include <assert.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <signal.h>
10 #include <unistd.h>
11 #include <math.h>
12 #include <ieeefp.h>
13
14 static void
sigfpe(int sig,siginfo_t * si,void * v)15 sigfpe(int sig, siginfo_t *si, void *v)
16 {
17 char buf[132];
18
19 if (si) {
20 snprintf(buf, sizeof(buf), "sigfpe: addr=%p, code=%d\n",
21 si->si_addr, si->si_code);
22 write(1, buf, strlen(buf));
23 }
24 _exit(1);
25 }
26
27 int
main(int argc,char * argv[])28 main(int argc, char *argv[])
29 {
30 struct sigaction sa;
31
32 memset(&sa, 0, sizeof(sa));
33 sa.sa_sigaction = sigfpe;
34 sa.sa_flags = SA_SIGINFO;
35 sigaction(SIGFPE, &sa, NULL);
36
37 assert(rint(8.6) == 9.);
38 assert(rintf(8.6F) == 9);
39 assert(rintl(8.6L) == 9);
40 assert(lrint(8.6) == 9L);
41 assert(lrintf(8.6F) == 9L);
42 assert(llrint(8.6) == 9LL);
43 assert(llrintf(8.6F) == 9LL);
44
45 assert(lrint(0.0) == 0L);
46 assert(lrintf(0.0) == 0L);
47 assert(lrint(-0.0) == 0L);
48 assert(lrintf(-0.0) == 0L);
49
50 assert(llrint(4503599627370496.0) == 4503599627370496LL);
51 assert(llrintf(4503599627370496.0F) == 4503599627370496LL);
52 assert(llrint(-4503599627370496.0) == -4503599627370496LL);
53 assert(llrintf(-4503599627370496.0F) == -4503599627370496LL);
54
55 assert(llrint(0x7ffffffffffffc00.0p0) == 0x7ffffffffffffc00LL);
56 assert(llrintf(0x7fffff8000000000.0p0F) == 0x7fffff8000000000LL);
57 assert(llrint(-0x8000000000000000.0p0) == -0x8000000000000000LL);
58 assert(llrintf(-0x8000000000000000.0p0F) == -0x8000000000000000LL);
59
60 fpsetround(FP_RM);
61 assert(lrint(-0.1) == -1L);
62 assert(lrintf(-0.1) == -1L);
63
64 fpsetround(FP_RP);
65 assert(lrint(0.1) == 1L);
66 assert(lrintf(0.1) == 1L);
67
68 exit(0);
69 }
70