All of lore.kernel.org
 help / color / mirror / Atom feed
* [KJ] question on double cast
@ 2007-05-22  8:24 Yoann Padioleau
  2007-05-22 11:44 ` John Anthony Kazos Jr.
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Yoann Padioleau @ 2007-05-22  8:24 UTC (permalink / raw)
  To: kernel-janitors


Hi, 

In drivers/infiniband/hw/amso1100/c2_cm.c", line 321
there is

 	wr->ep_handle = (u32) (unsigned long) cm_id->provider_data;

What is the goal of this double cast ? There are plenty of
such double cast in the kernel.

_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/kernel-janitors

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [KJ] question on double cast
  2007-05-22  8:24 [KJ] question on double cast Yoann Padioleau
@ 2007-05-22 11:44 ` John Anthony Kazos Jr.
  2007-05-22 12:15 ` Yoann Padioleau
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: John Anthony Kazos Jr. @ 2007-05-22 11:44 UTC (permalink / raw)
  To: kernel-janitors

> In drivers/infiniband/hw/amso1100/c2_cm.c", line 321
> there is
> 
>  	wr->ep_handle = (u32) (unsigned long) cm_id->provider_data;
> 
> What is the goal of this double cast ? There are plenty of
> such double cast in the kernel.

provider_data is type "void *", so presumably the object it points to is 
of type [un]signed long (meaning its size differs on different 
architectures). It is being cast to unsigned long to set it to that size 
with no sign bits, and then the low-order 32 bits are being chopped off.
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/kernel-janitors

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [KJ] question on double cast
  2007-05-22  8:24 [KJ] question on double cast Yoann Padioleau
  2007-05-22 11:44 ` John Anthony Kazos Jr.
@ 2007-05-22 12:15 ` Yoann Padioleau
  2007-05-22 12:52 ` John Anthony Kazos Jr.
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Yoann Padioleau @ 2007-05-22 12:15 UTC (permalink / raw)
  To: kernel-janitors

"John Anthony Kazos Jr." <jakj@j-a-k-j.com> writes:

>> In drivers/infiniband/hw/amso1100/c2_cm.c", line 321
>> there is
>> 
>>  	wr->ep_handle = (u32) (unsigned long) cm_id->provider_data;
>> 
>> What is the goal of this double cast ? There are plenty of
>> such double cast in the kernel.
>
> provider_data is type "void *", so presumably the object it points to is 
> of type [un]signed long (meaning its size differs on different 
> architectures). It is being cast to unsigned long to set it to that size 
> with no sign bits, and then the low-order 32 bits are being chopped off.

But isn't it equivalent to directly do 

  wr->ep_handle = (u32) cm_id->provider_data;

? 




> _______________________________________________
> Kernel-janitors mailing list
> Kernel-janitors@lists.linux-foundation.org
> https://lists.linux-foundation.org/mailman/listinfo/kernel-janitors

_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/kernel-janitors

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [KJ] question on double cast
  2007-05-22  8:24 [KJ] question on double cast Yoann Padioleau
  2007-05-22 11:44 ` John Anthony Kazos Jr.
  2007-05-22 12:15 ` Yoann Padioleau
@ 2007-05-22 12:52 ` John Anthony Kazos Jr.
  2007-05-22 12:54 ` Matthew Wilcox
  2007-05-22 13:44 ` Matthew Wilcox
  4 siblings, 0 replies; 6+ messages in thread
From: John Anthony Kazos Jr. @ 2007-05-22 12:52 UTC (permalink / raw)
  To: kernel-janitors

> >> In drivers/infiniband/hw/amso1100/c2_cm.c", line 321
> >> there is
> >> 
> >>  	wr->ep_handle = (u32) (unsigned long) cm_id->provider_data;
> >> 
> >> What is the goal of this double cast ? There are plenty of
> >> such double cast in the kernel.
> >
> > provider_data is type "void *", so presumably the object it points to is 
> > of type [un]signed long (meaning its size differs on different 
> > architectures). It is being cast to unsigned long to set it to that size 
> > with no sign bits, and then the low-order 32 bits are being chopped off.
> 
> But isn't it equivalent to directly do 
> 
>   wr->ep_handle = (u32) cm_id->provider_data;

Depends on the endian-ness if you're on a 64-bit architecture. You'll get 
the high-order 32 or the low-order 32 depending on which are in the first 
4 bytes of the 64-bit quantity. The only way that would be safe is if the 
quantity is never wider than 32 bits.
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/kernel-janitors

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [KJ] question on double cast
  2007-05-22  8:24 [KJ] question on double cast Yoann Padioleau
                   ` (2 preceding siblings ...)
  2007-05-22 12:52 ` John Anthony Kazos Jr.
@ 2007-05-22 12:54 ` Matthew Wilcox
  2007-05-22 13:44 ` Matthew Wilcox
  4 siblings, 0 replies; 6+ messages in thread
From: Matthew Wilcox @ 2007-05-22 12:54 UTC (permalink / raw)
  To: kernel-janitors

On Tue, May 22, 2007 at 02:15:13PM +0200, Yoann Padioleau wrote:
> But isn't it equivalent to directly do 
> 
>   wr->ep_handle = (u32) cm_id->provider_data;

Try it on a 64-bit platform -- you get a warning about cast from pointer
to an integer of a different size.  The double-cast idiom tells the
compiler you really do know what you're doing here.
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/kernel-janitors

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [KJ] question on double cast
  2007-05-22  8:24 [KJ] question on double cast Yoann Padioleau
                   ` (3 preceding siblings ...)
  2007-05-22 12:54 ` Matthew Wilcox
@ 2007-05-22 13:44 ` Matthew Wilcox
  4 siblings, 0 replies; 6+ messages in thread
From: Matthew Wilcox @ 2007-05-22 13:44 UTC (permalink / raw)
  To: kernel-janitors

On Tue, May 22, 2007 at 08:52:14AM -0400, John Anthony Kazos Jr. wrote:
> > But isn't it equivalent to directly do 
> > 
> >   wr->ep_handle = (u32) cm_id->provider_data;
> 
> Depends on the endian-ness if you're on a 64-bit architecture. You'll get 
> the high-order 32 or the low-order 32 depending on which are in the first 
> 4 bytes of the 64-bit quantity. The only way that would be safe is if the 
> quantity is never wider than 32 bits.

That would be true if there were a pointer dereference involved, but
it's just a cast.  You'd get the low 32-bits either way, but you get a
warning on 64-bit.
_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.linux-foundation.org
https://lists.linux-foundation.org/mailman/listinfo/kernel-janitors

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2007-05-22 13:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-22  8:24 [KJ] question on double cast Yoann Padioleau
2007-05-22 11:44 ` John Anthony Kazos Jr.
2007-05-22 12:15 ` Yoann Padioleau
2007-05-22 12:52 ` John Anthony Kazos Jr.
2007-05-22 12:54 ` Matthew Wilcox
2007-05-22 13:44 ` Matthew Wilcox

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.