New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use MemoryMarshal.Cast<byte, _> on 64-bit Android #1606
base: master
Are you sure you want to change the base?
Use MemoryMarshal.Cast<byte, _> on 64-bit Android #1606
Conversation
According to the documentation, Android is always little-endian, there is no need to check the endianness. |
Here is the comparison between ARMv7 and ARM64 code of the new implementation for SafeBitConverter.ToInt64 (Unity 2021.3.23f1 + IL2CPP with Release configuration): ARMv7: 00743858 <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F>:
743858: e92d4ff0 push {r4, r5, r6, r7, r8, r9, sl, fp, lr}
74385c: e24dd004 sub sp, sp, #4
743860: e59f611c ldr r6, [pc, #284] ; 743984 <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0x12c>
743864: e1a04000 mov r4, r0
743868: e1a05001 mov r5, r1
74386c: e08f6006 add r6, pc, r6
743870: e5d60000 ldrb r0, [r6]
743874: e3500000 cmp r0, #0
743878: 1a000004 bne 743890 <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0x38>
74387c: e59f0104 ldr r0, [pc, #260] ; 743988 <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0x130>
743880: e79f0000 ldr r0, [pc, r0]
743884: ebeb1109 bl 207cb0 <_Z42il2cpp_codegen_initialize_runtime_metadataPj>
743888: e3a00001 mov r0, #1
74388c: e5c60000 strb r0, [r6]
743890: e3550000 cmp r5, #0
743894: 0a000004 beq 7438ac <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0x54>
743898: e5d40000 ldrb r0, [r4]
74389c: e3550001 cmp r5, #1
7438a0: 1a000019 bne 74390c <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0xb4>
7438a4: e1a05000 mov r5, r0
7438a8: ea000001 b 7438b4 <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0x5c>
7438ac: ebeb1144 bl 207dc4 <_Z49il2cpp_codegen_raise_index_out_of_range_exceptionv>
7438b0: e5d45000 ldrb r5, [r4]
7438b4: ebeb1142 bl 207dc4 <_Z49il2cpp_codegen_raise_index_out_of_range_exceptionv>
7438b8: e5d49001 ldrb r9, [r4, #1]
7438bc: ebeb1140 bl 207dc4 <_Z49il2cpp_codegen_raise_index_out_of_range_exceptionv>
7438c0: e5d4a002 ldrb sl, [r4, #2]
7438c4: ebeb113e bl 207dc4 <_Z49il2cpp_codegen_raise_index_out_of_range_exceptionv>
7438c8: e5d4b003 ldrb fp, [r4, #3]
7438cc: ebeb113c bl 207dc4 <_Z49il2cpp_codegen_raise_index_out_of_range_exceptionv>
7438d0: e5d46004 ldrb r6, [r4, #4]
7438d4: ebeb113a bl 207dc4 <_Z49il2cpp_codegen_raise_index_out_of_range_exceptionv>
7438d8: e5d47005 ldrb r7, [r4, #5]
7438dc: ebeb1138 bl 207dc4 <_Z49il2cpp_codegen_raise_index_out_of_range_exceptionv>
7438e0: e5d48006 ldrb r8, [r4, #6]
7438e4: ebeb1136 bl 207dc4 <_Z49il2cpp_codegen_raise_index_out_of_range_exceptionv>
7438e8: e5d41007 ldrb r1, [r4, #7]
7438ec: e1850409 orr r0, r5, r9, lsl #8
7438f0: e1862407 orr r2, r6, r7, lsl #8
7438f4: e180080a orr r0, r0, sl, lsl #16
7438f8: e1822808 orr r2, r2, r8, lsl #16
7438fc: e1800c0b orr r0, r0, fp, lsl #24
743900: e1821c01 orr r1, r2, r1, lsl #24
743904: e28dd004 add sp, sp, #4
743908: e8bd8ff0 pop {r4, r5, r6, r7, r8, r9, sl, fp, pc}
74390c: e5d49001 ldrb r9, [r4, #1]
743910: e3550002 cmp r5, #2
743914: 9a000004 bls 74392c <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0xd4>
743918: e5d4a002 ldrb sl, [r4, #2]
74391c: e3550003 cmp r5, #3
743920: 1a000003 bne 743934 <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0xdc>
743924: e1a05000 mov r5, r0
743928: eaffffe5 b 7438c4 <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0x6c>
74392c: e1a05000 mov r5, r0
743930: eaffffe1 b 7438bc <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0x64>
743934: e5d4b003 ldrb fp, [r4, #3]
743938: e3550004 cmp r5, #4
74393c: 9a000004 bls 743954 <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0xfc>
743940: e5d46004 ldrb r6, [r4, #4]
743944: e3550005 cmp r5, #5
743948: 1a000003 bne 74395c <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0x104>
74394c: e1a05000 mov r5, r0
743950: eaffffdf b 7438d4 <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0x7c>
743954: e1a05000 mov r5, r0
743958: eaffffdb b 7438cc <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0x74>
74395c: e5d47005 ldrb r7, [r4, #5]
743960: e3550006 cmp r5, #6
743964: 9a000004 bls 74397c <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0x124>
743968: e5d48006 ldrb r8, [r4, #6]
74396c: e3550007 cmp r5, #7
743970: e1a05000 mov r5, r0
743974: 0affffda beq 7438e4 <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0x8c>
743978: eaffffda b 7438e8 <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0x90>
74397c: e1a05000 mov r5, r0
743980: eaffffd5 b 7438dc <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0x84>
743984: 00631d08 .word 0x00631d08
743988: 005e5650 .word 0x005e5650 ARM64: 00000000007ece64 <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F>:
7ece64: f81d0ff6 str x22, [sp,#-48]!
7ece68: a90153f5 stp x21, x20, [sp,#16]
7ece6c: a9027bf3 stp x19, x30, [sp,#32]
7ece70: 90003ad6 adrp x22, f44000 <GC_obj_kinds+0x40>
7ece74: f0003615 adrp x21, eaf000 <_GLOBAL_OFFSET_TABLE_+0x29d0>
7ece78: 397d32c8 ldrb w8, [x22,#3916]
7ece7c: f941e2b5 ldr x21, [x21,#960]
7ece80: aa0103f3 mov x19, x1
7ece84: aa0003f4 mov x20, x0
7ece88: 370000c8 tbnz w8, #0, 7ecea0 <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0x3c>
7ece8c: f0003600 adrp x0, eaf000 <_GLOBAL_OFFSET_TABLE_+0x29d0>
7ece90: f941e000 ldr x0, [x0,#960]
7ece94: 97ed5f90 bl 344cd4 <_Z42il2cpp_codegen_initialize_runtime_metadataPm>
7ece98: 52800028 mov w8, #0x1 // #1
7ece9c: 393d32c8 strb w8, [x22,#3916]
7ecea0: 90003ad6 adrp x22, f44000 <GC_obj_kinds+0x40>
7ecea4: 397d9ac8 ldrb w8, [x22,#3942]
7ecea8: f94002b5 ldr x21, [x21]
7eceac: 35000128 cbnz w8, 7eced0 <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0x6c>
7eceb0: f0003600 adrp x0, eaf000 <_GLOBAL_OFFSET_TABLE_+0x29d0>
7eceb4: f941e000 ldr x0, [x0,#960]
7eceb8: 97ed5f87 bl 344cd4 <_Z42il2cpp_codegen_initialize_runtime_metadataPm>
7ecebc: b0003600 adrp x0, ead000 <_GLOBAL_OFFSET_TABLE_+0x9d0>
7ecec0: f944b800 ldr x0, [x0,#2416]
7ecec4: 97ed5f84 bl 344cd4 <_Z42il2cpp_codegen_initialize_runtime_metadataPm>
7ecec8: 52800028 mov w8, #0x1 // #1
7ececc: 393d9ac8 strb w8, [x22,#3942]
7eced0: f9401ea8 ldr x8, [x21,#56]
7eced4: aa1403e0 mov x0, x20
7eced8: aa1303e1 mov x1, x19
7ecedc: f9400d02 ldr x2, [x8,#24]
7ecee0: f9400448 ldr x8, [x2,#8]
7ecee4: d63f0100 blr x8
7ecee8: f27d727f tst x19, #0xfffffff8
7eceec: 540000c0 b.eq 7ecf04 <SafeBitConverter_ToInt64_m9DE2E54118B3A6638F95C1F99E6B3DD3C64E375F+0xa0>
7ecef0: f9400000 ldr x0, [x0]
7ecef4: a9427bf3 ldp x19, x30, [sp,#32]
7ecef8: a94153f5 ldp x21, x20, [sp,#16]
7ecefc: f84307f6 ldr x22, [sp],#48
7ecf00: d65f03c0 ret
7ecf04: 97ed5fbb bl 344df0 <_Z49il2cpp_codegen_raise_index_out_of_range_exceptionv> |
@AlanLiu90 There is #if UNITY_ANDROID && !UNITY_EDITOR && !UNITY_64 Isn't the above enough? By the way, this PR will not to be merged because of #1734, I think. |
For the following code (Tested in Unity 2021.3.34 and with IL2CPP): unsafe void Start()
{
#if UNITY_64
Debug.Log($"sizeof(int*) is {sizeof(int*)} on 64-bit platform ({Application.platform})");
#else
Debug.Log($"sizeof(int*) is {sizeof(int*)} on 32-bit platform ({Application.platform})");
#endif
} On a 32-bit device, the Android player outputted: |
The original issue that SafeBitConverter tries to solve only happens on ARMv7 devices. But in current code, both 32-bit and 64-bit Android devices execute the non-efficient path.
This PR makes 64-bit Android can execute the efficent path. Due to the optimization of c++ compiler,
if (sizeof(int*) == 4)
and the unreachable branch will be eliminated, so they won't be in the compiled code.