🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.help
Article #49538

Is there a better way than this to multiply 16.16 fixed-point on i386?

#49538
From: Ben Aaron Rudiak-Gould
Date: Tue, 04 Jan 1994 13:31
39 lines
2001 bytes
I'm writing a program in DJGPP that uses fixed-point math.  It needs to
run as quickly as possible.  Currently I'm implementing multiplication
and division with the following functions:

typedef int fixed;

static inline fixed FixMul(fixed a,fixed b)
{
        fixed result;

        asm("movl %1,%%eax;imull %2;shrd $16,%%edx,%%eax;movl %%eax,%0"
         : "=g"(result) : "g"(a),"g"(b) : "%eax","%edx");
        return result;
}

static inline fixed FixDiv(fixed a,fixed b)
{
        fixed result;

        asm("movl %1,%%eax;movl %%eax,%%edx;shll $16,%%eax;sarl $16,%%edx;"
            "idivl %2;movl %%eax,%0"
         : "=g"(result) : "g"(a),"g"(b) : "%eax","%edx");
        return result;
}

This works pretty well, but it would be nice if GCC knew that it could use
%eax as an input and/or output register.  I could eliminate "%eax" from the
list of trashed registers, but it seems like this would confuse GCC into
thinking that %eax was preserved by these functions, leading to potential
problems.  Also, when I tried this, I got instructions like "movl %eax,%eax".

I'm not very well-versed in the intricacies of the asm() statement; is there
a better way to implement these functions?

--
Ben Rudiak-Gould
benrg@uclink.berkeley.edu

"Those are my principles. If you don't like them I have others." --Groucho Marx
Technical Details
Message-ID: <2gbr49$sem@agate.berkeley.edu>
Path: arc1-ca.pugleaf.net!gmd.de!newsserver.jvnc.net!darwin.sura.net!europa.eng.gtefsd.com!library.ucla.edu!agate!uclink.berkeley.edu!benrg

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,4f1ae26683e618bc,start
X-Google-Attributes: gid11318c,public
X-Google-ArrivalTime: 1994-01-04 05:33:54 PST
Path: arc1-ca.pugleaf.net!gmd.de!newsserver.jvnc.net!darwin.sura.net!europa.eng.gtefsd.com!library.ucla.edu!agate!uclink.berkeley.edu!benrg
From: benrg@uclink.berkeley.edu (Ben Aaron Rudiak-Gould)
Newsgroups: gnu.gcc.help
Subject: Is there a better way than this to multiply 16.16 fixed-point on i386?
Date: 4 Jan 1994 13:31:53 GMT
Organization: University of California, Berkeley
Lines: 39
Message-ID: <2gbr49$sem@agate.berkeley.edu>
NNTP-Posting-Host: uclink.berkeley.edu