On Wed, Jul 6, 2011 at 11:28 PM, Mahr, Stefan <[email protected]> wrote:
>>>> "mips32_pracc_read_mem" casts uint32 to void, so we need to cast it
>>>> back to uint32. I found no suitable macro in actual sources.
>>>
>>> Hmm.... then I think we ought to define one to get this put to
>>> bed once and for all...
>>
>> static inline uint32_t uint32_read_unaligned(const void *data)
>> {
>> uint32_t t;
>> // Let's trust the compiler to do something very clever here.
>> memcpy(&t, data, sizeof(t));
>> return t;
>> }
>
> Do we really need a memcpy?
Trust the compiler:
oyvind@fierce:~$ cat test.c
#include <string.h>
#include <stdint.h>
uint32_t uint32_read_unaligned(const void *data)
{
uint32_t t;
// Let's trust the compiler to do something very clever here.
memcpy(&t, data, sizeof(t));
return t;
}
oyvind@fierce:~$ gcc -O3 -S test.c
oyvind@fierce:~$ cat test.s
.file "test.c"
.text
.p2align 4,,15
.globl uint32_read_unaligned
.type uint32_read_unaligned, @function
uint32_read_unaligned:
.LFB22:
.cfi_startproc
movl (%rdi), %eax
ret
.cfi_endproc
.LFE22:
.size uint32_read_unaligned, .-uint32_read_unaligned
.ident "GCC: (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2"
.section .note.GNU-stack,"",@progbits
> Could we ever run into an alignment
> issue when simply cast void *back* to uint32 ? If not, I would prefer
> the simplifed solution.
The below does not work in all cases. It will break on architectures that
do not support unaligned access.
That's why GCC is complaining. Sometimes...
--
Øyvind Harboe - Can Zylin Consulting help on your project?
US toll free 1-866-980-3434 / International +47 51 87 40 27
http://www.zylin.com/
_______________________________________________
Openocd-development mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/openocd-development