flat assembler
Message board for the users of flat assembler.

Index > Projects and Ideas > idea for code generator for math functions (optimizing compi

Author
Thread Post new topic Reply to topic
tthsqe



Joined: 20 May 2009
Posts: 730
tthsqe
I had an idea to implement a quality code generator for math functions.
The user would type in a function in a HLL as
Code:
f(x,y)=x+sin(y)    

and the program would try to generate good code for computing this function.
This might help when you just need something that works and you don't want to mess around with the tricky fpu stack...

Edit:
To clarify what I mean, I will give an example from my prototype code generator. I think it does produce quality output, as you can see.
suppose the HLL code is something like: the following. ({a,b}={c,d} is a parallel copy)
Code:
f(x,y):
while x^2 + y^2 < R,
{x, y} = {x^2 - y^2 + X, 2 x y + Y}
end while
return x + y
    

then the program produces the code
Code:
f:      fld  tword[2]
        fld  tword[Y]
        fld  tword[X]
        fld  tword[R]
        fld  tword[esp+4+16*1]  ; 2nd argument
        fld  tword[esp+4+16*0]  ; 1st argument
       fxch  st4
       fstp  tword[esp-16*1]    ; spill location
.1:     fld  st3
       fmul  st0,st0
        fld  st1
       fmul  st0,st0
        fld  st0
       fadd  st0,st2
     fcomip  st0,st4
        jnb  .2
      fsubp  st1,st0
       fxch  st1
       fmul  st0,st5
      fmulp  st4,st0
        fld  tword[esp-16*1]
      faddp  st4,st0
       fadd  st0,st2
       fstp  st5
       fxch  st2
       fxch  st3
        jmp  .1
.2:    fstp  st0
       fstp  st0
       fstp  st1
       fstp  st1
       fstp  st2
      faddp  st1,st0
        ret                     ; return value on fpu stack    


the fpu stack is a real pain to optimize mechanically! Evil or Very Mad


Last edited by tthsqe on 03 Jun 2012, 16:02; edited 4 times in total
Post 30 May 2012, 06:05
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17716
Location: In your JS exploiting you and your system
revolution
I think that is a task for an HLL compiler. Write it in C (or whatever language one prefers), compile, and extract the resulting assembly code.

However don't expect it to be good code, it will be adequate.
Post 30 May 2012, 06:08
View user's profile Send private message Visit poster's website Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 730
tthsqe
hmmm..
That doesn't always work: Very Happy
What if I don't have a HLL compiler? (Or I can't get it to work)
What if the compiler places transcendental functions in some other place?
What if the program could use some simplifying assumptions about the function to produce better code than the compiler.

I was thinking it to be more of a fun exercise in basic HLL -> quality asm transformation. I admit it is probably not that useful.
Post 30 May 2012, 06:54
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 518
Location: Munich
shutdownall
Well you could create a mathlib (inc) which could be included in projects when needed and publish it here. Very Happy
Post 30 May 2012, 09:12
View user's profile Send private message Send e-mail Reply with quote
typedef



Joined: 25 Jul 2010
Posts: 2913
Location: 0x77760000
typedef
tthsqe wrote:
hmmm..
That doesn't always work: Very Happy
What if I don't have a HLL compiler? (Or I can't get it to work)
What if the compiler places transcendental functions in some other place?
What if the program could use some simplifying assumptions about the function to produce better code than the compiler.

I was thinking it to be more of a fun exercise in basic HLL -> quality asm transformation. I admit it is probably not that useful.


Use naked functions. Wink You must however make the prologue and epilogue in ASM (inline) like so.

Code:
__declspec(naked) type convention func_name(parameters...)
{
__asm push ebp
__asm mov ebp, esp

// your C code here

__asm mov esp, ebp
__asm pop ebp
ret                               // return that complies with appropriate convention used.
}
    


Your function will then start with MOV EDI,EDI. You can then re-assemble the code in FASM with probably some optimization and tweaks to suit. Good luck.
Post 30 May 2012, 09:41
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
Learn, to think assembly language. Then you will be able to freely write any function. Razz
Post 30 May 2012, 09:54
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
tthsqe



Joined: 20 May 2009
Posts: 730
tthsqe
I find thinking in assembly very error-prone when dealing with the x87 fpu - what I'm working on is a homemade optimizing compiler for HLL -> fpu to explore how compilers like gcc or icc work.
Post 03 Jun 2012, 23:12
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2140
Location: Estonia
Madis731
I think the optimizer (or the brain) should turn to FPU only for FSINCOS and such functions, because stuff like MULSD, ADDSD can be accomplished out-of-order in the SSE.
Post 26 Jun 2012, 22:35
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.