SIMD for Collision

(box2d.org)

61 points | by birdculture 3 days ago

7 comments

  • zamalek 46 minutes ago
    Back in the day I was writing a liero clone, and came up with a neat scheme for pixel-perfect collisions (probably serendipitously, no claims it's unique). A 64bit integer can be seen as an 8x8 mask. You can pixel shift it up and down by simply bitshift left or right by 8n bits. Pixel shift left and right required more instructions; if I was to write it today I would probably store an additional rotated version instead (as that would then effectively pixel shift left and right with bit shifts).

    You then have the terrain chopped up into these 8x8s too, and can then do a collision test with at most 4 of them with the 1 character/entity mask.

  • grg0 3 hours ago
    As a SIMD noob, one thing that wasn't obvious to me is that SIMD can also speed you up if your mem throughput is underutilized by having the CPU load more data per instruction. It isn't just about compute speedups, which is typically what it's advertised for. Using perf on Linux has been very educational for me to get an intuition for modern CPU performance.
    • Cloudef 3 hours ago
      The way simd speeds up "compute" is indeed mainly the reason that you operate on multiple pieces of data at once.
      • grg0 3 hours ago
        Yeah, and in the blog post he mentions that he had to transform the data to SoA. If he had done that alone, he might already have seen a speedup from better cache utilization.

        Also, I see no mention of alignment in the post. I understand x86/AVX2 likes your load/stores to be aligned, even if it technically allows unaligned access.

        • dzaima 2 hours ago
          Unaligned loads/stores aren't super bad; if still within a cacheline, there's zero penalty, and on crossing cachelines it's alike two ops (except page crossing, which is more bad).

          So, for 32B loads/stores and 64B cachelines, it's 1.5x more L1 cache ops (as half of the ops will cross a cacheline); perhaps bad if you're L1-cache-throughput-bound, but less so if you're at L2+ as the extra work sits in L1.

      • ChrisGreenHeur 1 hour ago
        If so they should just name it that
        • ks6g10 35 minutes ago
          They should name it Multiple memory data, one single CPU instruction, or mmdosci for short. Easy to remember.
  • dcrazy 1 hour ago
    Wookash Podcast just interviewed Erin Catto (the Box2D author): https://podcasts.apple.com/us/podcast/what-makes-a-good-phys... (sorry, I couldn’t find a player-agnostic feed link, even on wooka.sh, the podcast’s own website!)
  • throwaway2027 1 hour ago
  • Decabytes 1 hour ago
    I feel like SIMD has been around for decades. Why has it taken this long to catch on? I feel like I have been hearing it a lot through the past few years. It feels like it’s talked about like some programming silver bullet
  • brcmthrowaway 4 hours ago
    No support for any ARM vector instructions.. thats horrible
  • NotGMan 2 hours ago
    [dead]