Re: Floating point in kernel

From: Abramo Bagnara (abramo@alsa-project.org)
Date: Thu May 04 2000 - 16:50:35 EST


"Richard B. Johnson" wrote:
>
> On Thu, 4 May 2000, Abramo Bagnara wrote:
>
> > As far as I know this is not needed at all.
> > * and / have the same precedence and associativity is specified from
> > left to right so this is interpreted as:
> >
> > foo = (some_other_ulong * 1234) / 1000
> >
> > It's not guaranted that some_other_ulong * 1234 is evaluated before 1000
> > but we are sure that multiplication is done before division.
> >
> > If I'm wrong I'd like to know ASAP as I've written bogus C code for 15
> > years now ;-))
> >
>
> I don't think the C standard specifies the order of evaluation of
> constants. If you use the parentheses as you have shown, you
> force the evaluation of whatever is inside them first. This makes sure
> the code does the right thing.

Why constants should be treated differently from variable or function
calls or whatever?

>
> The problem comes up often when constants are evaluated by the code-
> generator.
>
> main()
> {
> int i = 1;
>
> i = i * 1234 / 1000;
> printf("%d\n", i);
> }
>
> This generates:
>
> .file "aaa.c"
> .version "01.01"
> gcc2_compiled.:
> .section .rodata
> .LC0:
> .string "%d\n"
> .text
> .align 4
> .globl main
> .type main,@function
> main:
> pushl %ebp
> movl %esp,%ebp
> pushl $1 ; <--------------- Here
> pushl $.LC0
> call printf
> leave
> ret
> .Lfe1:
> .size main,.Lfe1-main
> .ident "GCC: (GNU) 2.7.2.3"
>
> gcc just pushed the constant 1 on the stack and printed it.
> Now, if you turn off optimization, you get some code-generation.

Then what???? :-)

Try to change:

int i = 1;

into

int i = 9;

I bet my trousers that you got 11 and not 9 as you seem to expect.

-- 
Abramo Bagnara                       mailto:abramo@alsa-project.org

Opera Unica Via Emilia Interna, 140 Phone: +39.0546.656023 48014 Castel Bolognese (RA) - Italy Fax: +39.0546.656023

ALSA project is http://www.alsa-project.org sponsored by SuSE Linux http://www.suse.com

It sounds good!

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Sun May 07 2000 - 21:00:16 EST