multi-precision arithmetics and overflow

Richard A. O'Keefe quintus!ok at sun.com
Wed Nov 23 07:56:48 AEST 1988


pom%and.s1.gov at 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 at +,d0
	addl	sp at +,d1
	trapv
	.end
	.inline	_isub,8
	movl	sp at +,d1
	movl	sp at +,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.



More information about the Comp.sys.sun mailing list