apple / swift-numerics

Advanced mathematical types and functions for Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Integer utilities module

stephentyrone opened this issue · comments

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

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

Definitely!

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

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

Yes, absolutely.

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

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

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.

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

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.

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”?

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).

Integer division and shifts with rounding: #203