All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mauro Carvalho Chehab <mchehab@redhat.com>
To: Sakari Ailus <sakari.ailus@iki.fi>
Cc: Andy Walls <awalls@md.metrocast.net>,
	Hans Verkuil <hverkuil@xs4all.nl>,
	linux-media@vger.kernel.org, laurent.pinchart@ideasonboard.com,
	remi@remlab.net, nbowler@elliptictech.com,
	james.dutton@gmail.com
Subject: Re: [RFC v3 1/2] v4l: Do not use enums in IOCTL structs
Date: Thu, 03 May 2012 09:07:35 -0300	[thread overview]
Message-ID: <4FA27507.3050508@redhat.com> (raw)
In-Reply-To: <4FA25F66.5090100@iki.fi>

Em 03-05-2012 07:35, Sakari Ailus escreveu:
> Hi Mauro,
> 
> Mauro Carvalho Chehab wrote:
>> Em 02-05-2012 21:42, Andy Walls escreveu:
>>> On Wed, 2012-05-02 at 19:17 -0300, Mauro Carvalho Chehab wrote:
>>>
>>>> We can speed-up the conversions, with something like:
>>>>
>>>> enum foo {
>>>>     BAR
>>>> };
>>>>
>>>> if (sizeof(foo) != sizeof(u32))
>>>>     call_compat_logic().
>>>>
>>>> I suspect that sizeof() won't work inside a macro.
>>>
>>> sizeof() is evaluated at compile time, after preprocessing.
>>> It should work inside of a macro.
>>
>> I tried to compile this small piece of code:
>>
>> enum foo { BAR };
>> #if sizeof(foo) != sizeof(int)
>> void main(void) { printf("different sizes\n"); }
>> #else
>> void main(void) { printf("same size\n"); }
>> #endif
>>
>> It gives an error:
>>
>> /tmp/foo.c:2:11: error: missing binary operator before token "("
>>
>> So, either this doesn't work, because sizeof() is evaluated too late,
>> or some trick is needed.
>>
>> Weird enough, cpp generates the error, but the expression is well-evaluated:
>>
>> $ cpp /tmp/foo.c
>> # 1 "/tmp/foo.c"
>> # 1 "<built-in>"
>> # 1 "<command-line>"
>> # 1 "/tmp/foo.c"
>> /tmp/foo.c:2:11: error: missing binary operator before token "("
>> enum foo { BAR };
> 
> sizeof() is processed by C compiler while #if is preprocessor directive, and its arguments have to be evaluable by the preprocessor, which is the problem here.
> 
> The C compiler can also optimise away things like that but it's more difficult to see whether that takes place or not; one would need to look at the resulting assembly code.

This code:

void main(void) {
	if (sizeof(int) == sizeof(char))
	   	printf("same size\n");
	else
		printf("different sizes\n"); 
}

should be evaluated by the compiler as if (0) and should not generate any code.

The assembler for it is:

	.file	"foo.c"
	.section	.rodata
.LC0:
	.string	"different sizes"
	.text
	.globl	main
	.type	main, @function
main:
.LFB0:
	.cfi_startproc
	pushq	%rbp
	.cfi_def_cfa_offset 16
	.cfi_offset 6, -16
	movq	%rsp, %rbp
	.cfi_def_cfa_register 6
	movl	$.LC0, %edi
	call	puts
	popq	%rbp
	.cfi_def_cfa 7, 8
	ret
	.cfi_endproc
.LFE0:
	.size	main, .-main
	.ident	"GCC: (GNU) 4.6.3 20120306 (Red Hat 4.6.3-2)"
	.section	.note.GNU-stack,"",@progbits

So, gcc will remove the dead code, as expected.

So, the trick is to do something similar to that on the compat code, in order
to avoid any penalties when sizeof(enum) is 32 bits.

Regards,
Mauro

  reply	other threads:[~2012-05-03 12:07 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-02 19:13 [RFC v2 0/2] V4L2 IOCTL enum compat wrapper Sakari Ailus
2012-05-02 19:13 ` [RFC v3 1/2] v4l: Do not use enums in IOCTL structs Sakari Ailus
2012-05-02 20:45   ` Hans Verkuil
2012-05-02 21:39     ` Sakari Ailus
2012-05-03  7:02       ` Hans Verkuil
2012-05-03 13:42         ` Mauro Carvalho Chehab
2012-05-03 14:12           ` Hans Verkuil
2012-05-03 10:57       ` Rémi Denis-Courmont
2012-05-03 10:58         ` Rémi Denis-Courmont
2012-05-03 12:20           ` Sakari Ailus
2012-05-02 22:17     ` Mauro Carvalho Chehab
2012-05-03  0:42       ` Andy Walls
2012-05-03 10:22         ` Mauro Carvalho Chehab
2012-05-03 10:35           ` Sakari Ailus
2012-05-03 12:07             ` Mauro Carvalho Chehab [this message]
2012-05-03 10:45           ` Sylwester Nawrocki
2012-05-03 23:02           ` Andy Walls
2012-05-03 10:39         ` Mauro Carvalho Chehab
2012-05-02 19:13 ` [RFC v3 2/2] v4l: Implement compat functions for enum to __u32 change Sakari Ailus
2012-05-02 22:32   ` Mauro Carvalho Chehab
2012-05-02 23:38     ` Laurent Pinchart
2012-05-03 12:20       ` Mauro Carvalho Chehab

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4FA27507.3050508@redhat.com \
    --to=mchehab@redhat.com \
    --cc=awalls@md.metrocast.net \
    --cc=hverkuil@xs4all.nl \
    --cc=james.dutton@gmail.com \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=linux-media@vger.kernel.org \
    --cc=nbowler@elliptictech.com \
    --cc=remi@remlab.net \
    --cc=sakari.ailus@iki.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.