Path: utzoo!attcan!uunet!ncrlnk!ncr-sd!hp-sdd!hplabs!ucbvax!husc6!rice!sun-spots-request From: quintus!ok@sun.com (Richard A. O'Keefe) Newsgroups: comp.sys.sun Subject: Re: multi-precision arithmetics and overflow Message-ID: <666@quintus.UUCP> Date: 22 Nov 88 21:56:48 GMT Sender: usenet@rice.edu Reply-To: Sun-Spots@Rice.edu Organization: Rice University, Houston, Texas Lines: 49 Approved: Sun-Spots@rice.edu Original-Date: 11 Nov 88 09:52:51 GMT X-Sun-Spots-Digest: Volume 7, Issue 22, message 3 of 14 X-Issue-Reference: v7n11 pom%and.s1.gov@mordor.s1.gov asks >1) How can I call from c the multi-precision arithmetic library such as >used e.g. in the dc program it may or may not be the same one, but see man 3 mp man -k is a good way of looking for things, man -k precision or man -k arithmetic would have got you there. >2) How can I trap overflows (in c.programs) ? >Is either of these two different when Fppa is used? You really must read the Sun guide to floating point. BEWARE! With -fsoft, or -fswitch on a machine without FP hardware, you _never_ get signals (that's the way IEEE 754 says it's supposed to be). With one of the old accelerators, you tampered with SIGFPE at your peril. SunOS 4.0 has *completely* revised this stuff. If you want to trap *integer* overflow, it isn't enormously hard. Use the "inline" facility (described in the floating-point guide, also mentioned in the "cc" manual page), e.g. cat >ovops.il <<'EOF' .inline _iadd,8 movl sp@+,d0 addl sp@+,d1 trapv .end .inline _isub,8 movl sp@+,d1 movl sp@+,d0 subl d1,d0 trapv .end | and so on EOF and then you write x = iadd(a, b); /* instead of x = a+b */ x = isub(a, b); /* instead of x = a-b */ in your C program foo.c, and compile it with cc foo.c ovops.il and away you go, getting sig=SIGFPE, code=FPE_TRAPV_TRAP on overflow. With a little imagination, you could generate SIGEMT or SIGILL instead. You'll have to write a separate .il file for Sun-2s, Sun-3s, Sun-4s, and Sun-386is (the lmult subroutine used for integer multiplication on Sun-2s apparently does not set the overflow flag) but the C code could be the same on all the machines.