* pointer arithmetic for alignment on short or int boundry
@ 2003-09-21 13:56 Andy Howell
2003-09-21 14:55 ` Burak Serdar
2003-09-21 15:40 ` Eljay Love-Jensen
0 siblings, 2 replies; 4+ messages in thread
From: Andy Howell @ 2003-09-21 13:56 UTC (permalink / raw)
To: gcc-help
I need to calculate an address on a short or int boundry. For some
reason this is not working:
char *a;
a = (a + 1) & ((char*) -2));
Gcc 3.3 on Solaris complains:
error: invalid operands of types `char*' and `
char*' to binary `operator&'
Why shouldn't I be able to mask off the low bit?
Thanks,
Andy
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: pointer arithmetic for alignment on short or int boundry
2003-09-21 13:56 pointer arithmetic for alignment on short or int boundry Andy Howell
@ 2003-09-21 14:55 ` Burak Serdar
2003-09-21 15:42 ` Andy Howell
2003-09-21 15:40 ` Eljay Love-Jensen
1 sibling, 1 reply; 4+ messages in thread
From: Burak Serdar @ 2003-09-21 14:55 UTC (permalink / raw)
To: Andy Howell; +Cc: gcc-help
Try
a = (char *)((unsigned)(a + 1) & (unsigned)((char*) -2))
You can only add or subtract pointers for pointer arithmetic.
Andy Howell wrote:
> I need to calculate an address on a short or int boundry. For some
> reason this is not working:
>
> char *a;
>
> a = (a + 1) & ((char*) -2));
>
> Gcc 3.3 on Solaris complains:
>
> error: invalid operands of types `char*' and `
> char*' to binary `operator&'
>
> Why shouldn't I be able to mask off the low bit?
>
> Thanks,
>
> Andy
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: pointer arithmetic for alignment on short or int boundry
2003-09-21 14:55 ` Burak Serdar
@ 2003-09-21 15:42 ` Andy Howell
0 siblings, 0 replies; 4+ messages in thread
From: Andy Howell @ 2003-09-21 15:42 UTC (permalink / raw)
To: bserdar; +Cc: gcc-help
Burak,
Thanks. You saved me from having to find that "do-as-I-mean" compiler
switch again.
Andy
> Try
>
> a = (char *)((unsigned)(a + 1) & (unsigned)((char*) -2))
>
> You can only add or subtract pointers for pointer arithmetic.
>
> Andy Howell wrote:
>
>> I need to calculate an address on a short or int boundry. For some
>> reason this is not working:
>>
>> char *a;
>>
>> a = (a + 1) & ((char*) -2));
>>
>> Gcc 3.3 on Solaris complains:
>>
>> error: invalid operands of types `char*' and `
>> char*' to binary `operator&'
>>
>> Why shouldn't I be able to mask off the low bit?
>>
>> Thanks,
>>
>> Andy
>>
>>
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: pointer arithmetic for alignment on short or int boundry
2003-09-21 13:56 pointer arithmetic for alignment on short or int boundry Andy Howell
2003-09-21 14:55 ` Burak Serdar
@ 2003-09-21 15:40 ` Eljay Love-Jensen
1 sibling, 0 replies; 4+ messages in thread
From: Eljay Love-Jensen @ 2003-09-21 15:40 UTC (permalink / raw)
To: Andy Howell, gcc-help
Hi Andy,
You need to do mathematical operations on non-pointer types. Use <stdint.h> (part of C99) to assist.
char* alignToIntPtr(char* a) {
const int lowBitsMask = sizeof(int) - 1;
int lowBits = reinterpret_cast<intptr_t>(a) & lowBitsMask;
// Rounding UP to next appropriate address, or stay same.
int delta = lowBits ? (sizeof(int) - lowBits) : 0;
a += delta;
// ...OR...
// Rounding DOWN to previous appropriate address, or stay same.
a -= lowBits;
return a;
}
I trust the optimizer to make the actual code nice and efficient. However, if this is in a really tight loop (as indicated by the profiler), you may want to investigate some bit-twiddling algorithms to make it even more efficient.
HTH,
--Eljay
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2003-09-21 15:42 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-21 13:56 pointer arithmetic for alignment on short or int boundry Andy Howell
2003-09-21 14:55 ` Burak Serdar
2003-09-21 15:42 ` Andy Howell
2003-09-21 15:40 ` Eljay Love-Jensen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).