iAPX86 code for ABS (using Microsoft C?)

Michael Meissner meissner at tiktok.dg.com
Wed Jun 28 00:51:24 AEST 1989


In article <1202 at draken.nada.kth.se> d88-eli at nada.kth.se (Erik Liljencrantz) writes:
| I remember a review of Microsoft's C compiler for MS-DOS that stated it
| produced no-conditional-jump-abs-code. The function to convert an integer
| to a positive integer was performed without a conditional jump (i.e. JS or
| something). I'm very interested in this piece of code, so if someone
| who does have a Microsoft C-compiler could try something like
|   main()
|   { int a,b;
|     a=-10;
|     b=abs(a);
|   }

I don't know beans about what Microsoft does, but doing an absolute
value without doing a just is fairly easy, if you have either an
arithmetic right shift (one that propigates the sign), or an extract
bit instruction that does sign extension.

The trick is make a register temp that every bit is a copy of the sign
bit of the value you want to take the absolute value of (ie, -1 if the
number is negative, 0 if it's zero or positive).  This can be done via
shifting by shifting right N-1 bits (where N is the number of bits in
the value, typically 16 or 32).  You XOR the original value with 0/-1,
if the temp is 0, the value will be the same, if the value is -1, then
the original value is complemented.  You finish by subtracting the
temp from the result of the XOR.

For example, here is the output of the GNU 88000 compiler, which I
just recently had modified to use this method:

	file	 "abs3.c"

; Cc1 arguments:
; -mdelay-slot -fomit-frame-pointer -quiet -dumpbase -O -o

	text
	align	 4
	global	 _func
_func:
	or.u	 r13,r0,hi16(_i2)
	ld	 r2,r13,lo16(_i2)
	ext	 r3,r2,0<31>
	xor	 r2,r2,r3
	subu	 r2,r2,r3
	or.u	 r13,r0,hi16(_i1)
	st	 r2,r13,lo16(_i1)
	jmp	 r1

	data
	comm	 _i2,4
	comm	 _i1,4
--
Michael Meissner, Data General.
Uucp:		...!mcnc!rti!xyzzy!meissner		If compiles were much
Internet:	meissner at dg-rtp.DG.COM			faster, when would we
Old Internet:	meissner%dg-rtp.DG.COM at relay.cs.net	have time for netnews?



More information about the Comp.lang.c mailing list