Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Mathematics long4 type

Discussion in 'Entity Component System' started by JanSietsma, Dec 15, 2019.

  1. JanSietsma

    JanSietsma

    Joined:
    Nov 16, 2010
    Posts:
    31
    Just wondering why there isn't longn or ulongn types in Unity.Mathematics? Should I be using double types and converting?
     
  2. DeepShade

    DeepShade

    Joined:
    Apr 10, 2018
    Posts:
    8
    I created an issue on the github repo regarding long and ulong vector types a few months ago.
    https://github.com/Unity-Technologies/Unity.Mathematics/issues/69

    Unfortunately I never got an answer :(
    I am really wondering why even double is supported but not 64bit integer numbers. The benefits are obvious and I don't see any technical reasons that whould explain why it is not already supported yet.
     
  3. JanSietsma

    JanSietsma

    Joined:
    Nov 16, 2010
    Posts:
    31
    That's a shame. I'm getting a lot out of the auto-vectorisation of int4, but need a long4 equivalent. I don't want to lose all those perf gains!
     
  4. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,554
    64bit integer numbers are supported like double (long/ulong). But not a vector version of both double and 64bit integer (double4/long4/ulong4).

    Maybe the technical reason is because uint4, int4, float4 all sums to 32 bits x 4 = 128 and it is possible to fit in xmm registers in AVX or similar sized register that they could ensure wider support. long4 / double4 would require special case and more work for some architecture with 256 bit register, then a special routine to divide and conquer with smaller registers on device without 256 bit commands?
     
  5. JanSietsma

    JanSietsma

    Joined:
    Nov 16, 2010
    Posts:
    31
    There is a double4. Am I misunderstanding something?

    Good point. I know very little about this, but it does seem like AVX2 has been around since 2013/2015 for Intel/AMD. But I suppose there would still need to be support for non-AVX2 processors.
     
  6. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,554
    I was referencing from Burst documentation, seems like it maybe burstable in the future and you can just use it now.
     
  7. JanSietsma

    JanSietsma

    Joined:
    Nov 16, 2010
    Posts:
    31
    Ah I see, thanks 5argon.

    Still a bit of a mystery why double4 is supported but long4 isn't, even as a non-vector type.
     
  8. Dale_Kim

    Dale_Kim

    Unity Technologies

    Joined:
    May 8, 2019
    Posts:
    54
    It may be supported in the future. What kinds of operations are you looking to perform with longN types?
     
  9. JanSietsma

    JanSietsma

    Joined:
    Nov 16, 2010
    Posts:
    31
  10. Dale_Kim

    Dale_Kim

    Unity Technologies

    Joined:
    May 8, 2019
    Posts:
    54
    Thanks for the example.

    I saw your comment in that code sample on DecodeMorton64 which says:
    /// Burst will not auto-vectorise 64 bit types.


    Do you have the assembly for that we can look at?
     
  11. JanSietsma

    JanSietsma

    Joined:
    Nov 16, 2010
    Posts:
    31
    Ah, I wasn't very clear on that comment. I meant there is no long4 type, so no packed assembly instructions. The burst docs don't list doublen a vector type, so there is no 64 bit vectorisation.

    I've added jobs and tests for the 64bit morton encoding to the project.

    I've previously measured the 32bit perf and looked at it's assembly, http://johnsietsma.com/2019/12/13/mordon-burst/. Having a uint4 version of the encoding and decoding functions doubles performance, I was hoping to do the same with long4.

    Assembly for the 64bit decode job is below:
    Code (CSharp):
    1.  
    2.         .text
    3.         .intel_syntax noprefix
    4.         .file   "main"
    5.         .globl  "Unity.Jobs.IJobExtensions.JobStruct`1<InfPoints.Octree.Jobs.Morton64EncodeJob>.Execute(ref InfPoints.Octree.Jobs.Morton64EncodeJob data, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, ref Unity.Jobs.LowLevel.Unsafe.JobRanges ranges, int jobIndex)_A91CEEBEFAC9E263"
    6.         .p2align        4, 0x90
    7.         .type   "Unity.Jobs.IJobExtensions.JobStruct`1<InfPoints.Octree.Jobs.Morton64EncodeJob>.Execute(ref InfPoints.Octree.Jobs.Morton64EncodeJob data, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, ref Unity.Jobs.LowLevel.Unsafe.JobRanges ranges, int jobIndex)_A91CEEBEFAC9E263",@function
    8. "Unity.Jobs.IJobExtensions.JobStruct`1<InfPoints.Octree.Jobs.Morton64EncodeJob>.Execute(ref InfPoints.Octree.Jobs.Morton64EncodeJob data, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, ref Unity.Jobs.LowLevel.Unsafe.JobRanges ranges, int jobIndex)_A91CEEBEFAC9E263":
    9.         .cfi_startproc
    10.         push    r15
    11.         .cfi_def_cfa_offset 16
    12.         push    r14
    13.         .cfi_def_cfa_offset 24
    14.         push    r13
    15.         .cfi_def_cfa_offset 32
    16.         push    r12
    17.         .cfi_def_cfa_offset 40
    18.         push    rsi
    19.         .cfi_def_cfa_offset 48
    20.         push    rdi
    21.         .cfi_def_cfa_offset 56
    22.         push    rbp
    23.         .cfi_def_cfa_offset 64
    24.         push    rbx
    25.         .cfi_def_cfa_offset 72
    26.         sub     rsp, 72
    27.         movdqa  xmmword ptr [rsp + 48], xmm6
    28.         .cfi_def_cfa_offset 144
    29.         .cfi_offset rbx, -72
    30.         .cfi_offset rbp, -64
    31.         .cfi_offset rdi, -56
    32.         .cfi_offset rsi, -48
    33.         .cfi_offset r12, -40
    34.         .cfi_offset r13, -32
    35.         .cfi_offset r14, -24
    36.         .cfi_offset r15, -16
    37.         .cfi_offset xmm6, -96
    38.         mov     rsi, rcx
    39.         mov     eax, dword ptr [rsi + 8]
    40.         test    eax, eax
    41.         jle     .LBB0_24
    42.         xor     r13d, r13d
    43.         movabs  rbx, offset .Lburst_abort_Ptr
    44.         movabs  r12, 1317624576693539401
    45.         xor     r14d, r14d
    46.         movabs  rbp, 1207822528635744451
    47.         .p2align        4, 0x90
    48. .LBB0_2:
    49.         movsxd  rcx, dword ptr [rsi + 12]
    50.         cmp     r14, rcx
    51.         jl      .LBB0_4
    52.         movsxd  rdx, dword ptr [rsi + 16]
    53.         cmp     r14, rdx
    54.         jle     .LBB0_9
    55. .LBB0_4:
    56.         test    ecx, ecx
    57.         jne     .LBB0_7
    58.         dec     eax
    59.         cmp     dword ptr [rsi + 16], eax
    60.         jne     .LBB0_7
    61.         movabs  rcx, offset .Lburst_abort.error.id.1
    62.         movabs  rdx, offset .Lburst_abort.error.message.2
    63.         jmp     .LBB0_8
    64.         .p2align        4, 0x90
    65. .LBB0_7:
    66.         movabs  rcx, offset .Lburst_abort.error.id.3
    67.         movabs  rdx, offset .Lburst_abort.error.message.4
    68. .LBB0_8:
    69.         call    qword ptr [rbx]
    70. .LBB0_9:
    71.         mov     rax, qword ptr [rsi + 24]
    72.         mov     ecx, dword ptr [rsi + 32]
    73.         mov     edx, dword ptr [rax]
    74.         and     edx, -7
    75.         cmp     ecx, edx
    76.         je      .LBB0_11
    77.         mov     edx, dword ptr [rsi + 36]
    78.         mov     qword ptr [rsp + 32], rax
    79.         mov     dword ptr [rsp + 40], ecx
    80.         mov     dword ptr [rsp + 44], edx
    81.         lea     rcx, [rsp + 32]
    82.         movabs  rax, offset ".LUnity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckReadAndThrowNoEarlyOut_Injected_Ptr"".LUnity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckWriteAndThrowNoEarlyOut_Injected_Ptr"
    83.         call    qword ptr [rax]
    84. .LBB0_23:
    85.         mov     rax, qword ptr [rsi + 56]
    86.         mov     qword ptr [rax + 8*r14], rdi
    87.         inc     r14
    88.         movsxd  rax, dword ptr [rsi + 8]
    89.         add     r13, 12
    90.         cmp     r14, rax
    91.         jl      .LBB0_2
    92. .LBB0_24:
    93.         movaps  xmm6, xmmword ptr [rsp + 48]
    94.         add     rsp, 72
    95.         pop     rbx
    96.         pop     rbp
    97.         pop     rdi
    98.         pop     rsi
    99.         pop     r12
    100.         pop     r13
    101.         pop     r14
    102.         pop     r15
    103.         ret
    104. .Lfunc_end0:
    105.         .size   "Unity.Jobs.IJobExtensions.JobStruct`1<InfPoints.Octree.Jobs.Morton64EncodeJob>.Execute(ref InfPoints.Octree.Jobs.Morton64EncodeJob data, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, ref Unity.Jobs.LowLevel.Unsafe.JobRanges ranges, int jobIndex)_A91CEEBEFAC9E263", .Lfunc_end0-"Unity.Jobs.IJobExtensions.JobStruct`1<InfPoints.Octree.Jobs.Morton64EncodeJob>.Execute(ref InfPoints.Octree.Jobs.Morton64EncodeJob data, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, ref Unity.Jobs.LowLevel.Unsafe.JobRanges ranges, int jobIndex)_A91CEEBEFAC9E263"
    106.         .cfi_endproc
    107.  
    108.         .globl  burst.initialize
    109.         .p2align        4, 0x90
    110.         .type   burst.initialize,@function
    111. burst.initialize:
    112.         .cfi_startproc
    113.         push    rsi
    114.         .cfi_def_cfa_offset 16
    115.         sub     rsp, 32
    116.         .cfi_def_cfa_offset 48
    117.         .cfi_offset rsi, -16
    118.         mov     rsi, rcx
    119.         movabs  rcx, offset .Lburst_abort.function.string
    120.         call    rsi
    121.         movabs  rcx, offset .Lburst_abort_Ptr
    122.         mov     qword ptr [rcx], rax
    123.         movabs  rcx, offset ".LUnity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckReadAndThrowNoEarlyOut_Injected.function.string"
    124.         call    rsi
    125.         movabs  rcx, offset ".LUnity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckReadAndThrowNoEarlyOut_Injected_Ptr"
    126.         mov     qword ptr [rcx], rax
    127.         movabs  rcx, offset ".LUnity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckWriteAndThrowNoEarlyOut_Injected.function.string"
    128.         call    rsi
    129.         movabs  rcx, offset ".LUnity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckWriteAndThrowNoEarlyOut_Injected_Ptr"
    130.         mov     qword ptr [rcx], rax
    131.         add     rsp, 32
    132.         pop     rsi
    133.         ret
    134. .Lfunc_end1:
    135.         .size   burst.initialize, .Lfunc_end1-burst.initialize
    136.         .cfi_endproc
    137.  
    138.         .type   .Lburst_abort.error.id,@object
    139.         .section        .rodata,"a",@progbits
    140. .Lburst_abort.error.id:
    141.         .asciz  "System.OverflowException"
    142.         .size   .Lburst_abort.error.id, 25
    143.  
    144.         .type   .Lburst_abort.error.message,@object
    145. .Lburst_abort.error.message:
    146.         .asciz  "An element of coordinates {0} is larger then the maximum {1}\nThrown from job: InfPoints.Octree.Jobs.Morton64EncodeJob"
    147.         .size   .Lburst_abort.error.message, 118
    148.  
    149.         .type   .Lburst_abort.error.id.1,@object
    150. .Lburst_abort.error.id.1:
    151.         .asciz  "System.IndexOutOfRangeException"
    152.         .size   .Lburst_abort.error.id.1, 32
    153.  
    154.         .type   .Lburst_abort.error.message.2,@object
    155. .Lburst_abort.error.message.2:
    156.         .asciz  "Index {0} is out of range of '{1}' Length.\nThrown from job: InfPoints.Octree.Jobs.Morton64EncodeJob"
    157.         .size   .Lburst_abort.error.message.2, 100
    158.  
    159.         .type   .Lburst_abort.error.id.3,@object
    160. .Lburst_abort.error.id.3:
    161.         .asciz  "System.IndexOutOfRangeException"
    162.         .size   .Lburst_abort.error.id.3, 32
    163.  
    164.         .type   .Lburst_abort.error.message.4,@object
    165. .Lburst_abort.error.message.4:
    166.         .asciz  "Index {0} is out of restricted IJobParallelFor range [{1}...{2}] in ReadWriteBuffer.\n\nThrown from job: InfPoints.Octree.Jobs.Morton64EncodeJob"
    167.         .size   .Lburst_abort.error.message.4, 143
    168.  
    169.         .type   .Lburst_abort.error.id.5,@object
    170. .Lburst_abort.error.id.5:
    171.         .asciz  "System.IndexOutOfRangeException"
    172.         .size   .Lburst_abort.error.id.5, 32
    173.  
    174.         .type   .Lburst_abort.error.message.6,@object
    175. .Lburst_abort.error.message.6:
    176.         .asciz  "Index {0} is out of range of '{1}' Length.\nThrown from job: InfPoints.Octree.Jobs.Morton64EncodeJob"
    177.         .size   .Lburst_abort.error.message.6, 100
    178.  
    179.         .type   .Lburst_abort.error.id.7,@object
    180. .Lburst_abort.error.id.7:
    181.         .asciz  "System.IndexOutOfRangeException"
    182.         .size   .Lburst_abort.error.id.7, 32
    183.  
    184.         .type   .Lburst_abort.error.message.8,@object
    185. .Lburst_abort.error.message.8:
    186.         .asciz  "Index {0} is out of restricted IJobParallelFor range [{1}...{2}] in ReadWriteBuffer.\n\nThrown from job: InfPoints.Octree.Jobs.Morton64EncodeJob"
    187.         .size   .Lburst_abort.error.message.8, 143
    188.  
    189.         .type   .Lburst_abort_Ptr,@object
    190.         .local  .Lburst_abort_Ptr
    191.         .comm   .Lburst_abort_Ptr,8,8
    192.         .type   .Lburst_abort.function.string,@object
    193. .Lburst_abort.function.string:
    194.         .asciz  "burst_abort"
    195.         .size   .Lburst_abort.function.string, 12
    196.  
    197.         .type   ".LUnity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckReadAndThrowNoEarlyOut_Injected_Ptr",@object
    198.         .local  ".LUnity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckReadAndThrowNoEarlyOut_Injected_Ptr"
    199.         .comm   ".LUnity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckReadAndThrowNoEarlyOut_Injected_Ptr",8,8
    200.         .type   ".LUnity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckReadAndThrowNoEarlyOut_Injected.function.string",@object
    201. ".LUnity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckReadAndThrowNoEarlyOut_Injected.function.string":
    202.         .asciz  "Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckReadAndThrowNoEarlyOut_Injected"
    203.         .size   ".LUnity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckReadAndThrowNoEarlyOut_Injected.function.string", 91
    204.  
    205.         .type   ".LUnity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckWriteAndThrowNoEarlyOut_Injected_Ptr",@object
    206.         .local  ".LUnity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckWriteAndThrowNoEarlyOut_Injected_Ptr"
    207.         .comm   ".LUnity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckWriteAndThrowNoEarlyOut_Injected_Ptr",8,8
    208.         .type   ".LUnity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckWriteAndThrowNoEarlyOut_Injected.function.string",@object
    209. ".LUnity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckWriteAndThrowNoEarlyOut_Injected.function.string":
    210.         .asciz  "Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckWriteAndThrowNoEarlyOut_Injected"
    211.         .size   ".LUnity.Collections.LowLevel.Unsafe.AtomicSafetyHandle::CheckWriteAndThrowNoEarlyOut_Injected.function.string", 92
    212.  
    213.  
    214.         .section        ".note.GNU-stack","",@progbits
    215.