Slow division on iOS 15.0 vs 16.4
I think this is a low-priority issue, but still (#58):
The difference appears to be between iOS 15 and iOS 16.4:
// Swift 5.8, iPhone 13 Pro, iOS 16.4 (0.027 seconds, 0.019 seconds)
Test Case '-[NBKDoubleWidthKitBenchmarks.NBKDoubleWidthBenchmarksOnDivisionAsUInt256 testQuotientAndRemainder]' passed (0.027 seconds).
Test Case '-[NBKDoubleWidthKitBenchmarks.NBKDoubleWidthBenchmarksOnDivisionAsUInt256 testQuotientAndRemainderDividingByDigit]' passed (0.019 seconds).
// Swift 5.8, iPhone 13 Pro, iOS 15.0 (0.233 seconds, 0.547 seconds)
Test Case '-[NBKDoubleWidthKitBenchmarks.NBKDoubleWidthBenchmarksOnDivisionAsUInt256 testQuotientAndRemainder]' passed (0.233 seconds).
Test Case '-[NBKDoubleWidthKitBenchmarks.NBKDoubleWidthBenchmarksOnDivisionAsUInt256 testQuotientAndRemainderDividingByDigit]' passed (0.547 seconds).
All of the time is spend calling __udivmodti4, according to Instruments. So, basically, UInt.dividingFullWidth(_:) is slow as snails on iOS 15. Is my interpretation correct? And, if so, why? It seems so random.
It seems everything is slower on Xcode 14.2 (iPhone 14 Pro), but not as slow as above.
Probably iOS 16 was the first iOS release to pick up this optimisation of __udivmodti4 by @danlark1. Seems a bit late - that patch was integrated in 2020, iOS 16 was released in 2022 - but maybe that's the speed of Swift/LLVM in this regard.
It's pretty funny that the implementation prior to that patch was based on a C translation of PowerPC assembly for a 64-bit divmod function for 32-bit architectures. 😆 (The PowerPC Compiler Writer’s Guide)
Those were interesting reads. Thanks! I appreciate that you found the source of the problem. Since it has been solved, in a way, I suppose I'll close this issue when iOS 15 gets phased out.