Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 20 Jun 2001 10:47:18 -0700
From:      John Merryweather Cooper <jmcoopr@webmail.bmi.net>
To:        j mckitrick <jcm@FreeBSD-uk.eu.org>
Cc:        John Merryweather Cooper <jmcoopr@webmail.bmi.net>, freebsd-questions@FreeBSD.ORG
Subject:   Re: what does this define do?
Message-ID:  <20010620104718.C14541@johncoop>
In-Reply-To: <20010620182353.A3958@dogma.freebsd-uk.eu.org>; from jcm@FreeBSD-uk.eu.org on Wed, Jun 20, 2001 at 10:23:53 -0700
References:  <20010620173855.A2941@dogma.freebsd-uk.eu.org> <20010620094925.A14541@johncoop> <20010620182353.A3958@dogma.freebsd-uk.eu.org>

next in thread | previous in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
On 2001.06.20 10:23 j mckitrick wrote:
> | This is, of course, highly compiler dependent.  Looks like somebody is
> | concerned with the efficiency of a MOV AX, 0 as opposed to an XOR AX,
> AX. 
> 
> I wondered about that.  But i agree, with no way of knowing what the
> compiler is doing anyway, it doesn't make much sense.  
> 
> The way it is being used seems to invalid the potential benefit:
> 
> static char var = FLAG1 | FLAG2 | n(FLAG3)
> 
> because the compiler might drop the n() values anyway, since ORing with 0
> does nothing anyway.  I can understand the n() values being in the code
> as
> placeholders, so you can see what lines are hi or low on the hardware
> register, but if the compiler drops them anyway, optimizing the macro
> rather
> than just using 0 doesn't seem to buy any benefits.
> 
> 
> Jonathon
> --
> "It is through will alone I set my mind in motion...."
> 

Because I qualify as among the curious, I ran a little test program to look
at the assembly.  A couple of observations:

	1) the "not/and" macro breaks on doubles;
	2) the "xor" macro also breaks on doubles;
	3) the compiler doesn't know about fldz . . . :)

See attached . . .

jmc
[-- Attachment #2 --]
#include <stdlib.h>

#define n1(flags) (~(flags) & (flags))
#define n2(flags) (0)
#define n3(flags) ((flags) ^ (flags))
#define n4(flags) ((flags) - (flags))

#define INIT_IVAR (-1)
#define INIT_DVAR (3.1425)

int main (int argc, char **argv)
{
  int ivar;
  double dvar;

  ivar = INIT_IVAR;
  ivar = n1(ivar);

  ivar = INIT_IVAR;
  ivar = n2(ivar);

  ivar = INIT_IVAR;
  ivar = n3(ivar);

  ivar = INIT_IVAR;
  ivar = n4(ivar);

  /* dvar = INIT_DVAR;  causes compiler error */
  /*  dvar = n1(dvar);                        */

  dvar = INIT_DVAR;
  dvar = n2(dvar);

  /* dvar = INIT_DVAR;  causes compiler error */
  /* dvar = n3(dvar);                         */

  dvar = INIT_DVAR;
  dvar = n4(dvar);

  exit(0);
}

[-- Attachment #3 --]

testzero.o:     file format elf32-i386

Disassembly of section .text:

00000000 <main>:
	;; prologue
   0:	55                   	push   %ebp
   1:	89 e5                	mov    %esp,%ebp
   3:	83 ec 18             	sub    $0x18,%esp
	;; n1(flags)
   6:	c7 45 fc ff ff ff ff 	movl   $0xffffffff,0xfffffffc(%ebp)
   d:	8b 45 fc             	mov    0xfffffffc(%ebp),%eax
  10:	f7 d0                	not    %eax
  12:	21 45 fc             	and    %eax,0xfffffffc(%ebp)
	;; n2(flags)
  15:	c7 45 fc ff ff ff ff 	movl   $0xffffffff,0xfffffffc(%ebp)
  1c:	c7 45 fc 00 00 00 00 	movl   $0x0,0xfffffffc(%ebp)
	;; n3(flags)
  23:	c7 45 fc ff ff ff ff 	movl   $0xffffffff,0xfffffffc(%ebp)
  2a:	8b 45 fc             	mov    0xfffffffc(%ebp),%eax
  2d:	31 45 fc             	xor    %eax,0xfffffffc(%ebp)
	;; n4(flags)
  30:	c7 45 fc ff ff ff ff 	movl   $0xffffffff,0xfffffffc(%ebp)
  37:	c7 45 fc 00 00 00 00 	movl   $0x0,0xfffffffc(%ebp)
	;; n2(flags)
  3e:	dd 05 00 00 00 00    	fldl   0x0
  44:	dd 5d f0             	fstpl  0xfffffff0(%ebp)
  47:	c7 45 f0 00 00 00 00 	movl   $0x0,0xfffffff0(%ebp)
  4e:	c7 45 f4 00 00 00 00 	movl   $0x0,0xfffffff4(%ebp)
	;; n4(flags)
  55:	dd 05 00 00 00 00    	fldl   0x0
  5b:	dd 5d f0             	fstpl  0xfffffff0(%ebp)
  5e:	dd 45 f0             	fldl   0xfffffff0(%ebp)
  61:	dc 65 f0             	fsubl  0xfffffff0(%ebp)
  64:	dd 5d f0             	fstpl  0xfffffff0(%ebp)
	;; epilogue, exit(0), etc.
  67:	83 c4 f4             	add    $0xfffffff4,%esp
  6a:	6a 00                	push   $0x0
  6c:	e8 fc ff ff ff       	call   6d <main+0x6d>
  71:	83 c4 10             	add    $0x10,%esp
  74:	c9                   	leave  
  75:	c3                   	ret    

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010620104718.C14541>