Board index » delphi » The use of coprcessor reals (previously Re: Stack overflow at binary tree balance)

The use of coprcessor reals (previously Re: Stack overflow at binary tree balance)

At Markus Humms request a new thread

"Jason Burgon" <gvision.nos...@ntlworld.com> schreef in bericht
news:BhLT9.38$Ge1.13389@newsfep2-gui...

Quote
> "Femme Verbeek" <fv[at]{*word*104}jet[dot]nl> wrote in message
> news:v1tspm13lb20b8@corp.supernews.com...

> > "Jason Birgon" <gvision.nos...@ntlworld.com> schreef in bericht
> > > type
> > >   { Real type [re]defininition }
> > > {$ifopt N+}
> > >   Real = Double;
> > > {$else}
> > > {$ifopt E+}
> > >   Real = Double;
> > > {$endif E+}
> > > {$endif N+}

> > > A Compile|Build is needed of all units if either the $N or $E
compiler
> > > option is changed.

> > I like the idea, however, since the $E option was created, somewhere
in
> > TP version 4 or so,  this is not necessary any more.
> > You can replace this code simply by
> > {$N+}
> > {$E+}
> > type real=double;

> As Dr Stockton points out in his response; not always. I'm currently
working
> on a concrete manufacturing control system that uses the "RTKernel"
Pascal
> library by OnTime Informatik. This provides a multi-threaded
environment
> that would suffer a considerable increase in context-switching time if
the
> co-processor were used. This is because the state of the co-pro would
have
> to be saved and restored on almost every context-switch of this
particular
> application.

OK, in special cases like that you can choose to compile in the N-
state. Problem with your code is that it also uses doubles in the
E+ N- state, which will not compile.

Better use something like
type
  { Real type [re]defininition }
{$ifopt N+}
{$E+}
    Real = Double;
{$else}
   double=real;
    single=real;
{$endif N+}

Problem with this is that E is switched on only in the unit that
contains this piece of code, while the type definition is valid
throughout the program. Easiest solution is to leave the E+ on in the
compiler anyway. It does not harm the program when compiled in the N-
state.

What I wanted to point out is that the emulated handling of 8 byte
doubles is faster than the normal 6 byte real type.
I tested this long time ago at the time when the E option was
introduced. I had some serious number crunching to do. The presence of a
80X87 processor made the difference between minutes or hours. But when
the program was demo'ed at a client, in most cases his computer would
not have a coprocessor. So the program needed to be runnable on any
machine without recompiling.

What most people here probably don't know is that the standard 6 byte
real is never handled by the coprocessor, which became standard with the
80486 and later.

--
Femme

 

Re:The use of coprcessor reals (previously Re: Stack overflow at binary tree balance)


Quote
"Femme Verbeek" <fv[at]{*word*104}jet[dot]nl> wrote in message

news:v2000fmp6ku57a@corp.supernews.com...

Quote
> OK, in special cases like that you can choose to compile in the N-
> state. Problem with your code is that it also uses doubles in the
> E+ N- state, which will not compile.

I see. I didn't know that, should have checked it.

Quote
> Better use something like
> type
>   { Real type [re]defininition }
> {$ifopt N+}
> {$E+}
>     Real = Double;
> {$else}
>    double=real;
>     single=real;
> {$endif N+}
> Problem with this is that E is switched on only in the unit that
> contains this piece of code, while the type definition is valid
> throughout the program.

It's worse than that. According to the Borland help:

"The {$E} (80x87 emulation) switch has no effect if used in a unit; it
 applies only to the compilation of a program."

So the above use of {$E+} has no effect at all.

Ok, but what about programs that ~must~ be run on a system with a co-pro
(for performance or other reasons)? IOW, where the programmer insists on
{$N+,E-}? I suppose an application like that could add a test for a real
(excuse the pun) co-pro during its initialialisation, but it will still drag
in the emulation code.

Quote
> Easiest solution is to leave the E+ on in the
> compiler anyway. It does not harm the program when compiled in the N-
> state.

Good point. Conclusion is that the common unit should not mess with either
the N or the E switches, and just test for {$N+}. So does this cover all
bases:

type
  { Real type [re]defininition }
{$ifopt N+}
    Real = Double;
{$else}
   double = real;
    single = real;
{$endif N+}

where both $N and $E are set by the main program or compiler settings?

Femme, you've restored my faith in c.l.p.b. It was starting to get very
boring around here.
--
Jay

Jason Burgon. Author of Graphic Vision
Version 2.21 available from:
http://homepage.ntlworld.com/gvision

Re:The use of coprcessor reals (previously Re: Stack overflow at binary tree balance)


"Jason Burgon" <gvision.nos...@ntlworld.com> schreef in bericht
news:2s%T9.953$Dy3.75815@newsfep1-gui.server.ntli.net...

Quote
> "Femme Verbeek" <fv[at]{*word*104}jet[dot]nl> wrote in message
> news:v2000fmp6ku57a@corp.supernews.com...

>> Problem with this is that E is switched on only in the unit that
>> contains this piece of code, while the type definition is valid
>> throughout the program.

>It's worse than that. According to the Borland help:

>"The {$E} (80x87 emulation) switch has no effect if used in a unit; it
> applies only to the compilation of a program."

>So the above use of {$E+} has no effect at all.

I now remember something: the N+ directive has to be put before any of
the code in the program.
You can start your mainprogram with

{$ifopt N+}
     {$E+}
{$endif N+}
That covers a possible use of the commandline compiler.

Quote
> Ok, but what about programs that ~must~ be run on a system with a
co-pro
> (for performance or other reasons)? IOW, where the programmer insists
on
> {$N+,E-}? I suppose an application like that could add a test for a
real
> (excuse the pun) co-pro during its initialialisation, but it will
still drag
> in the emulation code.

AFAIK the E+ N+ state adds a piece of runnable code to your program. At
startup the progam tests for the presence of a 80X87 processor. If
present the program uses the coprocessor code, if not it will use the
emulated code. It has no effect on either of the performances, just that
the program is a little bit bigger.
I don't expect that the E+ N- state will add extra code to the program.
I see no reason why, since it will always be dead code.

Quote
> > Easiest solution is to leave the E+ on in the
> > compiler anyway. It does not harm the program when compiled in the
N-
> > state.

> Good point. Conclusion is that the common unit should not mess with
either
> the N or the E switches, and just test for {$N+}. So does this cover
all
> bases:

> type
>   { Real type [re]defininition }
> {$ifopt N+}
>     Real = Double;
> {$else}
>    double = real;
>     single = real;
> {$endif N+}

> where both $N and $E are set by the main program or compiler settings?

I will add it to my realtype.pas unit.

BTW, if you want to have a single real or double regardless of the type
redefinition here, you can always use

system.single
system.real
system.double

Quote
> Femme, you've restored my faith in c.l.p.b. It was starting to get
very
> boring around here.

To much honour. [:-)

BTW. Does anybody know of a way to test for the presence of a coproc?

--
Femme

Re:The use of coprcessor reals (previously Re: Stack overflow at binary tree balance)


Quote
In article <v215gfgve9p...@corp.supernews.com>, Femme Verbeek wrote:
> BTW. Does anybody know of a way to test for the presence of a coproc?

protected mode asm:

asm
        fninit
        movl $0x5a5a, %eax
        fnstsw %ax
        cmpw $0, %ax
        seteb %al
end ['EAX'];

The protected mode machinecode is:

db e3
b8 5a 5a 00 00
df e0
66 83 f8 00
0f 94 c0

Re:The use of coprcessor reals (previously Re: Stack overflow at binary tree balance)


On Sat, 11 Jan 2003 23:01:29 +0000 (UTC), Marco van de Voort

Quote
<mar...@toad.stack.nl> wrote:
> In article <v215gfgve9p...@corp.supernews.com>, Femme Verbeek wrote:

> > BTW. Does anybody know of a way to test for the presence of a coproc?

> protected mode asm:

> asm
>         fninit
>         movl $0x5a5a, %eax
>         fnstsw %ax
>         cmpw $0, %ax
>         seteb %al
> end ['EAX'];

What kind of assembler is that? FPC? GNU? Both?
--
Rudy Velthuis
http://rvelthuis.bei.t-online.de/

Re:The use of coprcessor reals (previously Re: Stack overflow at binary tree balance)


Quote
> On Sat, 11 Jan 2003 23:01:29 +0000 (UTC), Marco van de Voort
><mar...@toad.stack.nl> wrote:

>> In article <v215gfgve9p...@corp.supernews.com>, Femme Verbeek wrote:

>> > BTW. Does anybody know of a way to test for the presence of a coproc?

>> protected mode asm:

>> asm
>>         fninit
>>         movl $0x5a5a, %eax
>>         fnstsw %ax
>>         cmpw $0, %ax
>>         seteb %al
>> end ['EAX'];

> What kind of assembler is that? FPC? GNU? Both?

Sorry, forget about that;

AT&T or Motorola style, as used a lot on Unix. (as most other processors do)
The normal way of writing is called "Intel" style. It is mere notational.

Both compilers named above support both conventions (they have to support AT&T because
the back-end assembler uses it). FPC has strong Intel support for historical reasons, but
I prefer AT&T, it is cleaner, more systematic, and has doublechecks (see above, e.g. the
cmp. The suffix w must match the width of ax).

I believe they can even get converted manually. I don't have a good tool for
that. but one can fool the FPC compiler to do so. (put asm code with
convention "A" in a pascal source, and generate code for the opposite convention.
(in this case select tasm as output assembler):

                fninit
                mov     eax,23130
                fnstsw  ax
                cmp     ax,0
                sete    al

The conversion isn't ideal, since hex immediates are now decimal, but I think you can see through
that ;_)

Probably the use of eax isn't necessary, and ax will work too.

Other Threads