get_native_u2 & friends
Gary Benson
gbenson at redhat.com
Tue Mar 10 01:04:51 PDT 2009
Hi Ed,
Sorry I haven't looked into this yet. I filed a bug for it
(http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=301)
to make sure it doesn't get lost.
Cheers,
Gary
Edward Nevill wrote:
> Hi all,
>
> I think the best thing to do with get_native_u2 and friends is to
> let the compiler decide how to access unaligned data. Most modern
> compilers have some facility for doing this. In gcc you can use
> __attribute__((packed)) as follows.
>
> typedef union unaligned {
> unsigned u;
> unsigned short us;
> unsigned long long ul;
> } __attribute__((packed)) unaligned;
>
> unsigned short get_native_u2(unaligned *p)
> {
> return p->us;
> }
>
> unsigned get_native_u4(unaligned *p)
> {
> return p->u;
> }
>
> unsigned long long get_native_u8(unaligned *p)
> {
> return p->ul;
> }
>
> Below is the code generated for ARM and X86. Note that in the X86
> case it just does the access since X86 allows unaligned accesses
> whereas for ARM it goes ahead and doesbyte loads.
>
> If on some architechture it is better to test the alignment and then
> do word/halfword loads if the pointer is aligned and byte loads
> otherwise then hopefully the compiler will know the best code to
> generate rarther than us trying to second guess what is best on
> individual architectures.
>
> Also, in many case these functions are called when it is known that
> the data is aligned as in this example from _tableswitch...
>
> CASE(_tableswitch): {
> jint* lpc = (jint*)VMalignWordUp(pc+1);
> int32_t key = STACK_INT(-1);
> int32_t low = Bytes::get_Java_u4((address)&lpc[1]);
> int32_t high = Bytes::get_Java_u4((address)&lpc[2]);
>
> Maybe it is worth having get_Java_u4() and get_Java_u4_unaligned()?
>
> Regards,
> Ed.
>
>
> --- x86.s --------------------------------------------------------
> .file "test.c"
> .text
> .p2align 4,,15
> .globl get_native_u2
> .type get_native_u2, @function
> get_native_u2:
> pushl %ebp
> movl %esp, %ebp
> movl 8(%ebp), %eax
> popl %ebp
> movzwl (%eax), %eax
> ret
> .size get_native_u2, .-get_native_u2
> .p2align 4,,15
> .globl get_native_u4
> .type get_native_u4, @function
> get_native_u4:
> pushl %ebp
> movl %esp, %ebp
> movl 8(%ebp), %eax
> popl %ebp
> movl (%eax), %eax
> ret
> .size get_native_u4, .-get_native_u4
> .p2align 4,,15
> .globl get_native_u8
> .type get_native_u8, @function
> get_native_u8:
> pushl %ebp
> movl %esp, %ebp
> movl 8(%ebp), %eax
> popl %ebp
> movl 4(%eax), %edx
> movl (%eax), %eax
> ret
> .size get_native_u8, .-get_native_u8
> .ident "GCC: (GNU) 4.2.4 (Ubuntu 4.2.4-1ubuntu3)"
> .section .note.GNU-stack,"", at progbits
> --- arm.s -------------------------------------------------------------
> .arch armv5t
> .fpu softvfp
> .eabi_attribute 20, 1
> .eabi_attribute 21, 1
> .eabi_attribute 23, 3
> .eabi_attribute 24, 1
> .eabi_attribute 25, 1
> .eabi_attribute 26, 2
> .eabi_attribute 30, 2
> .eabi_attribute 18, 4
> .file "test.c"
> .text
> .align 2
> .global get_native_u2
> .type get_native_u2, %function
> get_native_u2:
> @ args = 0, pretend = 0, frame = 0
> @ frame_needed = 0, uses_anonymous_args = 0
> @ link register save eliminated.
> ldrb r3, [r0, #1] @ zero_extendqisi2
> ldrb r0, [r0, #0] @ zero_extendqisi2
> orr r0, r0, r3, asl #8
> bx lr
> .size get_native_u2, .-get_native_u2
> .align 2
> .global get_native_u4
> .type get_native_u4, %function
> get_native_u4:
> @ args = 0, pretend = 0, frame = 0
> @ frame_needed = 0, uses_anonymous_args = 0
> @ link register save eliminated.
> ldrb r1, [r0, #1] @ zero_extendqisi2
> ldrb r3, [r0, #0] @ zero_extendqisi2
> ldrb r2, [r0, #2] @ zero_extendqisi2
> ldrb r0, [r0, #3] @ zero_extendqisi2
> orr r3, r3, r1, asl #8
> orr r3, r3, r2, asl #16
> orr r0, r3, r0, asl #24
> bx lr
> .size get_native_u4, .-get_native_u4
> .align 2
> .global get_native_u8
> .type get_native_u8, %function
> get_native_u8:
> @ args = 0, pretend = 0, frame = 0
> @ frame_needed = 0, uses_anonymous_args = 0
> @ link register save eliminated.
> stmfd sp!, {r4, r5, r6}
> ldrb r5, [r0, #1] @ zero_extendqisi2
> ldrb r6, [r0, #5] @ zero_extendqisi2
> ldrb r3, [r0, #0] @ zero_extendqisi2
> ldrb ip, [r0, #2] @ zero_extendqisi2
> ldrb r1, [r0, #4] @ zero_extendqisi2
> ldrb r2, [r0, #6] @ zero_extendqisi2
> ldrb r4, [r0, #7] @ zero_extendqisi2
> ldrb r0, [r0, #3] @ zero_extendqisi2
> orr r3, r3, r5, asl #8
> orr r1, r1, r6, asl #8
> orr r3, r3, ip, asl #16
> orr r1, r1, r2, asl #16
> orr r0, r3, r0, asl #24
> orr r1, r1, r4, asl #24
> ldmfd sp!, {r4, r5, r6}
> bx lr
> .size get_native_u8, .-get_native_u8
> .ident "GCC: (Ubuntu 4.3.3-1ubuntu1) 4.3.3"
> .section .note.GNU-stack,"",%progbits
>
>
--
http://gbenson.net/
More information about the zero-dev
mailing list