swift-numerics icon indicating copy to clipboard operation
swift-numerics copied to clipboard

Integer utilities module

Open stephentyrone opened this issue 5 years ago • 12 comments

A new module providing additional functionality on the standard library integer types (and more generally, any fixed-width integer type), including:

  • [x] - bitwise rotation
  • [ ] - full add / sub (with carryin/carryout)
  • [x] - saturating arithmetic (functions, not operators)
  • [x] - division with rounding control
  • [x] - shift with rounding control
  • [x] - "true" mod function (Euclidean division)
  • [x] - GCD
  • [ ] - division by constant (precompute magic multiply and shift a la libdivide)
  • [ ] - modulus by constant (use Lemire's method)

stephentyrone avatar Jul 01 '19 18:07 stephentyrone

Is bit reverse fit on this?

The FFT algorithm required bit reversal https://en.wikipedia.org/wiki/Bit-reversal_permutation https://github.com/apple/swift-numerics/blob/796a0dc3f74a60f102937f61cc363644513671d1/Sources/Accelerate/bit_reverse.swift

SusanDoggie avatar Nov 09 '19 01:11 SusanDoggie

Definitely!

stephentyrone avatar Nov 09 '19 01:11 stephentyrone

Would basic number-theory functions be in scope for this module?

  • gcd
  • lcm
  • Bézout coefficients
  • Modular inverse

NevinBR avatar Apr 13 '20 15:04 NevinBR

Yes, absolutely.

stephentyrone avatar Apr 13 '20 15:04 stephentyrone

I’d like to start working on this. What’s the recommended process?

Should there be a dedicate branch for work on this module?

NevinBR avatar Aug 25 '21 17:08 NevinBR

The first step is just to make a branch in your own clone of the repo. Once you have some initial work, we can either merge it to main or create a branch if it's going to be a longer term project to get a coherent API.

stephentyrone avatar Aug 25 '21 18:08 stephentyrone

@NevinBR I'll be happy to help if that's helpful.

lemire avatar Aug 25 '21 18:08 lemire

Actually, I'll go ahead and do the basic module setup for you on main and drop a rotate implementation in to seed it, since I already have a pretty good idea what a first cut of that should look like, and then folks can put smaller PRs up adding individual features.

stephentyrone avatar Aug 25 '21 18:08 stephentyrone

https://github.com/apple/swift-numerics/pull/188

stephentyrone avatar Aug 25 '21 19:08 stephentyrone

What’s our file-naming strategy?

I see you’ve created “Rotated.swift”. Do we want all files to be that small and single-purpose, or should we rename it something like “BitwiseOperations.swift”?

The first function I’d like to add to this module is gcd (using the binary GCD algorithm), and I’m wondering if the filename should be “GCD.swift” or something broader like “ModularArithmetic.swift”?

NevinBR avatar Aug 26 '21 16:08 NevinBR

I'm not too worried about that; file naming is not public API so we can move them around. If it's only going to contain a GCD initially, let's name it "GCD.swift".

(Also, a GCD is useful outside of modular arithmetic contexts, so it feels misleading to stuff it in a ModularArithmetic.swift).

stephentyrone avatar Aug 26 '21 16:08 stephentyrone

Integer division and shifts with rounding: https://github.com/apple/swift-numerics/pull/203

stephentyrone avatar Oct 15 '21 17:10 stephentyrone