fast integer multiplication and one asm questio

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

fast integer multiplication and one asm questio

pianoman-3
Hi, the UI32x32To64 function is great but is amazingly short. How can I
Specify which varriables in function will be accessed. In this function for
example it accesses a and b but source says mull (I don't know why not mul I
didnt find mull instruction in any assembly book) and %eax why mull %eax
multiplies a,b is it on the stack or why? If i would like to multiply a,b,c
what would look source code then? Thanx for response to those  quite stupid
assembly question(I am only beginner in asm :)
Pianoman
----- Original Message -----
From: <[hidden email]>
To: <[hidden email]>
Sent: Tuesday, August 02, 2005 12:00 PM
Subject: fpc-pascal Digest, Vol 12, Issue 2


Send fpc-pascal mailing list submissions to
[hidden email]

To subscribe or unsubscribe via the World Wide Web, visit
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
or, via email, send a message with subject or body 'help' to
[hidden email]

You can reach the person managing the list at
[hidden email]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of fpc-pascal digest..."


Today's Topics:

   1. Re:  lazarus crash at start (Jesus Reyes)
   2. Re:  fast integer multiplication (Gerhard Scholz)
   3.  Can a program find out how it was started?
      ([hidden email])
   4. Re:  Can a program find out how it was started? (Moz)


----------------------------------------------------------------------

Message: 1
Date: Mon, 1 Aug 2005 11:21:10 -0500 (CDT)
From: Jesus Reyes <[hidden email]>
Subject: Re: [fpc-pascal] lazarus crash at start
To: FPC-Pascal users discussions <[hidden email]>
Message-ID: <[hidden email]>
Content-Type: text/plain; charset=iso-8859-1


 --- Peter Vreman <[hidden email]> escribi?:

> At 01:12 1-8-2005, you wrote:
> >Trying to find why lazarus crashes at start in win98 I found a
> courious
> >problem, global variable IsConsole is true when it should be false
> as
> >lazarus is built as a gui application.
> >
> >Digging I found that IsConsole is set in two functions in
> wprt0.as:
> >_mainCRTStartup sets to true and _WinMainCRTStartup sets to False,
> this
> >cause a wrong setup of standard Output that DebugLn (lclprocs.pas)
> use to
> >emit debug messages.
> >
> >It seems that this functions are 'used' in the linker script, for
> example
> >ld .--verbose shows that by default a ENTRY(_mainCRTStartup) it's
> used. I
> >looked into the generated lazarus link.res file and didn't find a
> ENTRY
> >section so I guess it uses the default and that's why even when
> lazarus is
> >a gui app, it's linked as a console app.
> >
> >To demostrate this I modified the link.res script to inculde
> >ENTRY(_WinMainCRTStartup) section and then linked, the resulting
> >lazarus.exe file now behaves as a gui app and won't crash at
> start.
> >
> >It seems that WriteResponseFile lacks the code to add the ENTRY
> section to
> >the link.res script but I don't know if it's supposed to be there
> or
> >should it be in another stage, any comments?
>
> You need to use {$apptype gui} or use the -WG parameter. For the
> compiler
> it is not known if the application needs a console or not.
>
>
> Peter
>

Lazarus is already compiled with -WG

Jesus Reyes A.


__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ?gratis!
Reg?strate ya - http://correo.yahoo.com.mx/



------------------------------

Message: 2
Date: Mon, 1 Aug 2005 23:58:04 +0200
From: "Gerhard Scholz" <[hidden email]>
Subject: Re: [fpc-pascal] fast integer multiplication
To: "FPC-Pascal users discussions" <[hidden email]>
Message-ID: <04d601c596e4$1aef7de0$f7dcb9d9@es>
Content-Type: text/plain; charset="iso-8859-1"

...

>>The code generated for the above sample is:
>># [16] c:=a*b;
>>    movl    U_P$PROJECT1_A,%edx
>>    movl    U_P$PROJECT1_B,%eax
>>    mull    %edx
>>    movl    $0,%edx
>>    movl    %eax,U_P$PROJECT1_C
>>    movl    %edx,U_P$PROJECT1_C+4
>>
>>What I want is the above code, but without the "movl $0,%edx"
>>instruction. Is there a way to do this (wihtout using fpc_mul_qword).
>
>
> Only assembler for now. Any suggestions how the compiler could be told
> to generate such code?
...
>function UI32x32To64(A,B: Longword): QWord;
>assembler; register; nostackframe;
>asm
>    mull %edx
>end;
>
>It is fast but certainly much less than if it were inlined.

My suggestion would be:

  FUNCTION lmul ( CONST a,
                        b : LongInt ) : int64 ; inline ;

    BEGIN
{$ifdef cpu86}
      ASM
        movl    a,%eax
        imull   b
        movl    %eax,lmul
        movl    %edx,lmul+4
       END ;
{$else}
{$ifdef cpu68k}
      lmul := int64 ( a ) * b ;
{$else}
{$ifdef cpusparc}
      lmul := int64 ( a ) * b ;
{$else}
{$ifdef cpualpha}
      lmul := int64 ( a ) * b ;
{$else}
{$ifdef cpupowerpc}
      lmul := int64 ( a ) * b ;
{$else}
      lmul := int64 ( a ) * b ;
{$endif}
{$endif}
{$endif}
{$endif}
{$endif}
     END ;

and similar for unsigned mul.

(shortened here; full code in ulmul.pas; liitle test in tmuls.pas, timing
routines in
wtimer/tdrsc1)

Is portable so code doesn't need to be rewritten when compiled for other
processors (but not optimal then)

Tested only on i386.

Seems to be faster than standard multiplication (interesting: significantly
faster for signed mul than for unsigned mul), can be assembly-coded in the
branches for the other cpus
(if there are opcodes for such a multiplication - I don't know), could go to
unit math.pp.

It seems that routines which contain assembler are not inlined; on the day
somebody finds a trick to inline such code they should be really fast.

Gerhard
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ULMUL.pas
Type: application/octet-stream
Size: 2604 bytes
Desc: not available
Url :
http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20050801/86cde0
70/ULMUL-0001.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tmuls.pas
Type: application/octet-stream
Size: 1291 bytes
Desc: not available
Url :
http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20050801/86cde0
70/tmuls-0001.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: WTIMER.pas
Type: application/octet-stream
Size: 1645 bytes
Desc: not available
Url :
http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20050801/86cde0
70/WTIMER-0001.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: TDRSC1.pas
Type: application/octet-stream
Size: 2879 bytes
Desc: not available
Url :
http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20050801/86cde0
70/TDRSC1-0001.obj

------------------------------

Message: 3
Date: Tue,  2 Aug 2005 22:03:45 +1200
From: [hidden email]
Subject: [fpc-pascal] Can a program find out how it was started?
To: [hidden email]
Message-ID: <[hidden email]>
Content-Type: text/plain; charset=ISO-8859-1

Greetings from New Zealand,

I use fpc to compile programs intended to be run from the command line in a
cmd
window (Windows 2000, etc).  Sometimes, though, new users try to start the
programs by double-clicking on the exe file (application icon). When the
program starts, I would like to be able to tell if they have done that,
so that I can present a special error message.

So my question is:
  Is it possible for a program to tell whether it was started from
  the command line or started by double-clicking on the application icon?

I tried checking ParamStr(0) and DOSVersion, but these have the same values
regardless of how the program was started.

Any suggestions?

Thank you for your time,

Jeff Miller



------------------------------

Message: 4
Date: Tue, 2 Aug 2005 19:25:13 +1000
From: Moz <[hidden email]>
Subject: Re: [fpc-pascal] Can a program find out how it was started?
To: FPC-Pascal users discussions <[hidden email]>
Message-ID: <[hidden email]>
Content-Type: text/plain; charset=us-ascii

[hidden email] said:
> Sometimes, though, new users try to start the programs by
> double-clicking on the exe file (application icon). When the program
> starts, I would like to be able to tell if they have done that, so
> that I can present a special error message.

One trivial solution is to drop a readline at the end of the help
routine. That way the cmd window stays open until they hit enter
(which you should tell them to do). Mildly annoying for people at the
command line if there's only a few lines of help, but solves the
problem.

Moz




------------------------------

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


End of fpc-pascal Digest, Vol 12, Issue 2
*****************************************


__________ Informacia od NOD32 1.977 (20050120) __________

Tato sprava bola preverena antivirusovym systemom NOD32.
http://www.eset.sk




_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fast integer multiplication and one asm questio

Gerhard Scholz
For learning some basics of assembler you should download the NASM, it is
bundled with a documentation; more documentation should be found at the intel
sides. Or e.g. try http://www.agner.org/assem

But keep in mind that the exact syntax differs from assembler to assembler.

Also a good idea is to compile (little) pascal programs and look at the
generated code (compile with the option "-al")

A general assembler tutorial here would overflow this mail list.

Gerhard

-----Urspr?ngliche Nachricht-----
Von: "Pianoman" <[hidden email]>
An: <[hidden email]>
Gesendet: Dienstag, 2. August 2005 17:22
Betreff: [fpc-pascal] fast integer multiplication and one asm questio


> Hi, the UI32x32To64 function is great but is amazingly short. How can I
> Specify which varriables in function will be accessed. In this function for
> example it accesses a and b but source says mull (I don't know why not mul I
> didnt find mull instruction in any assembly book) and %eax why mull %eax
> multiplies a,b is it on the stack or why? If i would like to multiply a,b,c
> what would look source code then? Thanx for response to those  quite stupid
> assembly question(I am only beginner in asm :)
> Pianoman
> ----- Original Message -----
> From: <[hidden email]>
> To: <[hidden email]>
> Sent: Tuesday, August 02, 2005 12:00 PM
> Subject: fpc-pascal Digest, Vol 12, Issue 2
>
>
> Send fpc-pascal mailing list submissions to
> [hidden email]
>
> To subscribe or unsubscribe via the World Wide Web, visit
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
> or, via email, send a message with subject or body 'help' to
> [hidden email]
>
> You can reach the person managing the list at
> [hidden email]
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of fpc-pascal digest..."
>
>
> Today's Topics:
>
>    1. Re:  lazarus crash at start (Jesus Reyes)
>    2. Re:  fast integer multiplication (Gerhard Scholz)
>    3.  Can a program find out how it was started?
>       ([hidden email])
>    4. Re:  Can a program find out how it was started? (Moz)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Mon, 1 Aug 2005 11:21:10 -0500 (CDT)
> From: Jesus Reyes <[hidden email]>
> Subject: Re: [fpc-pascal] lazarus crash at start
> To: FPC-Pascal users discussions <[hidden email]>
> Message-ID: <[hidden email]>
> Content-Type: text/plain; charset=iso-8859-1
>
>
>  --- Peter Vreman <[hidden email]> escribi?:
>
> > At 01:12 1-8-2005, you wrote:
> > >Trying to find why lazarus crashes at start in win98 I found a
> > courious
> > >problem, global variable IsConsole is true when it should be false
> > as
> > >lazarus is built as a gui application.
> > >
> > >Digging I found that IsConsole is set in two functions in
> > wprt0.as:
> > >_mainCRTStartup sets to true and _WinMainCRTStartup sets to False,
> > this
> > >cause a wrong setup of standard Output that DebugLn (lclprocs.pas)
> > use to
> > >emit debug messages.
> > >
> > >It seems that this functions are 'used' in the linker script, for
> > example
> > >ld .--verbose shows that by default a ENTRY(_mainCRTStartup) it's
> > used. I
> > >looked into the generated lazarus link.res file and didn't find a
> > ENTRY
> > >section so I guess it uses the default and that's why even when
> > lazarus is
> > >a gui app, it's linked as a console app.
> > >
> > >To demostrate this I modified the link.res script to inculde
> > >ENTRY(_WinMainCRTStartup) section and then linked, the resulting
> > >lazarus.exe file now behaves as a gui app and won't crash at
> > start.
> > >
> > >It seems that WriteResponseFile lacks the code to add the ENTRY
> > section to
> > >the link.res script but I don't know if it's supposed to be there
> > or
> > >should it be in another stage, any comments?
> >
> > You need to use {$apptype gui} or use the -WG parameter. For the
> > compiler
> > it is not known if the application needs a console or not.
> >
> >
> > Peter
> >
>
> Lazarus is already compiled with -WG
>
> Jesus Reyes A.
>
>
> __________________________________________________
> Correo Yahoo!
> Espacio para todos tus mensajes, antivirus y antispam ?gratis!
> Reg?strate ya - http://correo.yahoo.com.mx/
>
>
>
> ------------------------------
>
> Message: 2
> Date: Mon, 1 Aug 2005 23:58:04 +0200
> From: "Gerhard Scholz" <[hidden email]>
> Subject: Re: [fpc-pascal] fast integer multiplication
> To: "FPC-Pascal users discussions" <[hidden email]>
> Message-ID: <04d601c596e4$1aef7de0$f7dcb9d9@es>
> Content-Type: text/plain; charset="iso-8859-1"
>
> ...
> >>The code generated for the above sample is:
> >># [16] c:=a*b;
> >>    movl    U_P$PROJECT1_A,%edx
> >>    movl    U_P$PROJECT1_B,%eax
> >>    mull    %edx
> >>    movl    $0,%edx
> >>    movl    %eax,U_P$PROJECT1_C
> >>    movl    %edx,U_P$PROJECT1_C+4
> >>
> >>What I want is the above code, but without the "movl $0,%edx"
> >>instruction. Is there a way to do this (wihtout using fpc_mul_qword).
> >
> >
> > Only assembler for now. Any suggestions how the compiler could be told
> > to generate such code?
> ...
> >function UI32x32To64(A,B: Longword): QWord;
> >assembler; register; nostackframe;
> >asm
> >    mull %edx
> >end;
> >
> >It is fast but certainly much less than if it were inlined.
>
> My suggestion would be:
>
>   FUNCTION lmul ( CONST a,
>                         b : LongInt ) : int64 ; inline ;
>
>     BEGIN
> {$ifdef cpu86}
>       ASM
>         movl    a,%eax
>         imull   b
>         movl    %eax,lmul
>         movl    %edx,lmul+4
>        END ;
> {$else}
> {$ifdef cpu68k}
>       lmul := int64 ( a ) * b ;
> {$else}
> {$ifdef cpusparc}
>       lmul := int64 ( a ) * b ;
> {$else}
> {$ifdef cpualpha}
>       lmul := int64 ( a ) * b ;
> {$else}
> {$ifdef cpupowerpc}
>       lmul := int64 ( a ) * b ;
> {$else}
>       lmul := int64 ( a ) * b ;
> {$endif}
> {$endif}
> {$endif}
> {$endif}
> {$endif}
>      END ;
>
> and similar for unsigned mul.
>
> (shortened here; full code in ulmul.pas; liitle test in tmuls.pas, timing
> routines in
> wtimer/tdrsc1)
>
> Is portable so code doesn't need to be rewritten when compiled for other
> processors (but not optimal then)
>
> Tested only on i386.
>
> Seems to be faster than standard multiplication (interesting: significantly
> faster for signed mul than for unsigned mul), can be assembly-coded in the
> branches for the other cpus
> (if there are opcodes for such a multiplication - I don't know), could go to
> unit math.pp.
>
> It seems that routines which contain assembler are not inlined; on the day
> somebody finds a trick to inline such code they should be really fast.
>
> Gerhard
> -------------- next part --------------
> A non-text attachment was scrubbed...
> Name: ULMUL.pas
> Type: application/octet-stream
> Size: 2604 bytes
> Desc: not available
> Url :
> http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20050801/86cde0
> 70/ULMUL-0001.obj
> -------------- next part --------------
> A non-text attachment was scrubbed...
> Name: tmuls.pas
> Type: application/octet-stream
> Size: 1291 bytes
> Desc: not available
> Url :
> http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20050801/86cde0
> 70/tmuls-0001.obj
> -------------- next part --------------
> A non-text attachment was scrubbed...
> Name: WTIMER.pas
> Type: application/octet-stream
> Size: 1645 bytes
> Desc: not available
> Url :
> http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20050801/86cde0
> 70/WTIMER-0001.obj
> -------------- next part --------------
> A non-text attachment was scrubbed...
> Name: TDRSC1.pas
> Type: application/octet-stream
> Size: 2879 bytes
> Desc: not available
> Url :
> http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20050801/86cde0
> 70/TDRSC1-0001.obj
>
> ------------------------------
>
> Message: 3
> Date: Tue,  2 Aug 2005 22:03:45 +1200
> From: [hidden email]
> Subject: [fpc-pascal] Can a program find out how it was started?
> To: [hidden email]
> Message-ID: <[hidden email]>
> Content-Type: text/plain; charset=ISO-8859-1
>
> Greetings from New Zealand,
>
> I use fpc to compile programs intended to be run from the command line in a
> cmd
> window (Windows 2000, etc).  Sometimes, though, new users try to start the
> programs by double-clicking on the exe file (application icon). When the
> program starts, I would like to be able to tell if they have done that,
> so that I can present a special error message.
>
> So my question is:
>   Is it possible for a program to tell whether it was started from
>   the command line or started by double-clicking on the application icon?
>
> I tried checking ParamStr(0) and DOSVersion, but these have the same values
> regardless of how the program was started.
>
> Any suggestions?
>
> Thank you for your time,
>
> Jeff Miller
>
>
>
> ------------------------------
>
> Message: 4
> Date: Tue, 2 Aug 2005 19:25:13 +1000
> From: Moz <[hidden email]>
> Subject: Re: [fpc-pascal] Can a program find out how it was started?
> To: FPC-Pascal users discussions <[hidden email]>
> Message-ID: <[hidden email]>
> Content-Type: text/plain; charset=us-ascii
>
> [hidden email] said:
> > Sometimes, though, new users try to start the programs by
> > double-clicking on the exe file (application icon). When the program
> > starts, I would like to be able to tell if they have done that, so
> > that I can present a special error message.
>
> One trivial solution is to drop a readline at the end of the help
> routine. That way the cmd window stays open until they hit enter
> (which you should tell them to do). Mildly annoying for people at the
> command line if there's only a few lines of help, but solves the
> problem.
>
> Moz
>
>
>
>
> ------------------------------
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
>
> End of fpc-pascal Digest, Vol 12, Issue 2
> *****************************************
>
>
> __________ Informacia od NOD32 1.977 (20050120) __________
>
> Tato sprava bola preverena antivirusovym systemom NOD32.
> http://www.eset.sk
>
>
>
>
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal