C Floating point arithmetic

Daniel R. Levy levy at ttrdc.UUCP
Thu Dec 5 09:24:40 AEST 1985


In article <4647 at alice.UUCP>, ark at alice.UucP (Andrew Koenig) writes:
>>	float a,b;
>>
>>	a = b + 1.0;	/* Gets done in double because 1.0 is a double.
>>			Gag me with a spoon. */
>
>Nah, gets done in single because the compiler realizes that 1.0 has
>the same representation in single and double, and therefore that
>the result of the addition will be the same.

On a 3B20S running Sys5 Release 2:
$ cat fabc.c
main()
{
	float a,b;
	a = b + 1.0;
}
$ cc -c -O fabc.c
$ dis fabc.o
		****   DISASSEMBLER  ****


disassembly for fabc.o

section	.text
main()
	   0:  7a02                          save    &0x0,&0x2
	   2:  c870 a04e 0000                movsd   0x4(%fp),%r0
	   8:  c908 0000 018e 0000           faddd2  $0x18,%r0 <==DOUBLE
	  10:  c96e 0000 a000                movds   %r0,0x0(%fp)
	  16:  7b00                          ret     &0x0
$ cat fabf.f
      real a,b
      a = b + 1.0
      end
$ f77 -c -O fabf.f
fabf.f:
   MAIN:
$ dis fabf.o
		****   DISASSEMBLER  ****


disassembly for fabf.o

section	.text
	   0:  7a00                          save    &0x0,&0x0
	   2:  114b                          addw2   &0x4,%sp
	   4:  800b                          br      +0xb <1c>
	   6:  ca08 0000 0208 0000 028e 0000 fadds3  $0x20,$0x28,%r0 <==SINGLE
	  12:  5108 0000 0240                movw    %r0,$0x24
	  18:  a100 7b00                     ret     &0x0
	  1c:  900c                          br      -0xc <6>
	  1e:  dede                          nop     
-- 
 -------------------------------    Disclaimer:  The views contained herein are
|       dan levy | yvel nad      |  my own and are not at all those of my em-
|         an engihacker @        |  ployer or the administrator of any computer
| at&t computer systems division |  upon which I may hack.
|        skokie, illinois        |
 --------------------------------   Path: ..!ihnp4!ttrdc!levy



More information about the Comp.lang.c mailing list