Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
100 user(s) are online (56 user(s) are browsing Forums)

Members: 0
Guests: 100

more...

Headlines

 
  Register To Post  

GCC 8 - how to mix PPC asm & C code
Just popping in
Just popping in


See User information
Hello,
for a C AmigaOS4 program I'd like to add some asm code as external module and later link all together with other C objects (.o)

..Any suggestion about it? ..Any makefile example to see?

Go to top
Re: GCC 8 - how to mix PPC asm & C code
Home away from home
Home away from home


See User information
@flash
That easy:

Assembler object with "asm_func()" doing simple newlib's printf(), which we will use from C (see we do there .globl and .type for declaration):

# asm.s : asm object
        
.globl asm_func
        
.type  asm_func,%function
asm_func:
        
lis %r3,.msg@ha          #
        
la %r3,.msg@l(%r3)       # printf("aaaa");
        
bl printf                #
 
        
li %r3,0                 # exit(0);
        
bl exit                  #  
 
.msg:
        .
string "aaaa"


And C code:

// test.c : C object
#include <stdlib.h>

extern void asm_func();

int main()
{
  
asm_func();
  exit(
0);
}


Compilation and linking:

ppc-amigaos-as asm.s -o asm.o
ppc-amigaos-gcc -c test.c -o test.o
ppc-amigaos-gcc test.o asm.o -o test

Tested right now, works.

Join us to improve dopus5!
AmigaOS4 on youtube
Go to top
Re: GCC 8 - how to mix PPC asm & C code
Not too shy to talk
Not too shy to talk


See User information
The given code works but is not exactly right.
In the asm_func code, the function code should be:

asm_func:
        
lis %r3,.msg@ha          #
        
la %r3,.msg@l(%r3)       # printf("aaaa");
        
bl printf                #
    
blr


In kas1e's example, the call to the exit() function has nothing to do there. I bet the other call to exit() (in the main C function) is never called.

Go to top
Re: GCC 8 - how to mix PPC asm & C code
Not too shy to talk
Not too shy to talk


See User information
Hello

The better solution is to let gcc do the works for you
I mean: put the function to optimise in separate file say "myfunction.c" then ask gcc to compile to asm with -S option you will obtain a myfunction.s that you can edit to optimise the code
then build/link the project with this modified myfunction.s
as source

1)
gcc -c main.c
gcc -S myfunction.c
2)
edit myfunction.s to myfunction-optimized.s
3)
gcc -c myfunction-optimized.s -o myfunction.o
gcc -o myprog main.o myfunction.o

Go to top
Re: GCC 8 - how to mix PPC asm & C code
Just popping in
Just popping in


See User information
@all

I'm trying to rewrite a simple Mandebrot function from StormAsm to Gcc compatible one.
it's still untested because I got a DSI error due missing prolog and epilog instructions sequence.
In StormAsm I just inserted "prolog" and "epilog" at start and end of function and all worked very well, sadly under Gcc this syntax seems to be incompatible.

Does anyone can help me finding the right stack/register preservation instructions? T
he function is hand optimized to be compatible with powerpc from 604e to G5 and all embedded cores too.

I'm not sure also on this line of code:

lfd %f8,0(Radius) #MaxDist = Radius

So I ask you if this is correct too.

######################################################################
#            Written by Dino Papararo            15-Jan-2020
#
#  FUNCTION
#
#    MandelPPC -- perform Z = Z^n + C iteration.
#
#  SYNOPSIS
#
#    int MandelPPC (long Iterations,double Cre,double Cim)
#
#
#  This function tests if a point belongs or not at mandelbrot's set
#
#  Optimized for pipelines of PPC processors.
#
#  r3:Iterations r4:Power r5:Temp
#  f1:Cre f2:Cim f3:Zr f4:Zi f5:Tmp1/Zr2 f6:Tmp2/Zi2 f7:Dist f8:MaxDist
#######################################################################

    
.file "mandelppc.s"

    
.section ".text"
    
.align   2
    
.global  MandelPPC
    
.type    MandelPPC, @function

MandelPPC:
#start prolog

#end prolog

    
lfd    %f8,0(Radius)       #MaxDist = Radius
    
fmr    %f4,%f2             #Zi = Cim
    
mtctr  %r3                 #Load Iterations into counter
    
fmr    %f3,%f1             #Zr = Cre
MainLoop:
    
mfctr  %r5                 #Save iterations from counter into Temp
    
mtctr  %r4                 #Load Power into counter
PowerLoop:
    
fmul   %f6,%f4,%f4         #Zi2 = Zi * Zi
    
fmul   %f4,%f3,%f3         #Zi *= Zr
    
fmul   %f5,%f3,%f3         #Zr2 = Zr * Zr
    
fsub   %f3,%f5,%f6         #Zr = Zr2 - Zi2
    
fadd   %f4,%f4,%f4         #Zi += Zi
    
bdnz   PowerLoop           #if --Power != 0 goto PowerLoop
    
fadd   %f7,%f5,%f6         #Dist = Zr2 + Zi2
    
mtctr  %r5                 #Load Iterations into counter
    
fadd   %f4,%f4,%f2         #Zi += Cim
    
fadd   %f3,%f3,%f1         #Zr += Cre
    
fcmpu  cr1,%f7,%f8         #compare dist with Radius
    
bdnzf  0,MainLoop          #if maxdist > Radius or --iterations != 0 goto MainLoop
    
mfctr  %r3                 #Store Iterations from counter

#epilog

#end epilog

    
blr                        #return

    
.size MandelPPC,.-MandelPPC

    
.section ".rodata"
    
.align 3
    Radius 
4



Edited by flash on 2020/1/16 13:10:09
Go to top
Re: GCC 8 - how to mix PPC asm & C code
Just can't stay away
Just can't stay away


See User information
@corto

When using bl you need to save and restore the link register or the blr won't return to correct point in the program (kas1e gets away without because of the call to exit which never returns).

Basically you need to do:
asm_func:
    
mflr %r0                 # move lr to r0
    
stwu %r1,-16(%r1)        # allocate stack frame
    
stw %r0,20(%r1)          # save lr
    
lis %r3,.msg@ha          #
    
la %r3,.msg@l(%r3)       # printf("aaaa");
    
bl printf                #
    
lwz %r0,20(%r1)          # retrieve lr
    
addi %r1,%r1,16          # free stack frame
    
mtlr %r0                 # move r0 to lr
    
blr


Since the function call is at the end and the stack is not used you could also get away with just:
asm_func:
    
lis %r3,.msg@ha          #
    
la %r3,.msg@l(%r3)       # printf("aaaa");
    
b printf                 #

Go to top
Re: GCC 8 - how to mix PPC asm & C code
Not too shy to talk
Not too shy to talk


See User information
@salass00

Oops, of course, you're right. I focused on the method to return and missed this call to printf.
Thanks!

Go to top
Re: GCC 8 - how to mix PPC asm & C code
Just popping in
Just popping in


See User information
@kas1e

Thanks a lot, your help was useful to let me look at right way..
I have just rewrote my function in a GCC compatible way wihout need to allocate and free aby stack space or sare any particular register,
I just used only voatile registers and optimized scheduling to get some instructions cycles for free.

So I paste my results here, in case it can be useful as example to some other guy.
There were some differncies between old StormAsm and GCC (AS) assembler, ie. in constant declarations and absence of proper automatic prolog and epilog routines, among other stuff too.
It was also not simple to declare the 4.0 float constant due IEEE rapresentation..
The loop was expanded two times to gain speed and has been applied some code reorg to match best speed without nreak compatibility with all powerpc cpus.
If domeone has some suggestion to offer is welcome.
Next step is to make a Tabor specific version of FlashMandel based on SPE mah.. meanwhile I'll go with a little update.
Now it's mostly compatible with MorphOS too via OS4EMU

####################################################################
#  Written by Dino Papararo            15-Jan-2020
#
#  FUNCTION
#
#    MandelPPC -- perform Z = Z^n + C iteration.
#
#  SYNOPSIS
#
#    unsigned int MandelnPPC (long Iterations,double Cre,double Cim)
#
#
#  This function tests if a point belongs or not at Mandelbrot's set
#  Handmade optimized for PowerPC processors.#
#
#  r3:Iterations r4:Power r5:Temp
#  f1:Cre f2:Cim f3:Zr f4:Zi f5:Zr2 f6:Zi2 f7:Dist f8:MaxDist
####################################################################

    
.file "mandelnppc.s"

    
.section ".text"
    
.align   2
    
.globl   MandelnPPC
    
.type    MandelnPPC, @function

MandelnPPC:

    
lis %r5,.Radius@ha         #set high 16bits R5 as Radius and crealr lower 16bits
    
lfd %f8,.Radius@l(%r5)     #load dword from Radius into F8

    
fmr    %f4,%f2             #Zi = Cim
    
fmr    %f3,%f1             #Zr = Cre

.MainLoop:
    
mtctr  %r4                 #Load Power into counter

.PowerLoop1:
    
fmul   %f6,%f4,%f4         #Zi2 = Zi * Zi
    
fmul   %f4,%f4,%f3         #Zi *= Zr
    
fmul   %f5,%f3,%f3         #Zr2 = Zr * Zr
    
fsub   %f3,%f5,%f6         #Zr = Zr2 - Zi2
    
fadd   %f4,%f4,%f4         #Zi += Zi
    
bdnz   .PowerLoop1         #if Power > 0 goto .PowerLoop1

    
fadd   %f7,%f5,%f6         #Dist = Zr2 + Zi2
    
fcmpu  cr7,%f7,%f8         #compare dist with Radius
    
fadd   %f4,%f4,%f2         #Zi += Cim
    
bgt    cr7,.Exit           #if dist > radius goto .Exit
    
cmpwi  cr6,%r3,0           #compare Iterations with 0
    
fadd   %f3,%f3,%f1         #Zr += Cre
    
beq    cr6,.Exit           #if Iterations == 0 goto .Exit
    
addi   %r3,%r3,-1          #Iterations--

    
mtctr  %r4                 #Load Power into counter

.PowerLoop2:
    
fmul   %f6,%f4,%f4         #Zi2 = Zi * Zi
    
fmul   %f4,%f4,%f3         #Zi *= Zr
    
fmul   %f5,%f3,%f3         #Zr2 = Zr * Zr
    
fsub   %f3,%f5,%f6         #Zr = Zr2 - Zi2
    
fadd   %f4,%f4,%f4         #Zi += Zi
    
bdnz   .PowerLoop2         #if Power > 0 goto .PowerLoop2

    
fadd   %f7,%f5,%f6         #Dist = Zr2 + Zi2
    
fcmpu  cr7,%f7,%f8         #compare dist with Radius
    
fadd   %f4,%f4,%f2         #Zi += Cim
    
bgt    cr7,.Exit           #if dist > radius goto .Exit
    
cmpwi  cr6,%r3,0           #compare Iterations with 0
    
fadd   %f3,%f3,%f1         #Zr += Cre
    
beq    cr6,.Exit           #if Iterations == 0 goto .Exit
    
addi   %r3,%r3,-1          #Iterations--
    
b      .MainLoop           #goto .MainLoop

.Exit:
    
blr                        #return

    
.size MandelnPPC,.-MandelnPPC
    
.section    .rodata
    
.align 3
.Radius:
    .
long   1074790400
    
.long   0

Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2023 The XOOPS Project