fpu code optimisation request

From: PatD (spamtrap_at_crayne.org)
Date: 07/07/04


Date: Wed, 7 Jul 2004 19:32:08 +0000 (UTC)


Consider the following pseudo-code function:

function myAND(float a, float b)
  if a = 0 then test_a = 0 else test_a = 1
  if b = 0 then test_b = 0 else test_b = 1
  result = test_a AND test_b

Or, in "normal" language, zero stays zero,
anything else becomes 1.
Compute the logical "and" of these two booleans.
(i.e. return "[0|1] and [0|1]").

All numbers are 64bit floats.

Carefull (sort of) thinking lead to the
following FPU code:

fld1 ; load 0 and 1 for later...
fldz

fld1
fldz
fld qword [someplace] ; get first value
fcomip st1 ; compare to 0 and "forget" it
fcmove st0,st1 ; st0 = 0 if [someplace] = 0, 1 otherwise
fxch ; cleanup
fstp st0

fld1
fldz
fld qword [someplace + 8] ; idem for value 2
fcomip st1
fcmove st0,st1
fxch
fstp st0

; ..."later" starts here:
fcomip st1 ; compare the 0/1 of values 1 and 2
fcmove st0,st2 ; result is either 0 or 1
fcmovne st0,st1
fxch st0,st2 ; heavy cleaning
fcompp
; final result in st0

Now, this code has two rather interesting properties:
1. it works :)
2. it has no "jmp"s

However, it sort of looks sub-optimal,
and, well, I was hoping that someone here
would see some improvement to it.
(Less code, something faster, ...)

FWIW, the final code will have to run on P3s.

Any comments and suggestions welcome

cu
P



Relevant Pages

  • Re: Interesting math
    ... precision when a number is zero. ... A C Float Number must have six or more significant digits ... and read about C Float Numbers and programming in C language. ...
    (alt.usage.english)
  • Re: is it safe to zero float array with memset?
    ... > No, but it's "safe enough". ... > that doesn't use all bits zero for float 0.0. ... If I were going to use memset() to zero an array of floats, ...
    (comp.lang.c)
  • Re: C2124 is most disrespectful of IEEE floating point arithmetic
    ... division of a non-zero by a zero to be a signed infinity, ... float inf; ... should result in a compile-time error, ... Am I being unreasonable in my expectations? ...
    (microsoft.public.vc.language)
  • RE: VFP 9: Making transform() Drop Decimal 0s
    ... Numeric (includes Double, Float, or Integer data types): ... value is less than one but greater than negative one, zero is included before ... Transform() to the worse! ...
    (microsoft.public.fox.programmer.exchange)
  • Re: is it safe to zero float array with memset?
    ... I have to initialize all elements of a very big float point array to zero. ... It seems memsetis faster than a simple loop. ... I just want to know whether it is safe to do so, since I know it's danger to initialize NULL pointers this way. ...
    (comp.lang.c)