├── .github └── workflows │ ├── publish-specifications-on-cocoapods.yml │ ├── run-lint-on-cocoapods-specifications.yml │ ├── run-tests-on-linux.yml │ └── run-tests-on-macos.yml ├── .gitignore ├── .swiftpm ├── NBKCoreKit-Benchmarks.xctestplan ├── NBKCoreKit.xctestplan ├── NBKDoubleWidthKit-Benchmarks.xctestplan ├── NBKDoubleWidthKit.xctestplan ├── NBKFlexibleWidthKit-Benchmarks.xctestplan ├── NBKFlexibleWidthKit.xctestplan ├── Numberick-Benchmarks.xctestplan ├── Numberick.xctestplan └── xcode │ ├── package.xcworkspace │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist │ └── xcshareddata │ └── xcschemes │ ├── NBKCoreKit-Benchmarks.xcscheme │ ├── NBKCoreKit.xcscheme │ ├── NBKDoubleWidthKit-Benchmarks.xcscheme │ ├── NBKDoubleWidthKit.xcscheme │ ├── NBKFlexibleWidthKit-Benchmarks.xcscheme │ ├── NBKFlexibleWidthKit.xcscheme │ ├── Numberick-Benchmarks.xcscheme │ └── Numberick.xcscheme ├── .utilities ├── update-gh-pages-docs-no-push.sh └── xcodebuild-clean-build-platforms.sh ├── LICENSE ├── Numberick-NBKCoreKit.podspec ├── Numberick-NBKDoubleWidthKit.podspec ├── Numberick.podspec ├── Package.swift ├── README.md ├── Sources ├── NBKCoreKit │ ├── Aliases.swift │ ├── Models │ │ ├── NBKChunkedInt.swift │ │ ├── NBKEndianness.swift │ │ ├── NBKPrimeSieve.swift │ │ └── NBKStaticBigInt.swift │ ├── NBKBinaryInteger.swift │ ├── NBKBitPatternConvertible.swift │ ├── NBKCoreInteger.swift │ ├── NBKFixedWidthInteger.swift │ ├── NBKSignedInteger.swift │ ├── NBKUnsignedInteger.swift │ └── Private │ │ ├── NBK+BitCast.swift │ │ ├── NBK+BlackHole.swift │ │ ├── NBK+Collection.swift │ │ ├── NBK+Data.swift │ │ ├── NBK+Endianness.swift │ │ ├── NBK+Messages.swift │ │ ├── NBK+Sign.swift │ │ ├── NBK+Swift.swift │ │ ├── NBK.swift │ │ ├── NBKCyclicIterator.swift │ │ ├── NBKGuarantee.swift │ │ ├── NBKIntegerDescription+Components.swift │ │ ├── NBKIntegerDescription+Decoding.swift │ │ ├── NBKIntegerDescription+Encoding.swift │ │ ├── NBKIntegerDescription+RadixAlphabet.swift │ │ ├── NBKIntegerDescription+RadixSolution.swift │ │ ├── NBKIntegerDescription.swift │ │ ├── NBKIntegerSignMagnitude+Comparisons.swift │ │ ├── NBKIntegerSignMagnitude.swift │ │ ├── NBKMaybeTwosComplement.swift │ │ ├── NBKProperBinaryInteger+Division.swift │ │ ├── NBKProperBinaryInteger+GreatestCommonDivisor.swift │ │ ├── NBKProperBinaryInteger+ModularMultiplicativeInverse.swift │ │ ├── NBKProperBinaryInteger+Roots.swift │ │ ├── NBKProperBinaryInteger.swift │ │ ├── NBKStrictBinaryInteger+Bits.swift │ │ ├── NBKStrictBinaryInteger+Complements.swift │ │ ├── NBKStrictBinaryInteger+Division.swift │ │ ├── NBKStrictBinaryInteger+Shifts.swift │ │ ├── NBKStrictBinaryInteger+Words.swift │ │ ├── NBKStrictBinaryInteger.swift │ │ ├── NBKStrictSignMagnitude+Comparisons.swift │ │ ├── NBKStrictSignMagnitude.swift │ │ ├── NBKStrictSignedInteger+Comparisons.swift │ │ ├── NBKStrictSignedInteger.swift │ │ ├── NBKStrictUnsignedInteger+Addition.swift │ │ ├── NBKStrictUnsignedInteger+Comparisons.swift │ │ ├── NBKStrictUnsignedInteger+Complements.swift │ │ ├── NBKStrictUnsignedInteger+Division+Digit.swift │ │ ├── NBKStrictUnsignedInteger+Division+Long.swift │ │ ├── NBKStrictUnsignedInteger+Multiplication+Digit.swift │ │ ├── NBKStrictUnsignedInteger+Multiplication+Karatsuba.swift │ │ ├── NBKStrictUnsignedInteger+Multiplication+Long.swift │ │ ├── NBKStrictUnsignedInteger+Multiplication.swift │ │ ├── NBKStrictUnsignedInteger+Partition.swift │ │ ├── NBKStrictUnsignedInteger+Shifts.swift │ │ ├── NBKStrictUnsignedInteger+Subtraction.swift │ │ ├── NBKStrictUnsignedInteger.swift │ │ ├── NBKSuccinctInt.swift │ │ ├── NBKTupleBinaryInteger+Addition.swift │ │ ├── NBKTupleBinaryInteger+Collection.swift │ │ ├── NBKTupleBinaryInteger+Comparisons.swift │ │ ├── NBKTupleBinaryInteger+Complements.swift │ │ ├── NBKTupleBinaryInteger+Division.swift │ │ ├── NBKTupleBinaryInteger+Multiplication.swift │ │ ├── NBKTupleBinaryInteger+Subtraction.swift │ │ ├── NBKTupleBinaryInteger.swift │ │ └── NBKTwinHeaded.swift ├── NBKDoubleWidthKit │ ├── NBKDoubleWidth+Addition+Digit.swift │ ├── NBKDoubleWidth+Addition.swift │ ├── NBKDoubleWidth+Bits.swift │ ├── NBKDoubleWidth+Comparisons.swift │ ├── NBKDoubleWidth+Complements.swift │ ├── NBKDoubleWidth+Data.swift │ ├── NBKDoubleWidth+Division+Digit.swift │ ├── NBKDoubleWidth+Division.swift │ ├── NBKDoubleWidth+Endianness.swift │ ├── NBKDoubleWidth+Literals.swift │ ├── NBKDoubleWidth+Logic.swift │ ├── NBKDoubleWidth+Multiplication+Digit.swift │ ├── NBKDoubleWidth+Multiplication.swift │ ├── NBKDoubleWidth+Numbers.swift │ ├── NBKDoubleWidth+Shifts.swift │ ├── NBKDoubleWidth+Strides.swift │ ├── NBKDoubleWidth+Subtraction+Digit.swift │ ├── NBKDoubleWidth+Subtraction.swift │ ├── NBKDoubleWidth+Text.swift │ ├── NBKDoubleWidth+Words+Collection.swift │ ├── NBKDoubleWidth+Words+Pointers.swift │ ├── NBKDoubleWidth+Words.swift │ └── NBKDoubleWidth.swift ├── NBKFlexibleWidthKit │ ├── IntXLOrUIntXL.swift │ ├── Models │ │ └── NBKFibonacciXL.swift │ ├── NBKFlexibleWidth+Addition+Digit.swift │ ├── NBKFlexibleWidth+Addition.swift │ ├── NBKFlexibleWidth+Bits.swift │ ├── NBKFlexibleWidth+Comparisons.swift │ ├── NBKFlexibleWidth+Complements.swift │ ├── NBKFlexibleWidth+Division+Digit.swift │ ├── NBKFlexibleWidth+Division.swift │ ├── NBKFlexibleWidth+Exponentiation.swift │ ├── NBKFlexibleWidth+Literals.swift │ ├── NBKFlexibleWidth+Logic.swift │ ├── NBKFlexibleWidth+Multiplication+Digit.swift │ ├── NBKFlexibleWidth+Multiplication.swift │ ├── NBKFlexibleWidth+Numbers.swift │ ├── NBKFlexibleWidth+Shifts.swift │ ├── NBKFlexibleWidth+Storage.swift │ ├── NBKFlexibleWidth+Subtraction+Digit.swift │ ├── NBKFlexibleWidth+Subtraction.swift │ ├── NBKFlexibleWidth+Text.swift │ ├── NBKFlexibleWidth+Update.swift │ ├── NBKFlexibleWidth+Words+Collection.swift │ ├── NBKFlexibleWidth+Words+Pointers.swift │ ├── NBKFlexibleWidth+Words+Uninitialized.swift │ ├── NBKFlexibleWidth+Words.swift │ └── NBKFlexibleWidth.swift └── Numberick │ ├── Documentation.docc │ └── Documentation.md │ └── Numberick.swift └── Tests ├── NBKCoreKitBenchmarks ├── Models │ ├── NBKChunkedInt.swift │ └── NBKPrimeSieve.swift ├── NBKCoreInteger+Bits.swift ├── NBKCoreInteger+Comparisons.swift ├── NBKCoreInteger+Division.swift ├── NBKCoreInteger+Numbers.swift ├── NBKCoreInteger+Shifts.swift ├── NBKCoreInteger+Words.swift └── Private │ ├── NBK+Sign.swift │ ├── NBKMaybeTwosComplement.swift │ ├── NBKProperBinaryInteger+GreatestCommonDivisor.swift │ ├── NBKProperBinaryInteger+ModularMultiplicativeInverse.swift │ ├── NBKProperBinaryInteger+Roots.swift │ ├── NBKStrictBinaryInteger+Bits.swift │ ├── NBKStrictSignedInteger+Comparisons.swift │ ├── NBKStrictUnsignedInteger+Addition.swift │ ├── NBKStrictUnsignedInteger+Comparisons.swift │ ├── NBKStrictUnsignedInteger+Subtraction.swift │ └── NBKSuccintInt.swift ├── NBKCoreKitTests ├── Models │ ├── NBKChunkedInt.swift │ ├── NBKEndianness.swift │ ├── NBKPrimeSieve.swift │ └── NBKStaticBigInt.swift ├── NBKCoreInteger+Addition.swift ├── NBKCoreInteger+Bits.swift ├── NBKCoreInteger+Comparisons.swift ├── NBKCoreInteger+Complements.swift ├── NBKCoreInteger+Division.swift ├── NBKCoreInteger+Multiplication.swift ├── NBKCoreInteger+Numbers.swift ├── NBKCoreInteger+Shifts.swift ├── NBKCoreInteger+Subtraction.swift ├── NBKCoreInteger+Text.swift ├── NBKCoreInteger+Words.swift ├── NBKCoreInteger.swift └── Private │ ├── NBK+BitCast.swift │ ├── NBK+Collection.swift │ ├── NBK+Data.swift │ ├── NBK+Endianness.swift │ ├── NBK+Sign.swift │ ├── NBKCyclicIterator.swift │ ├── NBKGuarantee.swift │ ├── NBKIntegerDescription+Components.swift │ ├── NBKIntegerDescription+Decoding.swift │ ├── NBKIntegerDescription+Encoding.swift │ ├── NBKIntegerDescription+RadixAlphabet.swift │ ├── NBKIntegerDescription+RadixSolution.swift │ ├── NBKIntegerSignMagnitude+Comparisons.swift │ ├── NBKMaybeTwosComplement.swift │ ├── NBKProperBinaryInteger+Division.swift │ ├── NBKProperBinaryInteger+GreatestCommonDivisor.swift │ ├── NBKProperBinaryInteger+ModularMultiplicativeInverse.swift │ ├── NBKProperBinaryInteger+Roots.swift │ ├── NBKStrictBinaryInteger+Bits.swift │ ├── NBKStrictBinaryInteger+Complements.swift │ ├── NBKStrictBinaryInteger+Shifts.swift │ ├── NBKStrictBinaryInteger+Words.swift │ ├── NBKStrictSignMagnitude+Comparisons.swift │ ├── NBKStrictSignedInteger+Comparisons.swift │ ├── NBKStrictUnsignedInteger+Addition.swift │ ├── NBKStrictUnsignedInteger+Comparisons.swift │ ├── NBKStrictUnsignedInteger+Division.swift │ ├── NBKStrictUnsignedInteger+Multiplication.swift │ ├── NBKStrictUnsignedInteger+Partition.swift │ ├── NBKStrictUnsignedInteger+Shifts.swift │ ├── NBKStrictUnsignedInteger+Subtraction.swift │ ├── NBKSuccinctInt.swift │ ├── NBKTupleBinaryInteger+Addition.swift │ ├── NBKTupleBinaryInteger+Collection.swift │ ├── NBKTupleBinaryInteger+Comparisons.swift │ ├── NBKTupleBinaryInteger+Complements.swift │ ├── NBKTupleBinaryInteger+Division.swift │ ├── NBKTupleBinaryInteger+Multiplication.swift │ ├── NBKTupleBinaryInteger+Subtraction.swift │ └── NBKTwinHeaded.swift ├── NBKDoubleWidthKitBenchmarks ├── NBKDoubleWidth+Addition.swift ├── NBKDoubleWidth+Bits.swift ├── NBKDoubleWidth+Comparisons.swift ├── NBKDoubleWidth+Complements.swift ├── NBKDoubleWidth+Division.swift ├── NBKDoubleWidth+Endianness.swift ├── NBKDoubleWidth+Logic.swift ├── NBKDoubleWidth+Multiplication.swift ├── NBKDoubleWidth+Numbers.swift ├── NBKDoubleWidth+Random.swift ├── NBKDoubleWidth+Shifts.swift ├── NBKDoubleWidth+Strides.swift ├── NBKDoubleWidth+Subtraction.swift ├── NBKDoubleWidth+Text.swift └── NBKDoubleWidth.swift ├── NBKDoubleWidthKitTests ├── NBKDoubleWidth+Addition.swift ├── NBKDoubleWidth+Bits.swift ├── NBKDoubleWidth+Comparisons.swift ├── NBKDoubleWidth+Complements.swift ├── NBKDoubleWidth+Division.swift ├── NBKDoubleWidth+Endianness.swift ├── NBKDoubleWidth+Literals.swift ├── NBKDoubleWidth+Logic.swift ├── NBKDoubleWidth+Multiplication.swift ├── NBKDoubleWidth+Numbers.swift ├── NBKDoubleWidth+Shifts.swift ├── NBKDoubleWidth+Strides.swift ├── NBKDoubleWidth+Subtraction.swift ├── NBKDoubleWidth+Text.swift ├── NBKDoubleWidth+Words.swift └── NBKDoubleWidth.swift ├── NBKFlexibleWidthKitBenchmarks ├── Models │ └── NBKFibonacciXL.swift ├── NBKFlexibleWidth+Addition.swift ├── NBKFlexibleWidth+Bits.swift ├── NBKFlexibleWidth+Comparisons.swift ├── NBKFlexibleWidth+Complements.swift ├── NBKFlexibleWidth+Division.swift ├── NBKFlexibleWidth+Exponentiation.swift ├── NBKFlexibleWidth+Logic.swift ├── NBKFlexibleWidth+Multiplication.swift ├── NBKFlexibleWidth+Numbers.swift ├── NBKFlexibleWidth+Shifts.swift ├── NBKFlexibleWidth+Subtraction.swift ├── NBKFlexibleWidth+Text.swift ├── NBKFlexibleWidth+Words.swift └── NBKFlexibleWidth.swift └── NBKFlexibleWidthKitTests ├── Models └── NBKFibonacciXL.swift ├── NBKFlexibleWidth+Addition.swift ├── NBKFlexibleWidth+Bits.swift ├── NBKFlexibleWidth+Comparisons.swift ├── NBKFlexibleWidth+Complements.swift ├── NBKFlexibleWidth+Division.swift ├── NBKFlexibleWidth+Exponentiation.swift ├── NBKFlexibleWidth+Literals.swift ├── NBKFlexibleWidth+Logic.swift ├── NBKFlexibleWidth+Multiplication.swift ├── NBKFlexibleWidth+Numbers.swift ├── NBKFlexibleWidth+Shifts.swift ├── NBKFlexibleWidth+Subtraction.swift ├── NBKFlexibleWidth+Text.swift ├── NBKFlexibleWidth+Update.swift ├── NBKFlexibleWidth+Words.swift └── NBKFlexibleWidth.swift /.github/workflows/publish-specifications-on-cocoapods.yml: -------------------------------------------------------------------------------- 1 | name: Publish specifications on CocoaPods 2 | 3 | on: 4 | workflow_dispatch: 5 | release: 6 | types: [created] 7 | 8 | jobs: 9 | publish: 10 | runs-on: macos-13 11 | steps: 12 | - name: Checkout 13 | uses: actions/checkout@v3 14 | 15 | - name: Select Xcode 14.3.1 16 | run: sudo xcode-select --switch /Applications/Xcode_14.3.1.app 17 | 18 | - name: Publish 'Numberick-NBKCoreKit.podspec' 19 | run: pod trunk push --allow-warnings --synchronous Numberick-NBKCoreKit.podspec 20 | env: 21 | COCOAPODS_TRUNK_TOKEN: ${{ secrets.COCOAPODS_TRUNK_TOKEN }} 22 | 23 | - name: Publish 'Numberick-NBKDoubleWidthKit.podspec' 24 | run: pod trunk push --allow-warnings --synchronous Numberick-NBKDoubleWidthKit.podspec 25 | env: 26 | COCOAPODS_TRUNK_TOKEN: ${{ secrets.COCOAPODS_TRUNK_TOKEN }} 27 | 28 | - name: Publish 'Numberick.podspec' 29 | run: pod trunk push --allow-warnings --synchronous Numberick.podspec 30 | env: 31 | COCOAPODS_TRUNK_TOKEN: ${{ secrets.COCOAPODS_TRUNK_TOKEN }} 32 | -------------------------------------------------------------------------------- /.github/workflows/run-lint-on-cocoapods-specifications.yml: -------------------------------------------------------------------------------- 1 | name: Run lint on CocoaPods specifications 2 | 3 | on: 4 | workflow_dispatch: 5 | push: 6 | tags: 7 | - 'CocoaPods-v[0-9]+.[0-9]+.[0-9]+' 8 | - 'CocoaPods-v[0-9]+.[0-9]+.[0-9]+-*' 9 | 10 | jobs: 11 | macos: 12 | strategy: 13 | matrix: 14 | element: 15 | - xcode: '14.3' 16 | machine: macos-13 17 | 18 | runs-on: ${{ matrix.element.machine }} 19 | 20 | steps: 21 | - name: Checkout 22 | uses: actions/checkout@v3 23 | - name: Select Xcode ${{ matrix.element.xcode }} 24 | run: sudo xcode-select --switch /Applications/Xcode_${{ matrix.element.xcode }}.app 25 | - name: Run lint on 'Numberick.podspec' 26 | run: | 27 | DEPENDENCIES='' 28 | DEPENDENCIES+=Numberick-NBKCoreKit.podspec, 29 | DEPENDENCIES+=Numberick-NBKDoubleWidthKit.podspec, 30 | pod lib lint Numberick.podspec \ 31 | --allow-warnings \ 32 | --fail-fast \ 33 | --include-podspecs=\{$DEPENDENCIES\} 34 | -------------------------------------------------------------------------------- /.github/workflows/run-tests-on-linux.yml: -------------------------------------------------------------------------------- 1 | name: Run tests on Linux 2 | 3 | on: 4 | workflow_dispatch: 5 | push: 6 | branches: [main] 7 | pull_request: 8 | branches: [main] 9 | 10 | jobs: 11 | linux: 12 | strategy: 13 | matrix: 14 | element: 15 | - swift: '5.7' 16 | - swift: '5.8' 17 | - swift: '5.9' 18 | 19 | runs-on: ubuntu-latest 20 | container: 21 | image: swift:${{ matrix.element.swift }} 22 | 23 | steps: 24 | - name: Checkout 25 | uses: actions/checkout@v3 26 | - name: Test 27 | run: swift test 28 | -------------------------------------------------------------------------------- /.github/workflows/run-tests-on-macos.yml: -------------------------------------------------------------------------------- 1 | name: Run tests on macOS 2 | 3 | on: 4 | workflow_dispatch: 5 | push: 6 | branches: [main] 7 | pull_request: 8 | branches: [main] 9 | 10 | jobs: 11 | macos: 12 | strategy: 13 | matrix: 14 | element: 15 | - xcode: '14.2' 16 | machine: macos-12 17 | - xcode: '14.3' 18 | machine: macos-13 19 | - xcode: '15.0' 20 | machine: macos-14 21 | 22 | runs-on: ${{ matrix.element.machine }} 23 | 24 | steps: 25 | - name: Checkout 26 | uses: actions/checkout@v3 27 | - name: Select Xcode ${{ matrix.element.xcode }} 28 | run: sudo xcode-select --switch /Applications/Xcode_${{ matrix.element.xcode }}.app 29 | - name: Test 30 | run: xcrun swift test 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/config/registries.json 8 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 9 | .netrc 10 | -------------------------------------------------------------------------------- /.swiftpm/NBKCoreKit-Benchmarks.xctestplan: -------------------------------------------------------------------------------- 1 | { 2 | "configurations" : [ 3 | { 4 | "id" : "2BBD9A51-D890-4B97-AED4-B9962867DF57", 5 | "name" : "Main", 6 | "options" : { 7 | 8 | } 9 | } 10 | ], 11 | "defaultOptions" : { 12 | "codeCoverage" : false 13 | }, 14 | "testTargets" : [ 15 | { 16 | "skippedTests" : [ 17 | "NBKPrimeSieveBenchmarks\/testNoLoop1E6()", 18 | "NBKPrimeSieveBenchmarks\/testNoLoop1E7()", 19 | "NBKPrimeSieveBenchmarks\/testNoLoop1E8()", 20 | "NBKPrimeSieveBenchmarks\/testNoLoop1E9()" 21 | ], 22 | "target" : { 23 | "containerPath" : "container:", 24 | "identifier" : "NBKCoreKitBenchmarks", 25 | "name" : "NBKCoreKitBenchmarks" 26 | } 27 | } 28 | ], 29 | "version" : 1 30 | } 31 | -------------------------------------------------------------------------------- /.swiftpm/NBKCoreKit.xctestplan: -------------------------------------------------------------------------------- 1 | { 2 | "configurations" : [ 3 | { 4 | "id" : "1DC89388-5756-4D8F-B634-E1EB75FCCBF2", 5 | "name" : "Main", 6 | "options" : { 7 | 8 | } 9 | } 10 | ], 11 | "defaultOptions" : { 12 | "codeCoverage" : { 13 | "targets" : [ 14 | { 15 | "containerPath" : "container:", 16 | "identifier" : "NBKCoreKit", 17 | "name" : "NBKCoreKit" 18 | } 19 | ] 20 | } 21 | }, 22 | "testTargets" : [ 23 | { 24 | "target" : { 25 | "containerPath" : "container:", 26 | "identifier" : "NBKCoreKitTests", 27 | "name" : "NBKCoreKitTests" 28 | } 29 | } 30 | ], 31 | "version" : 1 32 | } 33 | -------------------------------------------------------------------------------- /.swiftpm/NBKDoubleWidthKit-Benchmarks.xctestplan: -------------------------------------------------------------------------------- 1 | { 2 | "configurations" : [ 3 | { 4 | "id" : "2BBD9A51-D890-4B97-AED4-B9962867DF57", 5 | "name" : "Main", 6 | "options" : { 7 | 8 | } 9 | } 10 | ], 11 | "defaultOptions" : { 12 | "codeCoverage" : false 13 | }, 14 | "testTargets" : [ 15 | { 16 | "skippedTests" : [ 17 | "NBKDoubleWidthBenchmarksOnTextAsInt256\/testDecodingUsingSwiftStdlibRadix10()", 18 | "NBKDoubleWidthBenchmarksOnTextAsInt256\/testDecodingUsingSwiftStdlibRadix16()", 19 | "NBKDoubleWidthBenchmarksOnTextAsInt256\/testEncodingUsingSwiftStdlibRadix10()", 20 | "NBKDoubleWidthBenchmarksOnTextAsInt256\/testEncodingUsingSwiftStdlibRadix16()", 21 | "NBKDoubleWidthBenchmarksOnTextAsUInt256\/testDecodingUsingSwiftStdlibRadix10()", 22 | "NBKDoubleWidthBenchmarksOnTextAsUInt256\/testDecodingUsingSwiftStdlibRadix16()", 23 | "NBKDoubleWidthBenchmarksOnTextAsUInt256\/testEncodingUsingSwiftStdlibRadix10()", 24 | "NBKDoubleWidthBenchmarksOnTextAsUInt256\/testEncodingUsingSwiftStdlibRadix16()" 25 | ], 26 | "target" : { 27 | "containerPath" : "container:", 28 | "identifier" : "NBKDoubleWidthKitBenchmarks", 29 | "name" : "NBKDoubleWidthKitBenchmarks" 30 | } 31 | } 32 | ], 33 | "version" : 1 34 | } 35 | -------------------------------------------------------------------------------- /.swiftpm/NBKDoubleWidthKit.xctestplan: -------------------------------------------------------------------------------- 1 | { 2 | "configurations" : [ 3 | { 4 | "id" : "BE1177B6-FD49-422D-91EC-6BD02E019A3D", 5 | "name" : "Main", 6 | "options" : { 7 | 8 | } 9 | } 10 | ], 11 | "defaultOptions" : { 12 | "codeCoverage" : { 13 | "targets" : [ 14 | { 15 | "containerPath" : "container:", 16 | "identifier" : "NBKDoubleWidthKit", 17 | "name" : "NBKDoubleWidthKit" 18 | } 19 | ] 20 | } 21 | }, 22 | "testTargets" : [ 23 | { 24 | "target" : { 25 | "containerPath" : "container:", 26 | "identifier" : "NBKDoubleWidthKitTests", 27 | "name" : "NBKDoubleWidthKitTests" 28 | } 29 | } 30 | ], 31 | "version" : 1 32 | } 33 | -------------------------------------------------------------------------------- /.swiftpm/NBKFlexibleWidthKit-Benchmarks.xctestplan: -------------------------------------------------------------------------------- 1 | { 2 | "configurations" : [ 3 | { 4 | "id" : "2BBD9A51-D890-4B97-AED4-B9962867DF57", 5 | "name" : "Main", 6 | "options" : { 7 | 8 | } 9 | } 10 | ], 11 | "defaultOptions" : { 12 | "codeCoverage" : false 13 | }, 14 | "testTargets" : [ 15 | { 16 | "skippedTests" : [ 17 | "NBKFibonacciXLBenchmarks\/testNoLoop10000000()", 18 | "NBKFlexibleWidthBenchmarksOnExponentiationAsUIntXL\/test7RaisedToPrime777()", 19 | "NBKFlexibleWidthBenchmarksOnExponentiationAsUIntXL\/testNoLoop5RaisedToPrime22222()", 20 | "NBKFlexibleWidthBenchmarksOnExponentiationAsUIntXL\/testNoLoop5RaisedToPrime33333()", 21 | "NBKFlexibleWidthBenchmarksOnExponentiationAsUIntXL\/testNoLoop5RaisedToPrime55555()", 22 | "NBKFlexibleWidthBenchmarksOnExponentiationAsUIntXL\/testNoLoop7RaisedToPrime77777()", 23 | "NBKFlexibleWidthBenchmarksOnTextAsUIntXL\/testEncodingUsingSwiftStdlibRadix10()", 24 | "NBKFlexibleWidthBenchmarksOnTextAsUIntXL\/testEncodingUsingSwiftStdlibRadix16()" 25 | ], 26 | "target" : { 27 | "containerPath" : "container:", 28 | "identifier" : "NBKFlexibleWidthKitBenchmarks", 29 | "name" : "NBKFlexibleWidthKitBenchmarks" 30 | } 31 | } 32 | ], 33 | "version" : 1 34 | } 35 | -------------------------------------------------------------------------------- /.swiftpm/NBKFlexibleWidthKit.xctestplan: -------------------------------------------------------------------------------- 1 | { 2 | "configurations" : [ 3 | { 4 | "id" : "6FC9897F-2BF3-4836-8372-787CE5195F87", 5 | "name" : "Main", 6 | "options" : { 7 | 8 | } 9 | } 10 | ], 11 | "defaultOptions" : { 12 | "codeCoverage" : { 13 | "targets" : [ 14 | { 15 | "containerPath" : "container:", 16 | "identifier" : "NBKFlexibleWidthKit", 17 | "name" : "NBKFlexibleWidthKit" 18 | } 19 | ] 20 | } 21 | }, 22 | "testTargets" : [ 23 | { 24 | "target" : { 25 | "containerPath" : "container:", 26 | "identifier" : "NBKFlexibleWidthKitTests", 27 | "name" : "NBKFlexibleWidthKitTests" 28 | } 29 | } 30 | ], 31 | "version" : 1 32 | } 33 | -------------------------------------------------------------------------------- /.swiftpm/Numberick-Benchmarks.xctestplan: -------------------------------------------------------------------------------- 1 | { 2 | "configurations" : [ 3 | { 4 | "id" : "2BBD9A51-D890-4B97-AED4-B9962867DF57", 5 | "name" : "Main", 6 | "options" : { 7 | 8 | } 9 | } 10 | ], 11 | "defaultOptions" : { 12 | "codeCoverage" : false 13 | }, 14 | "testTargets" : [ 15 | { 16 | "skippedTests" : [ 17 | "NBKPrimeSieveBenchmarks\/testNoLoop1E6()", 18 | "NBKPrimeSieveBenchmarks\/testNoLoop1E7()", 19 | "NBKPrimeSieveBenchmarks\/testNoLoop1E8()", 20 | "NBKPrimeSieveBenchmarks\/testNoLoop1E9()" 21 | ], 22 | "target" : { 23 | "containerPath" : "container:", 24 | "identifier" : "NBKCoreKitBenchmarks", 25 | "name" : "NBKCoreKitBenchmarks" 26 | } 27 | }, 28 | { 29 | "skippedTests" : [ 30 | "NBKDoubleWidthBenchmarksOnTextAsInt256\/testDecodingUsingSwiftStdlibRadix10()", 31 | "NBKDoubleWidthBenchmarksOnTextAsInt256\/testDecodingUsingSwiftStdlibRadix16()", 32 | "NBKDoubleWidthBenchmarksOnTextAsInt256\/testEncodingUsingSwiftStdlibRadix10()", 33 | "NBKDoubleWidthBenchmarksOnTextAsInt256\/testEncodingUsingSwiftStdlibRadix16()", 34 | "NBKDoubleWidthBenchmarksOnTextAsUInt256\/testDecodingUsingSwiftStdlibRadix10()", 35 | "NBKDoubleWidthBenchmarksOnTextAsUInt256\/testDecodingUsingSwiftStdlibRadix16()", 36 | "NBKDoubleWidthBenchmarksOnTextAsUInt256\/testEncodingUsingSwiftStdlibRadix10()", 37 | "NBKDoubleWidthBenchmarksOnTextAsUInt256\/testEncodingUsingSwiftStdlibRadix16()" 38 | ], 39 | "target" : { 40 | "containerPath" : "container:", 41 | "identifier" : "NBKDoubleWidthKitBenchmarks", 42 | "name" : "NBKDoubleWidthKitBenchmarks" 43 | } 44 | } 45 | ], 46 | "version" : 1 47 | } 48 | -------------------------------------------------------------------------------- /.swiftpm/Numberick.xctestplan: -------------------------------------------------------------------------------- 1 | { 2 | "configurations" : [ 3 | { 4 | "id" : "6FC9897F-2BF3-4836-8372-787CE5195F87", 5 | "name" : "Main", 6 | "options" : { 7 | 8 | } 9 | } 10 | ], 11 | "defaultOptions" : { 12 | "codeCoverage" : { 13 | "targets" : [ 14 | { 15 | "containerPath" : "container:", 16 | "identifier" : "NBKCoreKit", 17 | "name" : "NBKCoreKit" 18 | }, 19 | { 20 | "containerPath" : "container:", 21 | "identifier" : "NBKDoubleWidthKit", 22 | "name" : "NBKDoubleWidthKit" 23 | } 24 | ] 25 | } 26 | }, 27 | "testTargets" : [ 28 | { 29 | "target" : { 30 | "containerPath" : "container:", 31 | "identifier" : "NBKCoreKitTests", 32 | "name" : "NBKCoreKitTests" 33 | } 34 | }, 35 | { 36 | "target" : { 37 | "containerPath" : "container:", 38 | "identifier" : "NBKDoubleWidthKitTests", 39 | "name" : "NBKDoubleWidthKitTests" 40 | } 41 | } 42 | ], 43 | "version" : 1 44 | } 45 | -------------------------------------------------------------------------------- /.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.swiftpm/xcode/xcshareddata/xcschemes/NBKCoreKit-Benchmarks.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 34 | 37 | 38 | 39 | 40 | 50 | 51 | 57 | 58 | 64 | 65 | 66 | 67 | 69 | 70 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /.swiftpm/xcode/xcshareddata/xcschemes/NBKCoreKit.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 34 | 35 | 36 | 37 | 47 | 48 | 54 | 55 | 61 | 62 | 63 | 64 | 66 | 67 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /.swiftpm/xcode/xcshareddata/xcschemes/NBKDoubleWidthKit-Benchmarks.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 34 | 37 | 38 | 39 | 40 | 50 | 51 | 57 | 58 | 64 | 65 | 66 | 67 | 69 | 70 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /.swiftpm/xcode/xcshareddata/xcschemes/NBKDoubleWidthKit.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 34 | 35 | 36 | 37 | 47 | 48 | 54 | 55 | 61 | 62 | 63 | 64 | 66 | 67 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /.swiftpm/xcode/xcshareddata/xcschemes/NBKFlexibleWidthKit-Benchmarks.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 34 | 37 | 38 | 39 | 40 | 50 | 51 | 57 | 58 | 64 | 65 | 66 | 67 | 69 | 70 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /.swiftpm/xcode/xcshareddata/xcschemes/NBKFlexibleWidthKit.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 34 | 35 | 36 | 37 | 47 | 48 | 54 | 55 | 61 | 62 | 63 | 64 | 66 | 67 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /.utilities/update-gh-pages-docs-no-push.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -eu 2 | #=-----------------------------------------------------------------------------= 3 | # This source file is part of the Numberick open source project. 4 | # 5 | # Copyright (c) 2023 Oscar Byström Ericsson 6 | # Licensed under Apache License, Version 2.0 7 | # 8 | # See http://www.apache.org/licenses/LICENSE-2.0 for license information. 9 | #=-----------------------------------------------------------------------------= 10 | # This script requires an orphaned gh-pages branch and the following structure: 11 | # 12 | # PROJECT/ 13 | # │ 14 | # ├─── DIRECTORY/ 15 | # │ │ 16 | # │ └─── SCRIPT.sh 17 | # │ 18 | # └─── .swiftpm/xcode/xcshareddata/PROJECT.xcscheme 19 | #=-----------------------------------------------------------------------------= 20 | 21 | # Variables 22 | 23 | PROJECT_NAME="Numberick" 24 | XCODE_SCHEME_NAME=$PROJECT_NAME 25 | 26 | PROJECT_PATH=$(dirname $(dirname $(realpath $0))) 27 | CURRENT_COMMIT_HASH=$(git rev-parse --short HEAD) 28 | 29 | export DOCC_JSON_PRETTYPRINT="YES" # nondeterministic :( 30 | 31 | # Actions 32 | 33 | cd $PROJECT_PATH 34 | 35 | xcodebuild docbuild \ 36 | -scheme $XCODE_SCHEME_NAME \ 37 | -derivedDataPath "docc" \ 38 | -destination 'generic/platform=iOS' 39 | 40 | DOCC_ARCHIVE_PATH=$(find "docc" -name "$XCODE_SCHEME_NAME.doccarchive" | head -n 1) 41 | 42 | git worktree add --checkout "gh-pages" 43 | 44 | $(xcrun --find docc) process-archive transform-for-static-hosting $DOCC_ARCHIVE_PATH \ 45 | --output-path "gh-pages/docs" \ 46 | --hosting-base-path $PROJECT_NAME 47 | 48 | rm -rf docc 49 | cd gh-pages 50 | git add docs 51 | git commit -m "GitHub Pages. DocC. $CURRENT_COMMIT_HASH." 52 | cd .. 53 | git worktree remove gh-pages 54 | -------------------------------------------------------------------------------- /.utilities/xcodebuild-clean-build-platforms.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -eu 2 | #=-----------------------------------------------------------------------------= 3 | # This source file is part of the Numberick open source project. 4 | # 5 | # Copyright (c) 2023 Oscar Byström Ericsson 6 | # Licensed under Apache License, Version 2.0 7 | # 8 | # See http://www.apache.org/licenses/LICENSE-2.0 for license information. 9 | #=-----------------------------------------------------------------------------= 10 | # This script requires the following structure: 11 | # 12 | # PROJECT/ 13 | # │ 14 | # ├─── DIRECTORY/ 15 | # │ │ 16 | # │ └─── SCRIPT.sh 17 | # │ 18 | # └─── .swiftpm/xcode/xcshareddata/PROJECT.xcscheme 19 | #=-----------------------------------------------------------------------------= 20 | 21 | # Variables 22 | 23 | PROJECT_NAME="Numberick" 24 | PROJECT_PATH=$(dirname $(dirname $(realpath $0))) 25 | XCODE_SCHEME_NAME=$PROJECT_NAME 26 | 27 | # Actions 28 | 29 | cd $PROJECT_PATH 30 | 31 | xcodebuild clean build \ 32 | -scheme $XCODE_SCHEME_NAME \ 33 | -destination "generic/platform=iOS" \ 34 | -destination "generic/platform=macOS" \ 35 | -destination "generic/platform=tvOS" \ 36 | -destination "generic/platform=watchOS" 37 | -------------------------------------------------------------------------------- /Numberick-NBKCoreKit.podspec: -------------------------------------------------------------------------------- 1 | #=-----------------------------------------------------------------------------= 2 | # This source file is part of the Numberick open source project. 3 | # 4 | # Copyright (c) 2023 Oscar Byström Ericsson 5 | # Licensed under Apache License, Version 2.0 6 | # 7 | # See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | #=-----------------------------------------------------------------------------= 9 | 10 | #*=============================================================================* 11 | # MARK: * NBK x Core Kit 12 | #*=============================================================================* 13 | 14 | Pod::Spec.new do |spec| 15 | spec.version = "0.17.0" 16 | spec.module_name = "NBKCoreKit" 17 | spec.name = "Numberick-#{spec.module_name}" 18 | spec.summary = "A new protocol hierarchy that refines Swift's standard library." 19 | 20 | spec.license = { :type => "Apache-2.0", :file => "LICENSE" } 21 | spec.author = { "Oscar Byström Ericsson" => "oscbyspro@protonmail.com" } 22 | 23 | spec.homepage = "https://github.com/oscbyspro/Numberick" 24 | spec.readme = "https://raw.githubusercontent.com/oscbyspro/Numberick/v#{spec.version}/README.md" 25 | spec.documentation_url = "https://oscbyspro.github.io/Numberick/documentation/numberick/" 26 | 27 | spec.source = { :git => "https://github.com/oscbyspro/Numberick.git", :tag => "CocoaPods-v#{spec.version}" } 28 | spec.source_files = "Sources/#{spec.module_name}/**/*.swift" 29 | 30 | #=-------------------------------------------------------------------------= 31 | # MARK: Requirements 32 | #=-------------------------------------------------------------------------= 33 | 34 | spec.swift_version = "5.7" 35 | spec.platforms = { :ios => "14.0", :osx => "11.0", :tvos => "14.0", :watchos => "7.0" } 36 | 37 | #=-------------------------------------------------------------------------= 38 | # MARK: Tests 39 | #=-------------------------------------------------------------------------= 40 | 41 | spec.test_spec "Tests" do |test_spec| 42 | test_spec.source_files = "Tests/#{spec.module_name}Tests/**/*.swift" 43 | test_spec.platforms = { :ios => "14.0", :osx => "11.0", :tvos => "14.0" } 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /Numberick-NBKDoubleWidthKit.podspec: -------------------------------------------------------------------------------- 1 | #=-----------------------------------------------------------------------------= 2 | # This source file is part of the Numberick open source project. 3 | # 4 | # Copyright (c) 2023 Oscar Byström Ericsson 5 | # Licensed under Apache License, Version 2.0 6 | # 7 | # See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | #=-----------------------------------------------------------------------------= 9 | 10 | #*=============================================================================* 11 | # MARK: * NBK x Double Width Kit 12 | #*=============================================================================* 13 | 14 | Pod::Spec.new do |spec| 15 | spec.version = "0.17.0" 16 | spec.module_name = "NBKDoubleWidthKit" 17 | spec.name = "Numberick-#{spec.module_name}" 18 | spec.summary = "A composable, large, fixed-width, two's complement, binary integer." 19 | 20 | spec.license = { :type => "Apache-2.0", :file => "LICENSE" } 21 | spec.author = { "Oscar Byström Ericsson" => "oscbyspro@protonmail.com" } 22 | 23 | spec.homepage = "https://github.com/oscbyspro/Numberick" 24 | spec.readme = "https://raw.githubusercontent.com/oscbyspro/Numberick/v#{spec.version}/README.md" 25 | spec.documentation_url = "https://oscbyspro.github.io/Numberick/documentation/numberick/" 26 | 27 | spec.source = { :git => "https://github.com/oscbyspro/Numberick.git", :tag => "CocoaPods-v#{spec.version}" } 28 | spec.source_files = "Sources/#{spec.module_name}/**/*.swift" 29 | 30 | #=-------------------------------------------------------------------------= 31 | # MARK: Requirements 32 | #=-------------------------------------------------------------------------= 33 | 34 | spec.swift_version = "5.7" 35 | spec.platforms = { :ios => "14.0", :osx => "11.0", :tvos => "14.0", :watchos => "7.0" } 36 | 37 | #=-------------------------------------------------------------------------= 38 | # MARK: Tests 39 | #=-------------------------------------------------------------------------= 40 | 41 | spec.test_spec "Tests" do |test_spec| 42 | test_spec.source_files = "Tests/#{spec.module_name}Tests/**/*.swift" 43 | test_spec.platforms = { :ios => "14.0", :osx => "11.0", :tvos => "14.0" } 44 | end 45 | 46 | #=-------------------------------------------------------------------------= 47 | # MARK: Dependencies 48 | #=-------------------------------------------------------------------------= 49 | 50 | spec.dependency "Numberick-NBKCoreKit", "#{spec.version}" 51 | end 52 | -------------------------------------------------------------------------------- /Numberick.podspec: -------------------------------------------------------------------------------- 1 | #=-----------------------------------------------------------------------------= 2 | # This source file is part of the Numberick open source project. 3 | # 4 | # Copyright (c) 2023 Oscar Byström Ericsson 5 | # Licensed under Apache License, Version 2.0 6 | # 7 | # See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | #=-----------------------------------------------------------------------------= 9 | 10 | #*=============================================================================* 11 | # MARK: * NBK 12 | #*=============================================================================* 13 | 14 | Pod::Spec.new do |spec| 15 | spec.version = "0.17.0" 16 | spec.module_name = "Numberick" 17 | spec.name = "Numberick" 18 | spec.summary = "✨ An arithmagick overhaul in Swift." 19 | 20 | spec.license = { :type => "Apache-2.0", :file => "LICENSE" } 21 | spec.author = { "Oscar Byström Ericsson" => "oscbyspro@protonmail.com" } 22 | 23 | spec.homepage = "https://github.com/oscbyspro/Numberick" 24 | spec.readme = "https://raw.githubusercontent.com/oscbyspro/Numberick/v#{spec.version}/README.md" 25 | spec.documentation_url = "https://oscbyspro.github.io/Numberick/documentation/numberick/" 26 | 27 | spec.source = { :git => "https://github.com/oscbyspro/Numberick.git", :tag => "CocoaPods-v#{spec.version}" } 28 | spec.source_files = "Sources/#{spec.module_name}/**/*.swift" 29 | 30 | #=-------------------------------------------------------------------------= 31 | # MARK: Requirements 32 | #=-------------------------------------------------------------------------= 33 | 34 | spec.swift_version = "5.7" 35 | spec.platforms = { :ios => "14.0", :osx => "11.0", :tvos => "14.0", :watchos => "7.0" } 36 | 37 | #=-------------------------------------------------------------------------= 38 | # MARK: Tests 39 | #=-------------------------------------------------------------------------= 40 | 41 | spec.test_spec "Tests" do |test_spec| 42 | test_spec.source_files = "Tests/*Tests/**/*.swift" 43 | test_spec.platforms = { :ios => "14.0", :osx => "11.0", :tvos => "14.0" } 44 | end 45 | 46 | #=-------------------------------------------------------------------------= 47 | # MARK: Dependencies 48 | #=-------------------------------------------------------------------------= 49 | 50 | spec.dependency "Numberick-NBKCoreKit", "#{spec.version}" 51 | spec.dependency "Numberick-NBKDoubleWidthKit", "#{spec.version}" 52 | end 53 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Aliases.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Aliases 12 | //*============================================================================* 13 | 14 | /// A high and a low value. 15 | public typealias HL = (high: H, low: L) 16 | 17 | /// An index and an overflow indicator. 18 | public typealias IO = (index: I, overflow: Bool) 19 | 20 | /// A low and a high value. 21 | public typealias LH = (low: L, high: H) 22 | 23 | /// A partial value and an overflow indicator. 24 | public typealias PVO = (partialValue: PV, overflow: Bool) 25 | 26 | /// A quotient and a remainder, relating to division. 27 | public typealias QR = (quotient: Q, remainder: R) 28 | 29 | /// A sign and a magnitude. 30 | public typealias SM = (sign: FloatingPointSign, magnitude: M) 31 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Models/NBKEndianness.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Endianness 12 | //*============================================================================* 13 | 14 | /// An enumeration of little and big endianness. 15 | /// 16 | /// ### Static vs Dynamic 17 | /// 18 | /// Some algorithms differ depending on endianness. Generic type parameterization 19 | /// can express the difference, but dynamic solutions are often viable. This type 20 | /// encurages the latter. 21 | /// 22 | @frozen public enum NBKEndianness: Hashable, Sendable { 23 | 24 | //=------------------------------------------------------------------------= 25 | // MARK: State 26 | //=------------------------------------------------------------------------= 27 | 28 | /// A value representing a least-to-most-significant byte order. 29 | case little 30 | 31 | /// A value representing a most-to-least-significant byte order. 32 | case big 33 | 34 | //=------------------------------------------------------------------------= 35 | // MARK: Initializers 36 | //=------------------------------------------------------------------------= 37 | 38 | /// Returns the current system's byte order. 39 | @inlinable public static var system: Self { 40 | #if _endian(little) 41 | return .little 42 | #elseif _endian(big) 43 | return .big 44 | #endif 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBK+BlackHole.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Black Hole 12 | //*============================================================================* 13 | 14 | extension NBK { 15 | 16 | //=------------------------------------------------------------------------= 17 | // MARK: Utilities 18 | //=------------------------------------------------------------------------= 19 | 20 | /// Opaquely uses the argument. 21 | /// 22 | /// Optimizer folklore has it that its secrets are revealed only to the supreme architect. 23 | /// 24 | @_optimize(none) public static func blackHole(_ x: T) { } 25 | 26 | /// Opaquely returns the argument. 27 | /// 28 | /// Optimizer folklore has it that its secrets are revealed only to the supreme architect. 29 | /// 30 | @_optimize(none) public static func blackHoleIdentity(_ x: T) -> T { x } 31 | 32 | /// Opaquely mutates the argument. 33 | /// 34 | /// Optimizer folklore has it that its secrets are revealed only to the supreme architect. 35 | /// 36 | @_optimize(none) public static func blackHoleInoutIdentity(_ x: inout T) { } 37 | } 38 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBK+Endianness.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Endianess 12 | //*============================================================================* 13 | 14 | extension NBK { 15 | 16 | //=------------------------------------------------------------------------= 17 | // MARK: Utilities 18 | //=------------------------------------------------------------------------= 19 | 20 | @_transparent public static var isBigEndian: Bool { 21 | #if _endian(big) 22 | return true 23 | #else 24 | return false 25 | #endif 26 | } 27 | 28 | @_transparent public static var isLittleEndian: Bool { 29 | #if _endian(little) 30 | return true 31 | #else 32 | return false 33 | #endif 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBK+Messages.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Messages 12 | //*============================================================================* 13 | 14 | extension NBK { 15 | 16 | //=------------------------------------------------------------------------= 17 | // MARK: Utilities 18 | //=------------------------------------------------------------------------= 19 | 20 | /// A message describing the location of an overflow error. 21 | @inlinable public static func callsiteOverflowInfo( 22 | function: StaticString = #function, file: StaticString = #file, line: UInt = #line) -> String { 23 | "overflow in \(function) at \(file):\(line)" 24 | } 25 | 26 | /// A message describing the location of an out-of-bounds error. 27 | @inlinable public static func callsiteOutOfBoundsInfo( 28 | function: StaticString = #function, file: StaticString = #file, line: UInt = #line) -> String { 29 | "out of bounds in \(function) at \(file):\(line)" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBK+Swift.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Swift 12 | //*============================================================================* 13 | 14 | extension NBK { 15 | 16 | //=------------------------------------------------------------------------= 17 | // MARK: Utilities 18 | //=------------------------------------------------------------------------= 19 | 20 | /// Returns the given `value` as some `Swift.BinaryInteger`. 21 | @inlinable public static func someSwiftBinaryInteger( 22 | _ value: T) -> some Swift.BinaryInteger where T: Swift.BinaryInteger { 23 | value 24 | } 25 | 26 | /// Returns the given `value` as some `Swift.FixedWidthInteger`. 27 | @inlinable public static func someSwiftFixedWidthInteger( 28 | _ value: T) -> some Swift.FixedWidthInteger where T: Swift.FixedWidthInteger { 29 | value 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKIntegerDescription.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Integer Description 12 | //*============================================================================* 13 | 14 | extension NBK { 15 | 16 | /// A namespace for decoding and encoding integer descriptions. 17 | /// 18 | /// - Note: Its algorithms commonly interpret byte sequences as ASCII text. 19 | /// 20 | @frozen public enum IntegerDescription { } 21 | } 22 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKIntegerSignMagnitude+Comparisons.swift: -------------------------------------------------------------------------------- 1 | 2 | //=----------------------------------------------------------------------------= 3 | // This source file is part of the Numberick open source project. 4 | // 5 | // Copyright (c) 2023 Oscar Byström Ericsson 6 | // Licensed under Apache License, Version 2.0 7 | // 8 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 9 | //=----------------------------------------------------------------------------= 10 | 11 | //*============================================================================* 12 | // MARK: * NBK x Integer Sign Magnitude x Comparisons 13 | //*============================================================================* 14 | 15 | extension NBK.IntegerSignMagnitude { 16 | 17 | //=------------------------------------------------------------------------= 18 | // MARK: Utilities 19 | //=------------------------------------------------------------------------= 20 | 21 | /// Returns whether `components` is less than `zero`. 22 | @inlinable public static func isLessThanZero(_ components: Components) -> Bool { 23 | components.sign == Sign.minus && !components.magnitude.isZero 24 | } 25 | 26 | /// Returns whether `components` is more than `zero`. 27 | @inlinable public static func isMoreThanZero(_ components: Components) -> Bool { 28 | components.sign == Sign.plus && !components.magnitude.isZero 29 | } 30 | 31 | /// A three-way comparison of `components` against `zero`. 32 | @inlinable public static func signum(_ components: Components) -> Int { 33 | components.magnitude.isZero ? 0 : components.sign == Sign.plus ? 1 : -1 34 | } 35 | } 36 | 37 | //=----------------------------------------------------------------------------= 38 | // MARK: + Composition 39 | //=----------------------------------------------------------------------------= 40 | 41 | extension NBK.IntegerSignMagnitude { 42 | 43 | //=------------------------------------------------------------------------= 44 | // MARK: Utilities 45 | //=------------------------------------------------------------------------= 46 | 47 | /// A three-way sign and magnitude comparison using the given magnitude operation. 48 | /// 49 | /// - Parameters: 50 | /// - compare: A three-way magnitude comparison. 51 | /// 52 | /// - Returns: One of the following values: -1 (less), 0 (same), or 1 (more). 53 | /// 54 | @inlinable public static func compare( 55 | _ lhs: Components, to rhs: NBK.IntegerSignMagnitude.Components, using compare: (Magnitude, Other) -> Int) -> Int { 56 | let positive: Int 57 | 58 | if lhs.sign == rhs.sign { 59 | positive = compare(lhs.magnitude, rhs.magnitude) 60 | } else { 61 | positive = lhs.magnitude.isZero && rhs.magnitude.isZero ? 0 : 1 62 | } 63 | 64 | Swift.assert(-1 <= positive && positive <= 1) 65 | return lhs.sign == Sign.plus ? positive : positive.twosComplement() 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKIntegerSignMagnitude.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Integer Sign Magnitude 12 | //*============================================================================* 13 | 14 | extension NBK { 15 | 16 | /// A namespace for integer sign-magnitude algorithms. 17 | @frozen public enum IntegerSignMagnitude where Magnitude: NBKUnsignedInteger { 18 | 19 | /// The sign and of this type. 20 | public typealias Sign = NBK.Sign 21 | 22 | /// The sign and a magnitude of this type. 23 | public typealias Components = SM 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKProperBinaryInteger.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Proper Binary Integer 12 | //*============================================================================* 13 | 14 | extension NBK { 15 | 16 | /// A namespace for proper binary integer algorithms. 17 | /// 18 | /// - Note: Proper binary integer models conform to ``NBKBinaryInteger``. 19 | /// 20 | @frozen public enum ProperBinaryInteger where Integer: NBKBinaryInteger { 21 | 22 | //*====================================================================* 23 | // MARK: * Magnitude 24 | //*====================================================================* 25 | 26 | public typealias Magnitude = ProperBinaryInteger 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKStrictBinaryInteger+Complements.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Strict Binary Integer x Complements x Sub Sequence 12 | //*============================================================================* 13 | 14 | extension NBK.StrictBinaryInteger.SubSequence where Base: MutableCollection { 15 | 16 | //=------------------------------------------------------------------------= 17 | // MARK: Transformations x One's Complement 18 | //=------------------------------------------------------------------------= 19 | 20 | @inlinable public static func formOnesComplement(_ base: inout Base) { 21 | for index in base.indices { 22 | base[index].formOnesComplement() 23 | } 24 | } 25 | 26 | //=------------------------------------------------------------------------= 27 | // MARK: Transformations x Two's Complement 28 | //=------------------------------------------------------------------------= 29 | 30 | @inlinable public static func formTwosComplement(_ base: inout Base) { 31 | _ = Unsigned.formTwosComplementReportingOverflow(&base) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKStrictBinaryInteger+Division.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Strict Binary Integer x Division 12 | //*============================================================================* 13 | //=----------------------------------------------------------------------------= 14 | // MARK: + Digit where Base is Mutable Collection 15 | //=----------------------------------------------------------------------------= 16 | 17 | extension NBK.StrictBinaryInteger where Base: MutableCollection { 18 | 19 | //=------------------------------------------------------------------------= 20 | // MARK: Transformations 21 | //=------------------------------------------------------------------------= 22 | 23 | /// Forms the `quotient` of dividing `base` by `other`, and returns 24 | /// the `remainder` along with an `overflow` indicator. 25 | /// 26 | /// - Note: In the case of `overflow`, the result is either truncated or, 27 | /// if undefined, the `base` and `base.first`. 28 | /// 29 | @inlinable public static func formQuotientWithRemainderReportingOverflow( 30 | dividing base: inout Base, by other: Base.Element, signedness: Digit.Type) -> PVO where Digit: NBKCoreInteger { 31 | //=--------------------------------------= 32 | var other = other as Base.Element 33 | let lhsIsLessThanZero: Bool = Digit.isSigned && base.last!.mostSignificantBit 34 | let rhsIsLessThanZero: Bool = Digit.isSigned && other/*-*/.mostSignificantBit 35 | //=--------------------------------------= 36 | if lhsIsLessThanZero { 37 | SubSequence.formTwosComplement(&base) 38 | } 39 | 40 | if rhsIsLessThanZero { 41 | other.formTwosComplement() 42 | } 43 | //=--------------------------------------= 44 | var remainder = Unsigned.SubSequence.formQuotientWithRemainderReportingOverflow(dividing: &base, by: other) 45 | //=--------------------------------------= 46 | if lhsIsLessThanZero != rhsIsLessThanZero { 47 | SubSequence.formTwosComplement(&base) 48 | } 49 | 50 | if lhsIsLessThanZero { 51 | remainder.partialValue.formTwosComplement() 52 | } 53 | 54 | if lhsIsLessThanZero && rhsIsLessThanZero && base.last!.mostSignificantBit { 55 | remainder.overflow = true 56 | } 57 | //=--------------------------------------= 58 | return remainder as PVO 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKStrictBinaryInteger+Words.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Strict Binary Integer x Words 12 | //*============================================================================* 13 | //=----------------------------------------------------------------------------= 14 | // MARK: + Digit where Base is Unsafe Buffer Pointer 15 | //=----------------------------------------------------------------------------= 16 | 17 | extension NBK.StrictBinaryInteger { 18 | 19 | //=------------------------------------------------------------------------= 20 | // MARK: Utilities 21 | //=------------------------------------------------------------------------= 22 | 23 | /// Grants unsafe access to the given element as a strict binary integer. 24 | @inlinable public static func withUnsafeBufferPointer( 25 | to element: T, perform body: (Base) throws -> U) rethrows -> U where Base == UnsafeBufferPointer { 26 | try Swift.withUnsafePointer(to: element) { 27 | try body(Base(start: $0, count: 1 as Int)) 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKStrictBinaryInteger.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Strict Binary Integer 12 | //*============================================================================* 13 | 14 | extension NBK { 15 | 16 | /// A namespace for strict binary integer algorithms. 17 | /// 18 | /// The `base` must be `nonempty` at the start and end of each access. 19 | /// 20 | /// ```swift 21 | /// static func algorithm(_ base: inout Base, input: Input) -> Output 22 | /// ``` 23 | /// 24 | /// ### Development 25 | /// 26 | /// Remaking this as a view when Swift gets view types might be neat. 27 | /// 28 | @frozen public enum StrictBinaryInteger where Base: RandomAccessCollection, Base.Element: NBKCoreInteger & NBKUnsignedInteger { 29 | 30 | /// The signed integer namespace of this type. 31 | public typealias Signed = NBK.StrictSignedInteger 32 | 33 | /// The unsigned integer namespace of this type. 34 | public typealias Unsigned = NBK.StrictUnsignedInteger 35 | 36 | //*====================================================================* 37 | // MARK: * Sub Sequence 38 | //*====================================================================* 39 | 40 | /// The sub sequence namespace of this type. 41 | /// 42 | /// The `base` may be `empty` at the start and end of each access. 43 | /// 44 | @frozen public enum SubSequence { 45 | 46 | /// The signed integer namespace of this type. 47 | public typealias Signed = NBK.StrictSignedInteger.SubSequence 48 | 49 | /// The unsigned integer namespace of this type. 50 | public typealias Unsigned = NBK.StrictUnsignedInteger.SubSequence 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKStrictSignMagnitude+Comparisons.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Strict Sign Magnitude x Comparisons x Sub Sequence 12 | //*============================================================================* 13 | 14 | extension NBK.StrictSignMagnitude.SubSequence { 15 | 16 | //=------------------------------------------------------------------------= 17 | // MARK: Utilities 18 | //=------------------------------------------------------------------------= 19 | 20 | /// Returns whether `components` is less than `zero`. 21 | @inlinable public static func isLessThanZero(_ components: Components) -> Bool { 22 | components.sign == Sign.minus && !components.magnitude.allSatisfy({ $0.isZero }) 23 | } 24 | 25 | /// Returns whether `components` is more than `zero`. 26 | @inlinable public static func isMoreThanZero(_ components: Components) -> Bool { 27 | components.sign == Sign.plus && !components.magnitude.allSatisfy({ $0.isZero }) 28 | } 29 | 30 | /// A three-way comparison of `components` against `zero`. 31 | @inlinable public static func signum(_ components: Components) -> Int { 32 | components.magnitude.allSatisfy({ $0.isZero }) ? 0 : components.sign == Sign.plus ? 1 : -1 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKStrictSignMagnitude.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Strict Sign Magnitude 12 | //*============================================================================* 13 | 14 | extension NBK { 15 | 16 | /// A namespace for strict sign-magnitude algorithms. 17 | /// 18 | /// The `base` must be `nonempty` at the start and end of each access. 19 | /// 20 | /// ```swift 21 | /// static func algorithm(_ base: inout Base, input: Input) -> Output 22 | /// ``` 23 | /// 24 | /// ### Development 25 | /// 26 | /// Remaking this as a view when Swift gets view types might be neat. 27 | /// 28 | @frozen public enum StrictSignMagnitude where Base: RandomAccessCollection, Base.Element: NBKCoreInteger & NBKUnsignedInteger { 29 | 30 | /// The sign and of this type. 31 | public typealias Sign = NBK.Sign 32 | 33 | /// The sign and a magnitude of this type. 34 | public typealias Components = SM 35 | 36 | //*====================================================================* 37 | // MARK: * Sub Sequence 38 | //*====================================================================* 39 | 40 | /// The sub sequence namespace of this type. 41 | /// 42 | /// The `base` may be `empty` at the start and end of each access. 43 | /// 44 | @frozen public enum SubSequence { 45 | 46 | /// The sign and of this type. 47 | public typealias Sign = NBK.Sign 48 | 49 | /// The sign and a magnitude of this type. 50 | public typealias Components = SM 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKStrictSignedInteger+Comparisons.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Strict Signed Integer x Comparisons 12 | //*============================================================================* 13 | //=----------------------------------------------------------------------------= 14 | // MARK: + where Base is Unsafe Buffer Pointer 15 | //=----------------------------------------------------------------------------= 16 | 17 | extension NBK.StrictSignedInteger { 18 | 19 | //=------------------------------------------------------------------------= 20 | // MARK: Utilities 21 | //=------------------------------------------------------------------------= 22 | 23 | /// A three-way comparison of `lhs` against `rhs`. 24 | /// 25 | /// ### Development 26 | /// 27 | /// Specializing it where `T == UInt` makes it faster. 28 | /// 29 | @inlinable public static func compare(_ lhs: Base, to rhs: some RandomAccessCollection) -> Int { 30 | let lhs = NBK.SuccinctInt(fromStrictSignedIntegerSubSequence: lhs)! 31 | let rhs = NBK.SuccinctInt(fromStrictSignedIntegerSubSequence: rhs)! 32 | return lhs.compared(to: rhs) as Int 33 | } 34 | 35 | /// A three-way comparison of `lhs` against `rhs` at `index`. 36 | /// 37 | /// ### Development 38 | /// 39 | /// Specializing it where `T == UInt` makes it faster. 40 | /// 41 | @inlinable public static func compare(_ lhs: Base, to rhs: some RandomAccessCollection, at index: Base.Index) -> Int { 42 | let lhs = NBK.SuccinctInt(fromStrictSignedIntegerSubSequence: lhs)! 43 | let rhs = NBK.SuccinctInt(fromStrictSignedIntegerSubSequence: rhs)! 44 | let partition = min(index,lhs.body.endIndex) 45 | let suffix = lhs.body.suffix(from: partition) 46 | let comparison = NBK.SuccinctInt(unchecked: suffix, sign: lhs.sign).compared(to: rhs) 47 | if !comparison.isZero { return comparison } 48 | let prefix = lhs.body.prefix(upTo: partition) 49 | return Int(bit: partition == index ? !prefix.allSatisfy({ $0.isZero }) : lhs.sign) 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKStrictSignedInteger.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Strict Signed Integer 12 | //*============================================================================* 13 | 14 | extension NBK { 15 | 16 | /// A namespace for strict signed integer algorithms. 17 | /// 18 | /// The `base` must be `nonempty` at the start and end of each access. 19 | /// 20 | /// ```swift 21 | /// static func algorithm(_ base: inout Base, input: Input) -> Output 22 | /// ``` 23 | /// 24 | /// ### Development 25 | /// 26 | /// Remaking this as a view when Swift gets view types might be neat. 27 | /// 28 | @frozen public enum StrictSignedInteger where Base: RandomAccessCollection, Base.Element: NBKCoreInteger & NBKUnsignedInteger { 29 | 30 | /// The binary integer namespace of this type. 31 | public typealias Binary = NBK.StrictBinaryInteger 32 | 33 | /// The unsigned integer namespace of this type. 34 | public typealias Unsigned = NBK.StrictUnsignedInteger 35 | 36 | //*====================================================================* 37 | // MARK: * Sub Sequence 38 | //*====================================================================* 39 | 40 | /// The sub sequence namespace of this type. 41 | /// 42 | /// The `base` may be `empty` at the start and end of each access. 43 | /// 44 | @frozen public enum SubSequence { 45 | 46 | /// The binary integer namespace of this type. 47 | public typealias Binary = NBK.StrictBinaryInteger.SubSequence 48 | 49 | /// The unsigned integer namespace of this type. 50 | public typealias Unsigned = NBK.StrictUnsignedInteger.SubSequence 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKStrictUnsignedInteger+Comparisons.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Strict Unsigned Integer x Comparisons x Sub Sequence 12 | //*============================================================================* 13 | //=----------------------------------------------------------------------------= 14 | // MARK: + where Base is Unsafe Buffer Pointer 15 | //=----------------------------------------------------------------------------= 16 | 17 | extension NBK.StrictUnsignedInteger.SubSequence { 18 | 19 | //=------------------------------------------------------------------------= 20 | // MARK: Utilities 21 | //=------------------------------------------------------------------------= 22 | 23 | /// A three-way comparison of `lhs` against `rhs`. 24 | /// 25 | /// - Note: This operation interprets empty collections as zero. 26 | /// 27 | @inlinable public static func compare(_ lhs: Base, to rhs: some RandomAccessCollection) -> Int { 28 | let lhs = NBK.SuccinctInt(fromStrictUnsignedIntegerSubSequence: lhs) 29 | let rhs = NBK.SuccinctInt(fromStrictUnsignedIntegerSubSequence: rhs) 30 | return lhs.compared(toSameSignUnchecked: rhs) as Int 31 | } 32 | 33 | /// A three-way comparison of `lhs` against `rhs` at `index`. 34 | /// 35 | /// - Note: This operation interprets empty collections as zero. 36 | /// 37 | @inlinable public static func compare(_ lhs: Base, to rhs: some RandomAccessCollection, at index: Base.Index) -> Int { 38 | let partition = Swift.min(index, lhs.endIndex) 39 | let suffix = lhs.suffix(from: partition) 40 | let comparison = NBK.SUISS.compare(suffix, to: rhs[...]) 41 | if !comparison.isZero { return comparison } 42 | let prefix = lhs.prefix(upTo: partition) 43 | return Int(bit: !prefix.allSatisfy({ $0.isZero })) 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKStrictUnsignedInteger+Complements.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Strict Unsigned Integer x Complements x Sub Sequence 12 | //*============================================================================* 13 | 14 | extension NBK.StrictUnsignedInteger.SubSequence where Base: MutableCollection { 15 | 16 | //=------------------------------------------------------------------------= 17 | // MARK: Transformations x Two's Complement 18 | //=------------------------------------------------------------------------= 19 | 20 | @inlinable public static func formTwosComplementReportingOverflow(_ base: inout Base) -> Bool { 21 | self.formTwosComplementSubsequence(&base, carry: true) 22 | } 23 | 24 | @inlinable public static func formTwosComplementSubsequence(_ base: inout Base, carry: Bool) -> Bool { 25 | var carry = carry 26 | 27 | for index in base.indices { 28 | carry = base[index].formTwosComplementSubsequence(carry) 29 | } 30 | 31 | return carry as Bool 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKStrictUnsignedInteger+Multiplication.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Strict Unsigned Integer x Multiplication x Sub Sequence 12 | //*============================================================================* 13 | //=----------------------------------------------------------------------------= 14 | // MARK: + where Base is Unsafe Buffer Pointer 15 | //=----------------------------------------------------------------------------= 16 | 17 | extension NBK.StrictUnsignedInteger.SubSequence where Base: MutableCollection { 18 | 19 | //=------------------------------------------------------------------------= 20 | // MARK: Initializers 21 | //=------------------------------------------------------------------------= 22 | 23 | /// Initializes `base` to the product of `lhs` and `rhs`. 24 | /// 25 | /// - Parameter base: A buffer of size `lhs.count` + `rhs.count`. 26 | /// 27 | /// - Important: The `base` must be uninitialized, or its elements must be trivial. 28 | /// 29 | @inlinable public static func initialize( 30 | _ base: inout Base, to lhs: UnsafeBufferPointer, times rhs: UnsafeBufferPointer) 31 | where Base == UnsafeMutableBufferPointer { 32 | if lhs.count < 20 || rhs.count < 20 { 33 | return self.initializeByLongAlgorithm( 34 | &base, to: lhs, times: rhs, plus: Base.Element.zero) 35 | } else { 36 | return self.initializeByKaratsubaAlgorithm( 37 | &base, to: lhs, times: rhs) 38 | } 39 | } 40 | 41 | /// Initializes `base` to the square product of `elements`. 42 | /// 43 | /// - Parameter base: A buffer of size `2 * elements.count`. 44 | /// 45 | /// - Important: The `base` must be uninitialized, or its elements must be trivial. 46 | /// 47 | @inlinable public static func initialize( 48 | _ base: inout Base, toSquareProductOf elements: UnsafeBufferPointer) 49 | where Base == UnsafeMutableBufferPointer { 50 | if elements.count < 20 { 51 | return self.initializeByLongAlgorithm( 52 | &base, toSquareProductOf: elements, plus: Base.Element.zero) 53 | } else { 54 | return self.initializeByKaratsubaAlgorithm( 55 | &base, toSquareProductOf: elements) 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKStrictUnsignedInteger+Partition.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Strict Unsigned Integer x Partition x Sub Sequence 12 | //*============================================================================* 13 | //=----------------------------------------------------------------------------= 14 | // MARK: + where Base is Unsafe Buffer Pointer 15 | //=----------------------------------------------------------------------------= 16 | 17 | extension NBK.StrictUnsignedInteger.SubSequence { 18 | 19 | //=------------------------------------------------------------------------= 20 | // MARK: Transformations 21 | //=------------------------------------------------------------------------= 22 | 23 | /// Splits `base` at `index` then trims redundant zeros from each sequence. 24 | @inlinable public static func partitionTrimmingRedundantZeros( 25 | _ base: Base, at index: Base.Index) -> HL where Base == UnsafeBufferPointer { 26 | let partition = Swift.min(base.count, index) 27 | let low = Base(start: base.baseAddress!, /*------*/ count: NBK.dropLast(from: base[.. Output 22 | /// ``` 23 | /// 24 | /// ### Development 25 | /// 26 | /// Remaking this as a view when Swift gets view types might be neat. 27 | /// 28 | @frozen public enum StrictUnsignedInteger where Base: RandomAccessCollection, Base.Element: NBKCoreInteger & NBKUnsignedInteger { 29 | 30 | /// The binary integer namespace of this type. 31 | public typealias Binary = NBK.StrictBinaryInteger 32 | 33 | /// The signed integer namespace of this type. 34 | public typealias Signed = NBK.StrictSignedInteger 35 | 36 | //*====================================================================* 37 | // MARK: * Sub Sequence 38 | //*====================================================================* 39 | 40 | /// The sub sequence namespace of this type. 41 | /// 42 | /// The `base` may be `empty` at the start and end of each access. 43 | /// 44 | @frozen public enum SubSequence { 45 | 46 | /// The binary integer namespace of this type. 47 | public typealias Binary = NBK.StrictBinaryInteger.SubSequence 48 | 49 | /// The signed integer namespace of this type. 50 | public typealias Signed = NBK.StrictSignedInteger.SubSequence 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKTupleBinaryInteger+Comparisons.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Tuple Binary Integer x Comparisons 12 | //*============================================================================* 13 | 14 | extension NBK.TupleBinaryInteger { 15 | 16 | //=------------------------------------------------------------------------= 17 | // MARK: Utilities 18 | //=------------------------------------------------------------------------= 19 | 20 | /// A three-way comparison that returns: `-1` (less), `0` (same), or `1` (more). 21 | /// 22 | /// ``` 23 | /// ┌─────────── → ────────┐ 24 | /// │ lhs vs rhs │ signum │ 25 | /// ├─────────── → ────────┤ 26 | /// │ lhs < rhs | Int(-1) | - less 27 | /// │ lhs == rhs | Int( 0) | - same 28 | /// │ lhs > rhs | Int( 1) | - more 29 | /// └─────────── → ────────┘ 30 | /// ``` 31 | /// 32 | @_transparent public static func compare22S(_ lhs: Wide2, to rhs: Wide2) -> Int { 33 | let a = lhs.high.compared(to: rhs.high); if !a.isZero { return a } 34 | return lhs.low .compared(to: rhs.low ); 35 | } 36 | 37 | /// A three-way comparison that returns: `-1` (less), `0` (same), or `1` (more). 38 | /// 39 | /// ``` 40 | /// ┌─────────── → ────────┐ 41 | /// │ lhs vs rhs │ signum │ 42 | /// ├─────────── → ────────┤ 43 | /// │ lhs < rhs | Int(-1) | - less 44 | /// │ lhs == rhs | Int( 0) | - same 45 | /// │ lhs > rhs | Int( 1) | - more 46 | /// └─────────── → ────────┘ 47 | /// ``` 48 | /// 49 | @_transparent public static func compare33S(_ lhs: Wide3, to rhs: Wide3) -> Int { 50 | let a = lhs.high.compared(to: rhs.high); if !a.isZero { return a } 51 | let b = lhs.mid .compared(to: rhs.mid ); if !b.isZero { return b } 52 | return lhs.low .compared(to: rhs.low ); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKTupleBinaryInteger+Complements.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Tuple Binary Integer x Complements x Unsigned 12 | //*============================================================================* 13 | 14 | extension NBK.TupleBinaryInteger { 15 | 16 | //=------------------------------------------------------------------------= 17 | // MARK: Transformations 18 | //=------------------------------------------------------------------------= 19 | 20 | /// Returns the `magnitude` of the given `value`. 21 | /// 22 | /// ``` 23 | /// ┌─────────────────── → ───────────────────┐ 24 | /// │ value │ magnitude │ 25 | /// ├─────────────────── → ───────────────────┤ 26 | /// │ Int(~1), UInt(~1) │ UInt( 1), UInt( 2) │ 27 | /// │ Int(~0), UInt( 0) │ UInt( 1), UInt( 0) │ 28 | /// │ Int( 0), UInt( 0) │ UInt( 0), UInt( 0) │ 29 | /// │ Int( 1), UInt( 0) │ UInt( 1), UInt( 0) │ 30 | /// │ Int( 1), UInt( 2) │ UInt( 1), UInt( 2) │ 31 | /// └─────────────────── → ───────────────────┘ 32 | /// ``` 33 | /// 34 | @_transparent public static func magnitude(of value: Wide2) -> Magnitude.Wide2 { 35 | var value = value as Wide2 36 | if value.high.isLessThanZero { 37 | var carry = true 38 | carry = value.low .formTwosComplementSubsequence(carry) 39 | carry = value.high.formTwosComplementSubsequence(carry) 40 | } 41 | 42 | return NBK.bitCast(value) as Magnitude.Wide2 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKTupleBinaryInteger+Division.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Tuple Binary Integer x Division x Unsigned 12 | //*============================================================================* 13 | 14 | extension NBK.TupleBinaryInteger where High: NBKUnsignedInteger { 15 | 16 | //=------------------------------------------------------------------------= 17 | // MARK: Transformation 18 | //=------------------------------------------------------------------------= 19 | 20 | /// Forms the `remainder` of dividing the `dividend` by the `divisor`, 21 | /// then returns the `quotient`. The `divisor` must be normalized and 22 | /// the `quotient` must fit in one element. 23 | /// 24 | /// - Returns: The `quotient` is returned and the `remainder` replaces the `dividend`. 25 | /// 26 | /// ### Development 1 27 | /// 28 | /// Comparing is faster than overflow checking, according to time profiler. 29 | /// 30 | /// ### Development 2 31 | /// 32 | /// The approximation needs at most two corrections, but looping is faster. 33 | /// 34 | @_transparent public static func formRemainderWithQuotient3212MSBUnchecked( 35 | dividing dividend: inout Wide3, by divisor: Wide2) -> High { 36 | //=--------------------------------------= 37 | Swift.assert(divisor.high.mostSignificantBit, 38 | "the divisor must be normalized") 39 | 40 | Swift.assert(self.compare22S(Wide2(dividend.high, dividend.mid), to: divisor).isLessThanZero, 41 | "the quotient must fit in one element") 42 | //=--------------------------------------= 43 | var quotient: High = divisor.high == dividend.high 44 | ? High.max // the quotient must fit in one element 45 | : divisor.high.dividingFullWidth(Wide2(dividend.high, dividend.mid)).quotient 46 | //=--------------------------------------= 47 | // decrement when overestimated (max 2) 48 | //=--------------------------------------= 49 | var product = self.multiplying213(divisor, by: quotient) 50 | 51 | while self.compare33S(dividend, to: product) == -1 { 52 | _ = quotient.subtractReportingOverflow(1 as High.Digit) 53 | _ = self.decrement32B(&product, by: divisor) 54 | }; _ = self.decrement33B(÷nd, by: product) 55 | 56 | Swift.assert(self.compare33S(dividend, to: Wide3(0, divisor.high, divisor.low)).isLessThanZero) 57 | return quotient as High 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKTupleBinaryInteger+Multiplication.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Tuple Binary Integer x Multiplication x Unsigned 12 | //*============================================================================* 13 | 14 | extension NBK.TupleBinaryInteger where High: NBKUnsignedInteger { 15 | 16 | //=------------------------------------------------------------------------= 17 | // MARK: Transformations 18 | //=------------------------------------------------------------------------= 19 | 20 | /// Returns the `product` of multiplying `lhs` by `rhs`. 21 | /// 22 | /// ``` 23 | /// ┌────────┬──── → ───────────┐ 24 | /// │ lhs │ rhs │ product │ 25 | /// ├────────┼──── → ───────────┤ 26 | /// │ 1, 2 │ 3, │ 0, 3, 6 │ 27 | /// │ ~1, ~2 │ ~3, │ ~4, 1, 12 │ 28 | /// │ ~0, ~0 │ ~0, │ ~1, ~0, 1 │ 29 | /// └────────┴──── → ───────────┘ 30 | /// ``` 31 | /// 32 | @_transparent public static func multiplying213(_ lhs: Wide2, by rhs: High) -> Wide3 { 33 | let a = lhs.low .multipliedFullWidth(by: rhs) 34 | var b = lhs.high.multipliedFullWidth(by: rhs) 35 | 36 | let x = b.low.addReportingOverflow(a.high) 37 | let _ = x && b.high.addReportingOverflow(1 as High.Digit) 38 | return Wide3(high: b.high, mid: b.low, low: a.low) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Sources/NBKCoreKit/Private/NBKTupleBinaryInteger.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Tuple Binary Integer 12 | //*============================================================================* 13 | 14 | extension NBK { 15 | 16 | /// A namespace for tuple binary integer algorithms. 17 | /// 18 | /// A tuple binary integer's signedness is determined by its `High` type. 19 | /// 20 | @frozen public enum TupleBinaryInteger where High: NBKFixedWidthInteger { 21 | 22 | /// An integer. 23 | public typealias Wide1 = High 24 | 25 | /// An integer split into 2 parts. 26 | public typealias Wide2 = NBK.Wide2 27 | 28 | /// An integer split into 3 parts. 29 | public typealias Wide3 = NBK.Wide3 30 | 31 | //*====================================================================* 32 | // MARK: * Magnitude 33 | //*====================================================================* 34 | 35 | public typealias Magnitude = TupleBinaryInteger 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Sources/NBKDoubleWidthKit/NBKDoubleWidth+Addition+Digit.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Double Width x Addition x Digit 14 | //*============================================================================* 15 | 16 | extension NBKDoubleWidth { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Transformations x Overflow 20 | //=------------------------------------------------------------------------= 21 | 22 | @_disfavoredOverload @inlinable public mutating func addReportingOverflow(_ other: Digit) -> Bool { 23 | let minus = other.isLessThanZero as Bool 24 | if self.first.addReportingOverflow(UInt(bitPattern: other)) == minus { return false } 25 | let extra = UInt(bitPattern: minus ? -1 : 1) 26 | for index in 1 ..< self.lastIndex where self[index].addReportingOverflow(extra) == minus { return false } 27 | return self.tail.addReportingOverflow(Digit(bitPattern: extra)) 28 | } 29 | 30 | @_disfavoredOverload @inlinable public func addingReportingOverflow(_ other: Digit) -> PVO { 31 | var partialValue = self 32 | let overflow: Bool = partialValue.addReportingOverflow(other) 33 | return PVO(partialValue, overflow) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Sources/NBKDoubleWidthKit/NBKDoubleWidth+Addition.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Double Width x Addition 14 | //*============================================================================* 15 | 16 | extension NBKDoubleWidth { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Transformations x Overflow 20 | //=------------------------------------------------------------------------= 21 | 22 | @inlinable public mutating func addReportingOverflow(_ other: Self) -> Bool { 23 | var overflow = self.low.addReportingOverflow(other.low) as Bool 24 | overflow = overflow && self.high.addReportingOverflow(1 as Digit) 25 | return overflow != self.high.addReportingOverflow(other.high) 26 | } 27 | 28 | @inlinable public func addingReportingOverflow(_ other: Self) -> PVO { 29 | var partialValue = self 30 | let overflow: Bool = partialValue.addReportingOverflow(other) 31 | return PVO(partialValue, overflow) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Sources/NBKDoubleWidthKit/NBKDoubleWidth+Bits.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Double Width x Bits 14 | //*============================================================================* 15 | 16 | extension NBKDoubleWidth { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Accessors 20 | //=------------------------------------------------------------------------= 21 | 22 | @inlinable public static var bitWidth: Int { 23 | assert(MemoryLayout.size / MemoryLayout.stride >= 2) 24 | assert(MemoryLayout.size % MemoryLayout.stride == 0) 25 | return MemoryLayout.size * 8 26 | } 27 | 28 | //=------------------------------------------------------------------------= 29 | // MARK: Initializers 30 | //=------------------------------------------------------------------------= 31 | 32 | @inlinable public init(bit: Bool) { 33 | self.init(low: Low(bit: bit)) 34 | } 35 | 36 | @inlinable public init(repeating bit: Bool) { 37 | self.init(bitPattern: bit ? Magnitude.max : Magnitude.min) 38 | } 39 | 40 | //=------------------------------------------------------------------------= 41 | // MARK: Accessors 42 | //=------------------------------------------------------------------------= 43 | 44 | @inlinable public var nonzeroBitCount: Int { 45 | self.low.nonzeroBitCount &+ self.high.nonzeroBitCount 46 | } 47 | 48 | @inlinable public var leadingZeroBitCount: Int { 49 | let result = self.high.leadingZeroBitCount 50 | guard result == High.bitWidth else { return result } 51 | return result &+ self.low .leadingZeroBitCount 52 | } 53 | 54 | @inlinable public var trailingZeroBitCount: Int { 55 | let result = self.low .trailingZeroBitCount 56 | guard result == Low .bitWidth else { return result } 57 | return result &+ self.high.trailingZeroBitCount 58 | } 59 | 60 | @inlinable public var mostSignificantBit: Bool { 61 | self.high.mostSignificantBit 62 | } 63 | 64 | @inlinable public var leastSignificantBit: Bool { 65 | self.low.leastSignificantBit 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Sources/NBKDoubleWidthKit/NBKDoubleWidth+Endianness.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Double Width x Endianness 14 | //*============================================================================* 15 | 16 | extension NBKDoubleWidth { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Initializers 20 | //=------------------------------------------------------------------------= 21 | 22 | @inlinable public init(bigEndian value: Self) { 23 | #if _endian(big) 24 | self = value 25 | #else 26 | self = value.byteSwapped 27 | #endif 28 | } 29 | 30 | @inlinable public init(littleEndian value: Self) { 31 | #if _endian(big) 32 | self = value.byteSwapped 33 | #else 34 | self = value 35 | #endif 36 | } 37 | 38 | //=------------------------------------------------------------------------= 39 | // MARK: Transformations 40 | //=------------------------------------------------------------------------= 41 | 42 | @inlinable public var bigEndian: Self { 43 | #if _endian(big) 44 | return self 45 | #else 46 | return self.byteSwapped 47 | #endif 48 | } 49 | 50 | @inlinable public var littleEndian: Self { 51 | #if _endian(big) 52 | return self.byteSwapped 53 | #else 54 | return self 55 | #endif 56 | } 57 | 58 | @inlinable public var byteSwapped: Self { 59 | let high = High(bitPattern: self.low .byteSwapped) 60 | let low = Low (bitPattern: self.high.byteSwapped) 61 | return Self(high: high, low: low) 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Sources/NBKDoubleWidthKit/NBKDoubleWidth+Logic.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Double Width x Logic 14 | //*============================================================================* 15 | 16 | extension NBKDoubleWidth { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Transformations 20 | //=------------------------------------------------------------------------= 21 | 22 | @inlinable public static prefix func ~(x: Self) -> Self { 23 | x.onesComplement() 24 | } 25 | 26 | @inlinable public static func &=(lhs: inout Self, rhs: Self) { 27 | lhs.low &= rhs.low 28 | lhs.high &= rhs.high 29 | } 30 | 31 | @inlinable public static func |=(lhs: inout Self, rhs: Self) { 32 | lhs.low |= rhs.low 33 | lhs.high |= rhs.high 34 | } 35 | 36 | @inlinable public static func ^=(lhs: inout Self, rhs: Self) { 37 | lhs.low ^= rhs.low 38 | lhs.high ^= rhs.high 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Sources/NBKDoubleWidthKit/NBKDoubleWidth+Numbers.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Double Width x Numbers 14 | //*============================================================================* 15 | 16 | extension NBKDoubleWidth { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Initializers 20 | //=------------------------------------------------------------------------= 21 | 22 | @inlinable public static var min: Self { 23 | Self(high: High.min, low: Low.min) 24 | } 25 | 26 | @inlinable public static var max: Self { 27 | Self(high: High.max, low: Low.max) 28 | } 29 | 30 | //=------------------------------------------------------------------------= 31 | // MARK: Initializers 32 | //=------------------------------------------------------------------------= 33 | 34 | @inlinable public init(digit: Digit) { 35 | let bit = digit.isLessThanZero 36 | let high = High(repeating: bit) 37 | let low = Low(truncatingIfNeeded: digit) 38 | self.init(high: high, low: low) 39 | } 40 | 41 | @inlinable public init(_truncatingBits source: UInt) { 42 | self.init(low: Low(_truncatingBits: source)) 43 | } 44 | 45 | //=------------------------------------------------------------------------= 46 | // MARK: Initializers x Binary Integer 47 | //=------------------------------------------------------------------------= 48 | 49 | @inlinable public init(_ source: some BinaryInteger) { 50 | if let value = Self(exactly: source) { self = value } else { 51 | preconditionFailure("\(Self.description) cannot represent \(source)") 52 | } 53 | } 54 | 55 | @inlinable public init?(exactly source: T) { 56 | let comparison: Int 57 | (self,comparison) = Self.validating(words: source.words, isSigned: T.isSigned) 58 | if !comparison.isZero { return nil } 59 | } 60 | 61 | @inlinable public init(clamping source: T) { 62 | let comparison: Int 63 | (self,comparison) = Self.validating(words: source.words, isSigned: T.isSigned) 64 | if !comparison.isZero { self = comparison == -1 ? Self.min : Self.max } 65 | } 66 | 67 | @inlinable public init(truncatingIfNeeded source: T) { 68 | self = Self.truncating(words: source.words, isSigned: T.isSigned).value 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Sources/NBKDoubleWidthKit/NBKDoubleWidth+Strides.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Double Width x Strides 14 | //*============================================================================* 15 | 16 | extension NBKDoubleWidth { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Details 20 | //=------------------------------------------------------------------------= 21 | 22 | @inlinable public func distance(to other: Self) -> Int { 23 | let distance = other.subtractingReportingOverflow(self) 24 | let distanceIsLessThanZero = distance.partialValue.isLessThanZero != distance.overflow 25 | return Int(words: distance.partialValue, isSigned: distanceIsLessThanZero)! 26 | } 27 | 28 | @inlinable public func advanced(by distance: Int) -> Self { 29 | if Self.isSigned || distance >= 0 as Int { 30 | return self + Digit(bitPattern: distance) 31 | } else { 32 | return self - Digit(bitPattern: distance.twosComplement()) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Sources/NBKDoubleWidthKit/NBKDoubleWidth+Subtraction+Digit.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Double Width x Subtraction x Digit 14 | //*============================================================================* 15 | 16 | extension NBKDoubleWidth { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Transformations x Overflow 20 | //=------------------------------------------------------------------------= 21 | 22 | @_disfavoredOverload @inlinable public mutating func subtractReportingOverflow(_ other: Digit) -> Bool { 23 | let minus = other.isLessThanZero as Bool 24 | if self.first.subtractReportingOverflow(UInt(bitPattern: other)) == minus { return false } 25 | let extra = UInt(bitPattern: minus ? -1 : 1) 26 | for index in 1 ..< self.lastIndex where self[index].subtractReportingOverflow(extra) == minus { return false } 27 | return self.tail.subtractReportingOverflow(Digit(bitPattern: extra)) 28 | } 29 | 30 | @_disfavoredOverload @inlinable public func subtractingReportingOverflow(_ other: Digit) -> PVO { 31 | var partialValue = self 32 | let overflow: Bool = partialValue.subtractReportingOverflow(other) 33 | return PVO(partialValue, overflow) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Sources/NBKDoubleWidthKit/NBKDoubleWidth+Subtraction.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Double Width x Subtraction 14 | //*============================================================================* 15 | 16 | extension NBKDoubleWidth { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Transformations x Overflow 20 | //=------------------------------------------------------------------------= 21 | 22 | @inlinable public mutating func subtractReportingOverflow(_ other: Self) -> Bool { 23 | var overflow = self.low.subtractReportingOverflow(other.low) as Bool 24 | overflow = overflow && self.high.subtractReportingOverflow(1 as Digit) 25 | return overflow != self.high.subtractReportingOverflow(other.high) 26 | } 27 | 28 | @inlinable public func subtractingReportingOverflow(_ other: Self) -> PVO { 29 | var partialValue = self 30 | let overflow: Bool = partialValue.subtractReportingOverflow(other) 31 | return PVO(partialValue, overflow) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Sources/NBKDoubleWidthKit/NBKDoubleWidth+Text.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Double Width x Text 14 | //*============================================================================* 15 | 16 | extension NBKDoubleWidth { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Details x Decoding 20 | //=------------------------------------------------------------------------= 21 | 22 | @inlinable public init?(_ description: some StringProtocol, radix: Int) { 23 | let decoder = NBK.IntegerDescription.Decoder(radix: radix) 24 | guard let components = decoder.decode(description) else { return nil } 25 | self.init(sign: components.sign, magnitude: components.magnitude) 26 | } 27 | 28 | //=------------------------------------------------------------------------= 29 | // MARK: Details x Encoding 30 | //=------------------------------------------------------------------------= 31 | 32 | @inlinable public func description(radix: Int, uppercase: Bool) -> String { 33 | NBK.IntegerDescription.Encoder(radix: radix, uppercase: uppercase).encode(self) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Addition+Digit.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Flexible Width x Addition x Digit x Unsigned 14 | //*============================================================================* 15 | 16 | extension NBKFlexibleWidth.Magnitude { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Transformations 20 | //=------------------------------------------------------------------------= 21 | 22 | @_disfavoredOverload @inlinable public static func +=(lhs: inout Self, rhs: UInt) { 23 | lhs.add(rhs, at: 0 as Int) 24 | } 25 | 26 | @_disfavoredOverload @inlinable public static func +(lhs: Self, rhs: UInt) -> Self { 27 | lhs.adding(rhs, at: 0 as Int) 28 | } 29 | 30 | //=------------------------------------------------------------------------= 31 | // MARK: Transformations x Index 32 | //=------------------------------------------------------------------------= 33 | 34 | @_disfavoredOverload @inlinable public mutating func add(_ other: UInt, at index: Int) { 35 | //=--------------------------------------= 36 | if other.isZero { return } 37 | //=--------------------------------------= 38 | self.storage.resize(minCount: index + 1) 39 | 40 | let overflow = self.storage.withUnsafeMutableBufferPointer(in: index...) { 41 | NBK.SUISS.increment(&$0, by: other).overflow 42 | } 43 | 44 | if overflow { 45 | self.storage.append(1 as UInt) 46 | } 47 | 48 | Swift.assert(self.storage.isNormal) 49 | } 50 | 51 | @_disfavoredOverload @inlinable public func adding(_ other: UInt, at index: Int) -> Self { 52 | var result = self 53 | result.add(other, at: index) 54 | return result as Self 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Addition.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Flexible Width x Addition x Unsigned 14 | //*============================================================================* 15 | 16 | extension NBKFlexibleWidth.Magnitude { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Transformations 20 | //=------------------------------------------------------------------------= 21 | 22 | @inlinable public static func +=(lhs: inout Self, rhs: Self) { 23 | lhs.add(rhs, at: 0 as Int) 24 | } 25 | 26 | @inlinable public static func +(lhs: Self, rhs: Self) -> Self { 27 | lhs.adding(rhs, at: 0 as Int) 28 | } 29 | 30 | //=------------------------------------------------------------------------= 31 | // MARK: Transformations x Index 32 | //=------------------------------------------------------------------------= 33 | 34 | @inlinable public mutating func add(_ other: Self, at index: Int) { 35 | //=--------------------------------------= 36 | if other.isZero { return } 37 | //=--------------------------------------= 38 | self.storage.resize(minCount: index + other.storage.elements.count) 39 | 40 | let overflow = self.storage.withUnsafeMutableBufferPointer(in: index...) { slice in 41 | other.storage.withUnsafeBufferPointer { other in 42 | NBK.SUISS.increment(&slice, by: other).overflow 43 | } 44 | } 45 | 46 | if overflow { 47 | self.storage.append(1 as UInt) 48 | } 49 | 50 | Swift.assert(self.storage.isNormal) 51 | } 52 | 53 | @inlinable public func adding(_ other: Self, at index: Int) -> Self { 54 | var result = self 55 | result.add(other, at: index) 56 | return result as Self 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Bits.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Flexible Width x Bits x Unsigned 14 | //*============================================================================* 15 | 16 | extension NBKFlexibleWidth.Magnitude { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Initializers 20 | //=------------------------------------------------------------------------= 21 | 22 | @inlinable public init(bit: Bool) { 23 | self.init(digit: Digit(bit: bit)) 24 | } 25 | 26 | //=------------------------------------------------------------------------= 27 | // MARK: Accessors 28 | //=------------------------------------------------------------------------= 29 | 30 | @inlinable public var bitWidth: Int { 31 | self.storage.elements.count * UInt.bitWidth 32 | } 33 | 34 | @inlinable public var nonzeroBitCount: Int { 35 | self.withUnsafeBufferPointer(NBK.SBISS.nonzeroBitCount(of:)) 36 | } 37 | 38 | @inlinable public var leadingZeroBitCount: Int { 39 | self.withUnsafeBufferPointer(NBK.SBISS.leadingZeroBitCount(of:)) 40 | } 41 | 42 | @inlinable public var trailingZeroBitCount: Int { 43 | self.withUnsafeBufferPointer(NBK.SBISS.trailingZeroBitCount(of:)) 44 | } 45 | 46 | @inlinable public var mostSignificantBit: Bool { 47 | self.last.mostSignificantBit 48 | } 49 | 50 | @inlinable public var leastSignificantBit: Bool { 51 | self.first.leastSignificantBit 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Complements.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Flexible Width x Complements x Unsigned 14 | //*============================================================================* 15 | 16 | extension NBKFlexibleWidth.Magnitude { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Details x One's Complement 20 | //=------------------------------------------------------------------------= 21 | 22 | @inlinable public mutating func formOnesComplement() { 23 | self.withUnsafeMutableBufferPointer { 24 | NBK.SBISS.formOnesComplement(&$0) 25 | } 26 | } 27 | 28 | @inlinable public func onesComplement() -> Self { 29 | Self(normalizing: Storage(unchecked: Elements(self.storage.elements.lazy.map(~)))) 30 | } 31 | 32 | //=------------------------------------------------------------------------= 33 | // MARK: Details x Two's Complement 34 | //=------------------------------------------------------------------------= 35 | 36 | @inlinable public mutating func formTwosComplementReportingOverflow() -> Bool { 37 | self.withUnsafeMutableBufferPointer { 38 | NBK.SUISS.formTwosComplementReportingOverflow(&$0) 39 | } 40 | } 41 | 42 | @inlinable public func twosComplementReportingOverflow() -> PVO { 43 | var partialValue = self 44 | let overflow = partialValue.formTwosComplementReportingOverflow() 45 | return PVO(partialValue, overflow) 46 | } 47 | 48 | @inlinable public mutating func formTwosComplementSubsequence(_ carry: Bool) -> Bool { 49 | self.withUnsafeMutableBufferPointer { 50 | NBK.SUISS.formTwosComplementSubsequence(&$0, carry: carry) 51 | } 52 | } 53 | 54 | @inlinable public func twosComplementSubsequence(_ carry: Bool) -> PVO { 55 | var partialValue = self 56 | let overflow = partialValue.formTwosComplementSubsequence(carry) 57 | return PVO(partialValue, overflow) 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Exponentiation.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Flexible Width x Exponentiation x Unsigned 14 | //*============================================================================* 15 | 16 | extension NBKFlexibleWidth.Magnitude { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Transformations 20 | //=------------------------------------------------------------------------= 21 | 22 | /// Returns the `power` of `self` raised to `exponent`. 23 | /// 24 | /// - Parameter exponent: A value greater than or equal to zero. 25 | /// 26 | @inlinable public func power(_ exponent: Int) -> Self { 27 | //=--------------------------------------= 28 | if exponent == 0 { return Self.one } 29 | else if exponent == 1 { return self } 30 | //=--------------------------------------= 31 | Swift.assert(exponent > 001) 32 | var power = Self(digit: 001) 33 | var multiplier: Self = self 34 | var pattern = UInt(exponent) 35 | //=--------------------------------------= 36 | repeat { 37 | 38 | if pattern.isOdd { 39 | power *= multiplier 40 | } 41 | 42 | pattern &>>= 000001 43 | multiplier.square() 44 | 45 | } while !pattern.isZero 46 | //=--------------------------------------= 47 | return power as Self as Self as Self as Self 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Multiplication+Digit.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Flexible Width x Multiplication x Digit x Unsigned 14 | //*============================================================================* 15 | 16 | extension NBKFlexibleWidth.Magnitude { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Transformations 20 | //=------------------------------------------------------------------------= 21 | 22 | @_disfavoredOverload @inlinable public static func *=(lhs: inout Self, rhs: UInt) { 23 | lhs.multiply(by: rhs, add: 0 as UInt) 24 | } 25 | 26 | @_disfavoredOverload @inlinable public static func *(lhs: Self, rhs: UInt) -> Self { 27 | lhs.multiplied(by: rhs, adding: 0 as UInt) 28 | } 29 | 30 | //=------------------------------------------------------------------------= 31 | // MARK: Transformations x Addition 32 | //=------------------------------------------------------------------------= 33 | 34 | @_disfavoredOverload @inlinable public mutating func multiply(by multiplier: UInt, add addend: UInt) { 35 | //=--------------------------------------= 36 | if multiplier.isZero { 37 | return self.update(addend) 38 | } 39 | //=--------------------------------------= 40 | self.storage.reserveCapacity(self.storage.elements.count + 1) 41 | 42 | let carry = self.storage.withUnsafeMutableBufferPointer { 43 | NBK.SUISS.multiply(&$0, by: multiplier, add: addend) 44 | } 45 | 46 | if !carry.isZero { 47 | self.storage.append(carry) 48 | } 49 | 50 | Swift.assert(self.storage.isNormal) 51 | } 52 | 53 | @_disfavoredOverload @inlinable public func multiplied(by multiplier: UInt, adding addend: UInt) -> Self { 54 | var result = self 55 | result.multiply(by: multiplier, add: addend) 56 | return result as Self 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Multiplication.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Flexible Width x Multiplication x Unsigned 14 | //*============================================================================* 15 | 16 | extension NBKFlexibleWidth.Magnitude { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Transformations 20 | //=------------------------------------------------------------------------= 21 | 22 | @inlinable public static func *=(lhs: inout Self, rhs: Self) { 23 | lhs = lhs * rhs 24 | } 25 | 26 | @inlinable public static func *(lhs: Self, rhs: Self) -> Self { 27 | lhs.withUnsafeBufferPointer { lhs in 28 | rhs.withUnsafeBufferPointer { rhs in 29 | Self.uninitialized(count: lhs.count + rhs.count) { 30 | NBK.SUISS.initialize(&$0, to: lhs, times: rhs) 31 | } 32 | } 33 | } 34 | } 35 | 36 | //=------------------------------------------------------------------------= 37 | // MARK: Transformations x Square 38 | //=------------------------------------------------------------------------= 39 | 40 | @inlinable public mutating func square() { 41 | self = self.squared() 42 | } 43 | 44 | @inlinable public func squared() -> Self { 45 | self.withUnsafeBufferPointer{ words in 46 | Self.uninitialized(count: words.count * 2) { 47 | NBK.SUISS.initialize(&$0, toSquareProductOf: words) 48 | } 49 | } 50 | } 51 | } 52 | 53 | -------------------------------------------------------------------------------- /Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Storage.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Flexible Width x Size x Unsigned x Storage 14 | //*============================================================================* 15 | 16 | extension NBKFlexibleWidth.Magnitude.Storage { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Transformations 20 | //=------------------------------------------------------------------------= 21 | 22 | @inlinable mutating func append(_ word: UInt) { 23 | self.elements.append(word) 24 | } 25 | 26 | @inlinable mutating func resize(minCount: Int) { 27 | self.reserveCapacity(minCount) 28 | appending: while self.elements.count < minCount { 29 | self.elements.append(0 as UInt) 30 | } 31 | } 32 | 33 | @inlinable mutating func resize(maxCount: Int) { 34 | precondition(maxCount.isMoreThanZero) 35 | if self.elements.count > maxCount { 36 | self.elements.removeSubrange(maxCount...) 37 | } 38 | } 39 | 40 | @inlinable mutating func reserveCapacity(_ minCapacity: Int) { 41 | self.elements.reserveCapacity(minCapacity) 42 | } 43 | } 44 | 45 | //=----------------------------------------------------------------------------= 46 | // MARK: + Normalization 47 | //=----------------------------------------------------------------------------= 48 | 49 | extension NBKFlexibleWidth.Magnitude.Storage { 50 | 51 | //=------------------------------------------------------------------------= 52 | // MARK: Accessors 53 | //=------------------------------------------------------------------------= 54 | 55 | @inlinable var isNormal: Bool { 56 | self.elements.count == 1 || !self.elements.last!.isZero 57 | } 58 | 59 | //=------------------------------------------------------------------------= 60 | // MARK: Transformations 61 | //=------------------------------------------------------------------------= 62 | 63 | @inlinable mutating func normalize() { 64 | trimming: while self.elements.count > 1, self.elements.last!.isZero { 65 | self.elements.removeLast() 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Text.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Flexible Width x Text x Unsigned 14 | //*============================================================================* 15 | 16 | extension NBKFlexibleWidth.Magnitude { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Details x Decoding 20 | //=------------------------------------------------------------------------= 21 | 22 | @inlinable public init?(_ description: some StringProtocol, radix: Int) { 23 | let decoder = NBK.IntegerDescription.Decoder(radix: radix) 24 | guard let components: SM = decoder.decode(description) else { return nil } 25 | self.init(sign: components.sign, magnitude: components.magnitude) 26 | } 27 | 28 | //=------------------------------------------------------------------------= 29 | // MARK: Details x Encoding 30 | //=------------------------------------------------------------------------= 31 | 32 | @inlinable public func description(radix: Int, uppercase: Bool) -> String { 33 | NBK.IntegerDescription.Encoder(radix: radix, uppercase: uppercase).encode(self) 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Update.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Flexible Width x Update x Unsigned 14 | //*============================================================================* 15 | 16 | extension NBKFlexibleWidth.Magnitude { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Transformations 20 | //=------------------------------------------------------------------------= 21 | 22 | @inlinable public mutating func update(_ value: Self) { 23 | self.storage.elements.replaceSubrange(self.storage.elements.indices, with: value.storage.elements) 24 | } 25 | 26 | @inlinable public mutating func update(_ value: Digit) { 27 | self.storage.elements.replaceSubrange(self.storage.elements.indices, with: CollectionOfOne(value)) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Words+Collection.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Double Width x Words x Collection 14 | //*============================================================================* 15 | //=----------------------------------------------------------------------------= 16 | // MARK: + Elements 17 | //=----------------------------------------------------------------------------= 18 | 19 | extension NBKFlexibleWidth.Magnitude { 20 | 21 | //=------------------------------------------------------------------------= 22 | // MARK: Accessors 23 | //=------------------------------------------------------------------------= 24 | 25 | /// The least significant word. 26 | /// 27 | /// - Note: This member is required by `Swift.BinaryInteger`. 28 | /// 29 | @inlinable public var _lowWord: UInt { 30 | self.first as UInt 31 | } 32 | 33 | /// The least significant word. 34 | @inlinable public var first: UInt { 35 | self.withUnsafeBufferPointer({ $0[0] }) 36 | } 37 | 38 | /// The most significant word. 39 | @inlinable public var last: UInt { 40 | self.withUnsafeBufferPointer({ $0[$0.count - 1] }) 41 | } 42 | } 43 | 44 | //=----------------------------------------------------------------------------= 45 | // MARK: + Elements x Subscripts 46 | //=----------------------------------------------------------------------------= 47 | 48 | extension NBKFlexibleWidth.Magnitude { 49 | 50 | //=------------------------------------------------------------------------= 51 | // MARK: Accessors 52 | //=------------------------------------------------------------------------= 53 | 54 | @inlinable public subscript(index: Int) -> UInt { 55 | index < self.storage.elements.endIndex ? self.storage.elements[index] : 0 as UInt 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Words.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | 12 | //*============================================================================* 13 | // MARK: * NBK x Flexible Width x Words x Unsigned 14 | //*============================================================================* 15 | 16 | extension NBKFlexibleWidth.Magnitude { 17 | 18 | //=------------------------------------------------------------------------= 19 | // MARK: Initializers 20 | //=------------------------------------------------------------------------= 21 | 22 | @inlinable public init(words: some Sequence) { 23 | self.init(words: words, isSigned: Self.isSigned)! 24 | } 25 | 26 | @inlinable public init?(words: some Sequence, isSigned: Bool) { 27 | var storage = Storage(nonemptying: Elements(words)) 28 | if isSigned && storage.elements.last!.mostSignificantBit { return nil } 29 | 30 | storage.normalize() 31 | self.init(unchecked: storage) 32 | } 33 | 34 | //=------------------------------------------------------------------------= 35 | // MARK: Accessors 36 | //=------------------------------------------------------------------------= 37 | 38 | @inlinable public var count: Int { 39 | self.storage.elements.count 40 | } 41 | 42 | @inlinable public var words: ContiguousArray { 43 | self.storage.elements 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Sources/Numberick/Numberick.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | //*============================================================================* 11 | // MARK: * NBK x Exports 12 | //*============================================================================* 13 | 14 | @_exported import NBKCoreKit 15 | @_exported import NBKDoubleWidthKit 16 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitBenchmarks/Models/NBKPrimeSieve.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | #if !DEBUG 11 | 12 | import NBKCoreKit 13 | import XCTest 14 | 15 | //*============================================================================* 16 | // MARK: * NBK x Prime Sieve 17 | //*============================================================================* 18 | 19 | final class NBKPrimeSieveBenchmarks: XCTestCase { 20 | 21 | typealias T = NBKPrimeSieve 22 | 23 | //=------------------------------------------------------------------------= 24 | // MARK: Tests 25 | //=------------------------------------------------------------------------= 26 | 27 | func testLimit1E6() { 28 | let ((sieve)) = T(cache: .KiB(128), wheel: .x11, culls: .x67, capacity: 0000155611)! 29 | 30 | while sieve.limit < 0001000000 { 31 | ((sieve)).increment() 32 | } 33 | 34 | XCTAssertEqual(sieve.limit, 0002097151) 35 | XCTAssertEqual(sieve.elements.last!, 0002097143) 36 | XCTAssertEqual(sieve.elements.count, 0000155611) 37 | } 38 | 39 | func testLimit1E7() { 40 | let ((sieve)) = T(cache: .KiB(128), wheel: .x11, culls: .x67, capacity: 0000694716)! 41 | 42 | while sieve.limit < 0010000000 { 43 | ((sieve)).increment() 44 | } 45 | 46 | XCTAssertEqual(sieve.limit, 0010485759) 47 | XCTAssertEqual(sieve.elements.last!, 0010485751) 48 | XCTAssertEqual(sieve.elements.count, 0000694716) 49 | } 50 | 51 | func testLimit1E8() { 52 | let ((sieve)) = T(cache: .KiB(128), wheel: .x11, culls: .x67, capacity: 0005797406)! 53 | 54 | while sieve.limit < 0100000000 { 55 | ((sieve)).increment() 56 | } 57 | 58 | XCTAssertEqual(sieve.limit, 0100663295) 59 | XCTAssertEqual(sieve.elements.last!, 0100663291) 60 | XCTAssertEqual(sieve.elements.count, 0005797406) 61 | } 62 | 63 | func testLimit1E9() { 64 | let ((sieve)) = T(cache: .KiB(128), wheel: .x11, culls: .x67, capacity: 0050863957)! 65 | 66 | while sieve.limit < 1000000000 { 67 | ((sieve)).increment() 68 | } 69 | 70 | XCTAssertEqual(sieve.limit, 1000341503) 71 | XCTAssertEqual(sieve.elements.last!, 1000341499) 72 | XCTAssertEqual(sieve.elements.count, 0050863957) 73 | } 74 | } 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitBenchmarks/NBKCoreInteger+Numbers.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | #if !DEBUG 11 | 12 | import NBKCoreKit 13 | import XCTest 14 | 15 | //*============================================================================* 16 | // MARK: * NBK x Core Integer x Numbers x Int 17 | //*============================================================================* 18 | 19 | final class NBKCoreIntegerBenchmarksOnNumbersAsInt: XCTestCase { 20 | 21 | typealias T = Int 22 | 23 | //=------------------------------------------------------------------------= 24 | // MARK: Tests x Constants 25 | //=------------------------------------------------------------------------= 26 | 27 | func testZero() { 28 | for _ in 0 ..< 5_000_000 { 29 | NBK.blackHole(T.zero) 30 | } 31 | } 32 | 33 | func testOne() { 34 | for _ in 0 ..< 5_000_000 { 35 | NBK.blackHole(T.one) 36 | } 37 | } 38 | 39 | func testMin() { 40 | for _ in 0 ..< 5_000_000 { 41 | NBK.blackHole(T.min) 42 | } 43 | } 44 | 45 | func testMax() { 46 | for _ in 0 ..< 5_000_000 { 47 | NBK.blackHole(T.max) 48 | } 49 | } 50 | } 51 | 52 | //*============================================================================* 53 | // MARK: * NBK x Core Integer x Numbers x UInt 54 | //*============================================================================* 55 | 56 | final class NBKCoreIntegerBenchmarksOnNumbersAsUInt: XCTestCase { 57 | 58 | typealias T = UInt 59 | 60 | //=------------------------------------------------------------------------= 61 | // MARK: Tests x Constants 62 | //=------------------------------------------------------------------------= 63 | 64 | func testZero() { 65 | for _ in 0 ..< 5_000_000 { 66 | NBK.blackHole(T.zero) 67 | } 68 | } 69 | 70 | func testOne() { 71 | for _ in 0 ..< 5_000_000 { 72 | NBK.blackHole(T.one) 73 | } 74 | } 75 | 76 | func testMin() { 77 | for _ in 0 ..< 5_000_000 { 78 | NBK.blackHole(T.min) 79 | } 80 | } 81 | 82 | func testMax() { 83 | for _ in 0 ..< 5_000_000 { 84 | NBK.blackHole(T.max) 85 | } 86 | } 87 | } 88 | 89 | #endif 90 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitBenchmarks/NBKCoreInteger+Words.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | #if !DEBUG 11 | 12 | import NBKCoreKit 13 | import XCTest 14 | 15 | //*============================================================================* 16 | // MARK: * NBK x Core Integer x Words 17 | //*============================================================================* 18 | 19 | final class NBKCoreIntegerBenchmarksOnWords: XCTestCase { 20 | 21 | typealias T = Int 22 | typealias M = UInt 23 | 24 | //=------------------------------------------------------------------------= 25 | // MARK: Tests 26 | //=------------------------------------------------------------------------= 27 | 28 | func testInt32FromWords() { 29 | var x0 = NBK.blackHoleIdentity([1 ] as [UInt]) 30 | var x1 = NBK.blackHoleIdentity([1, 2] as [UInt]) 31 | 32 | for _ in 0 ..< 5_000_000 { 33 | NBK.blackHole(Int32(words: x0)) 34 | NBK.blackHole(Int32(words: x1)) 35 | 36 | NBK.blackHoleInoutIdentity(&x0) 37 | NBK.blackHoleInoutIdentity(&x1) 38 | } 39 | } 40 | 41 | func testInt64FromWords() { 42 | var x0 = NBK.blackHoleIdentity([1 ] as [UInt]) 43 | var x1 = NBK.blackHoleIdentity([1, 2] as [UInt]) 44 | 45 | for _ in 0 ..< 5_000_000 { 46 | NBK.blackHole(Int64(words: x0)) 47 | NBK.blackHole(Int64(words: x1)) 48 | 49 | NBK.blackHoleInoutIdentity(&x0) 50 | NBK.blackHoleInoutIdentity(&x1) 51 | } 52 | } 53 | 54 | func testUInt32FromWords() { 55 | var x0 = NBK.blackHoleIdentity([1 ] as [UInt]) 56 | var x1 = NBK.blackHoleIdentity([1, 2] as [UInt]) 57 | 58 | for _ in 0 ..< 5_000_000 { 59 | NBK.blackHole(UInt32(words: x0)) 60 | NBK.blackHole(UInt32(words: x1)) 61 | 62 | NBK.blackHoleInoutIdentity(&x0) 63 | NBK.blackHoleInoutIdentity(&x1) 64 | } 65 | } 66 | 67 | func testUInt64FromWords() { 68 | var x0 = NBK.blackHoleIdentity([1 ] as [UInt]) 69 | var x1 = NBK.blackHoleIdentity([1, 2] as [UInt]) 70 | 71 | for _ in 0 ..< 5_000_000 { 72 | NBK.blackHole(UInt64(words: x0)) 73 | NBK.blackHole(UInt64(words: x1)) 74 | 75 | NBK.blackHoleInoutIdentity(&x0) 76 | NBK.blackHoleInoutIdentity(&x1) 77 | } 78 | } 79 | } 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitBenchmarks/Private/NBKMaybeTwosComplement.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | #if !DEBUG 11 | 12 | import NBKCoreKit 13 | import XCTest 14 | 15 | private typealias X = [UInt] 16 | private typealias X64 = [UInt64] 17 | private typealias X32 = [UInt32] 18 | 19 | //*============================================================================* 20 | // MARK: * NBK x Maybe Two's Complement 21 | //*============================================================================* 22 | 23 | final class NBKMaybeTwosComplementBenchmarks: XCTestCase { 24 | 25 | typealias T = NBK.MaybeTwosComplement 26 | 27 | //=------------------------------------------------------------------------= 28 | // MARK: Tests 29 | //=------------------------------------------------------------------------= 30 | 31 | func testArray() { 32 | var abc = NBK.blackHoleIdentity(X(repeating: 144, count: 144)) 33 | var xyz = NBK.blackHoleIdentity(X(repeating: 144, count: 144)) 34 | 35 | for _ in 0 ..< 1_000_000 { 36 | NBK.blackHole(abc.reduce(0, &+)) 37 | NBK.blackHole(xyz.reduce(0, &+)) 38 | 39 | NBK.blackHoleInoutIdentity(&abc) 40 | NBK.blackHoleInoutIdentity(&xyz) 41 | } 42 | } 43 | 44 | func testMaybeTwosComplement() { 45 | var abc = NBK.blackHoleIdentity(T(X(repeating: 144, count: 144), formTwosComplement: true )) 46 | var xyz = NBK.blackHoleIdentity(T(X(repeating: 144, count: 144), formTwosComplement: false)) 47 | 48 | for _ in 0 ..< 1_000_000 { 49 | NBK.blackHole(abc.reduce(0, &+)) 50 | NBK.blackHole(xyz.reduce(0, &+)) 51 | 52 | NBK.blackHoleInoutIdentity(&abc) 53 | NBK.blackHoleInoutIdentity(&xyz) 54 | } 55 | } 56 | } 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitBenchmarks/Private/NBKProperBinaryInteger+ModularMultiplicativeInverse.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | #if !DEBUG 11 | 12 | import NBKCoreKit 13 | import XCTest 14 | 15 | private typealias X = [UInt] 16 | private typealias X64 = [UInt64] 17 | private typealias X32 = [UInt32] 18 | 19 | //*============================================================================* 20 | // MARK: * NBK x Proper Binary Integer x Modular Multiplicative Inverse 21 | //*============================================================================* 22 | 23 | final class NBKProperBinaryIntegerBenchmarksOnModularMultiplicativeInverse: XCTestCase { 24 | 25 | typealias T = NBK.PBI 26 | 27 | //=------------------------------------------------------------------------= 28 | // MARK: Tests 29 | //=------------------------------------------------------------------------= 30 | 31 | func testInt8s() { 32 | for _ in 0 ..< 50 { 33 | for lhs in Int8.min ... Int8.max { 34 | for rhs in Int8.min ... Int8.max { 35 | NBK.blackHole(T.modularMultiplicativeInverse(of: lhs, modulo: rhs)) 36 | } 37 | } 38 | } 39 | } 40 | 41 | func testUInt8s() { 42 | for _ in 0 ..< 50 { 43 | for lhs in UInt8.min ... UInt8.max { 44 | for rhs in UInt8.min ... UInt8.max { 45 | NBK.blackHole(T.modularMultiplicativeInverse(of: lhs, modulo: rhs)) 46 | } 47 | } 48 | } 49 | } 50 | } 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitBenchmarks/Private/NBKProperBinaryInteger+Roots.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | #if !DEBUG 11 | 12 | import NBKCoreKit 13 | import XCTest 14 | 15 | private typealias X = [UInt] 16 | private typealias X64 = [UInt64] 17 | private typealias X32 = [UInt32] 18 | 19 | //*============================================================================* 20 | // MARK: * NBK x Proper Binary Integer x Roots 21 | //*============================================================================* 22 | 23 | final class NBKProperBinaryIntegerBenchmarksOnRoots: XCTestCase { 24 | 25 | typealias T = NBK.PBI 26 | 27 | //=------------------------------------------------------------------------= 28 | // MARK: Tests 29 | //=------------------------------------------------------------------------= 30 | 31 | func testSquareRootAsInt32() { 32 | var power = NBK.blackHoleIdentity(Int32.max) 33 | 34 | for _ in 0 ... 5_000_000 { 35 | NBK.blackHole(T.squareRootByNewtonsMethod(of: power)) 36 | NBK.blackHoleInoutIdentity(&power) 37 | } 38 | } 39 | 40 | func testSquareRootAsInt64() { 41 | var power = NBK.blackHoleIdentity(Int64.max) 42 | 43 | for _ in 0 ... 5_000_000 { 44 | NBK.blackHole(T.squareRootByNewtonsMethod(of: power)) 45 | NBK.blackHoleInoutIdentity(&power) 46 | } 47 | } 48 | 49 | func testSquareRootAsUInt32() { 50 | var power = NBK.blackHoleIdentity(UInt32.max) 51 | 52 | for _ in 0 ... 5_000_000 { 53 | NBK.blackHole(T.squareRootByNewtonsMethod(of: power)) 54 | NBK.blackHoleInoutIdentity(&power) 55 | } 56 | } 57 | 58 | func testSquareRootAsUInt64() { 59 | var power = NBK.blackHoleIdentity(UInt64.max) 60 | 61 | for _ in 0 ... 5_000_000 { 62 | NBK.blackHole(T.squareRootByNewtonsMethod(of: power)) 63 | NBK.blackHoleInoutIdentity(&power) 64 | } 65 | } 66 | } 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitBenchmarks/Private/NBKStrictSignedInteger+Comparisons.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | #if !DEBUG 11 | 12 | import NBKCoreKit 13 | import XCTest 14 | 15 | private typealias X = [UInt] 16 | private typealias X64 = [UInt64] 17 | private typealias X32 = [UInt32] 18 | 19 | //*============================================================================* 20 | // MARK: * NBK x Strict Signed Integer x Comparisons 21 | //*============================================================================* 22 | 23 | final class NBKStrictSignedIntegerBenchmarksOnComparisons: XCTestCase { 24 | 25 | typealias T = NBK.StrictSignedInteger> 26 | 27 | //=------------------------------------------------------------------------= 28 | // MARK: Tests 29 | //=------------------------------------------------------------------------= 30 | 31 | func testCompare() { 32 | var lhs = NBK.blackHoleIdentity([1, 2, 3, 4] as X) 33 | var rhs = NBK.blackHoleIdentity([1, 2, 3, 4] as X) 34 | var xyz = NBK.blackHoleIdentity((1) as Int) 35 | 36 | for _ in 0 ..< 2_500_000 { 37 | lhs.withUnsafeBufferPointer { lhs in 38 | rhs.withUnsafeBufferPointer { rhs in 39 | NBK.blackHole(T.compare(lhs, to: rhs)) 40 | }} 41 | 42 | NBK.blackHoleInoutIdentity(&lhs) 43 | NBK.blackHoleInoutIdentity(&rhs) 44 | NBK.blackHoleInoutIdentity(&xyz) 45 | } 46 | } 47 | 48 | func testCompareAtIndex() { 49 | var lhs = NBK.blackHoleIdentity([1, 2, 3, 4] as X) 50 | var rhs = NBK.blackHoleIdentity([2, 3, 4, 0] as X) 51 | var xyz = NBK.blackHoleIdentity((1) as Int) 52 | 53 | for _ in 0 ..< 2_500_000 { 54 | lhs.withUnsafeBufferPointer { lhs in 55 | rhs.withUnsafeBufferPointer { rhs in 56 | NBK.blackHole(T.compare(lhs, to: rhs, at: xyz)) 57 | }} 58 | 59 | NBK.blackHoleInoutIdentity(&lhs) 60 | NBK.blackHoleInoutIdentity(&rhs) 61 | NBK.blackHoleInoutIdentity(&xyz) 62 | } 63 | } 64 | } 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitBenchmarks/Private/NBKStrictUnsignedInteger+Comparisons.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | #if !DEBUG 11 | 12 | import NBKCoreKit 13 | import XCTest 14 | 15 | private typealias X = [UInt] 16 | private typealias X64 = [UInt64] 17 | private typealias X32 = [UInt32] 18 | 19 | //*============================================================================* 20 | // MARK: * NBK x Strict Unsigned Integer x Comparisons x Sub Sequence 21 | //*============================================================================* 22 | 23 | final class NBKStrictUnsignedIntegerBenchmarksOnComparisonsAsSubSequence: XCTestCase { 24 | 25 | typealias T = NBK.StrictUnsignedInteger>.SubSequence 26 | 27 | //=------------------------------------------------------------------------= 28 | // MARK: Tests 29 | //=------------------------------------------------------------------------= 30 | 31 | func testCompare() { 32 | var lhs = NBK.blackHoleIdentity([1, 2, 3, 4] as X) 33 | var rhs = NBK.blackHoleIdentity([1, 2, 3, 4] as X) 34 | var xyz = NBK.blackHoleIdentity((1) as Int) 35 | 36 | for _ in 0 ..< 2_500_000 { 37 | lhs.withUnsafeBufferPointer { lhs in 38 | rhs.withUnsafeBufferPointer { rhs in 39 | NBK.blackHole(T.compare(lhs, to: rhs)) 40 | }} 41 | 42 | NBK.blackHoleInoutIdentity(&lhs) 43 | NBK.blackHoleInoutIdentity(&rhs) 44 | NBK.blackHoleInoutIdentity(&xyz) 45 | } 46 | } 47 | 48 | func testCompareAtIndex() { 49 | var lhs = NBK.blackHoleIdentity([1, 2, 3, 4] as X) 50 | var rhs = NBK.blackHoleIdentity([2, 3, 4, 0] as X) 51 | var xyz = NBK.blackHoleIdentity((1) as Int) 52 | 53 | for _ in 0 ..< 2_500_000 { 54 | lhs.withUnsafeBufferPointer { lhs in 55 | rhs.withUnsafeBufferPointer { rhs in 56 | NBK.blackHole(T.compare(lhs, to: rhs, at: xyz)) 57 | }} 58 | 59 | NBK.blackHoleInoutIdentity(&lhs) 60 | NBK.blackHoleInoutIdentity(&rhs) 61 | NBK.blackHoleInoutIdentity(&xyz) 62 | } 63 | } 64 | } 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitBenchmarks/Private/NBKSuccintInt.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | #if !DEBUG 11 | 12 | import NBKCoreKit 13 | import XCTest 14 | 15 | private typealias X = [UInt] 16 | private typealias X64 = [UInt64] 17 | private typealias X32 = [UInt32] 18 | 19 | //*============================================================================* 20 | // MARK: * NBK x Succinct Int 21 | //*============================================================================* 22 | 23 | final class NBKSuccinctIntBenchmarks: XCTestCase { 24 | 25 | typealias T = NBK.SuccinctInt 26 | 27 | //=------------------------------------------------------------------------= 28 | // MARK: Tests 29 | //=------------------------------------------------------------------------= 30 | 31 | func testFromStrictSignedInteger() { 32 | var abc = NBK.blackHoleIdentity([ 1, 0, 2, 0, 3, 0, 4, 0] as X) 33 | var xyz = NBK.blackHoleIdentity([~1, ~0, ~2, ~0, ~3, ~0, ~4, ~0] as X) 34 | 35 | for _ in 0 ..< 5_000_000 { 36 | abc.withUnsafeBufferPointer { abc in 37 | xyz.withUnsafeBufferPointer { xyz in 38 | NBK.blackHole(T(fromStrictSignedIntegerSubSequence: abc)!) 39 | NBK.blackHole(T(fromStrictSignedIntegerSubSequence: xyz)!) 40 | }} 41 | 42 | NBK.blackHoleInoutIdentity(&abc) 43 | NBK.blackHoleInoutIdentity(&xyz) 44 | } 45 | } 46 | 47 | func testFromStrictUnsignedIntegerSubSequence() { 48 | var abc = NBK.blackHoleIdentity([ 1, 0, 2, 0, 3, 0, 4, 0] as X) 49 | var xyz = NBK.blackHoleIdentity([~1, ~0, ~2, ~0, ~3, ~0, ~4, ~0] as X) 50 | 51 | for _ in 0 ..< 5_000_000 { 52 | abc.withUnsafeBufferPointer { abc in 53 | xyz.withUnsafeBufferPointer { xyz in 54 | NBK.blackHole(T(fromStrictUnsignedIntegerSubSequence: abc)) 55 | NBK.blackHole(T(fromStrictUnsignedIntegerSubSequence: xyz)) 56 | }} 57 | 58 | NBK.blackHoleInoutIdentity(&abc) 59 | NBK.blackHoleInoutIdentity(&xyz) 60 | } 61 | } 62 | } 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitTests/Models/NBKEndianness.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | import XCTest 12 | 13 | //*============================================================================* 14 | // MARK: * NBK x Endianness 15 | //*============================================================================* 16 | 17 | final class NBKEndiannessTests: XCTestCase { 18 | 19 | typealias T = NBKEndianness 20 | 21 | //=------------------------------------------------------------------------= 22 | // MARK: Tests 23 | //=------------------------------------------------------------------------= 24 | 25 | func testSystem() { 26 | #if _endian(little) 27 | XCTAssertEqual(T.system, T.little) 28 | #elseif _endian(big) 29 | XCTAssertEqual(T.system, T.big) 30 | #endif 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitTests/NBKCoreInteger+Bits.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2022 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | import XCTest 12 | 13 | //*============================================================================* 14 | // MARK: * NBK x Core Integer x Bits 15 | //*============================================================================* 16 | 17 | final class NBKCoreIntegerTestsOnBits: XCTestCase { 18 | 19 | typealias T = any NBKCoreInteger.Type 20 | 21 | //=------------------------------------------------------------------------= 22 | // MARK: State 23 | //=------------------------------------------------------------------------= 24 | 25 | let types: [T] = NBKCoreIntegerTests.types 26 | 27 | //=------------------------------------------------------------------------= 28 | // MARK: Tests 29 | //=------------------------------------------------------------------------= 30 | 31 | func testFromBit() { 32 | func whereIs(_ type: T.Type) where T: NBKCoreInteger { 33 | XCTAssertEqual(T(bit: false), T( )) 34 | XCTAssertEqual(T(bit: true ), T(1)) 35 | } 36 | 37 | for type in types { 38 | whereIs(type) 39 | } 40 | } 41 | 42 | func testFromRepeatingBit() { 43 | func whereIs(_ type: T.Type) where T: NBKCoreInteger { 44 | XCTAssertEqual(T(repeating: false), T( )) 45 | XCTAssertEqual(T(repeating: true ), ~T( )) 46 | } 47 | 48 | for type in types { 49 | whereIs(type) 50 | } 51 | } 52 | 53 | //=------------------------------------------------------------------------= 54 | // MARK: Tests x Accessors 55 | //=------------------------------------------------------------------------= 56 | 57 | func testMostSignificantBit() { 58 | for type: T in types { 59 | XCTAssertEqual(type.min .mostSignificantBit, type.isSigned) 60 | XCTAssertEqual(type.zero.mostSignificantBit, false) 61 | XCTAssertEqual(type.max .mostSignificantBit, !type.isSigned) 62 | } 63 | } 64 | 65 | func testLeastSignificantBit() { 66 | for type: T in types { 67 | XCTAssertEqual(type.min .leastSignificantBit, false) 68 | XCTAssertEqual(type.zero.leastSignificantBit, false) 69 | XCTAssertEqual(type.max .leastSignificantBit, true ) 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitTests/NBKCoreInteger.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2022 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | import XCTest 12 | 13 | //*============================================================================* 14 | // MARK: * NBK x Core Integer 15 | //*============================================================================* 16 | 17 | final class NBKCoreIntegerTests: XCTestCase { 18 | 19 | typealias T = any NBKCoreInteger.Type 20 | 21 | //=------------------------------------------------------------------------= 22 | // MARK: State 23 | //=------------------------------------------------------------------------= 24 | 25 | static let signed: [T] = [ Int.self, Int8.self, Int16.self, Int32.self, Int64.self] 26 | static let unsigned: [T] = [UInt.self, UInt8.self, UInt16.self, UInt32.self, UInt64.self] 27 | static let types: [T] = signed + unsigned 28 | 29 | //=------------------------------------------------------------------------= 30 | // MARK: State 31 | //=------------------------------------------------------------------------= 32 | 33 | let types: [T] = NBKCoreIntegerTests.types 34 | 35 | //=------------------------------------------------------------------------= 36 | // MARK: Tests 37 | //=------------------------------------------------------------------------= 38 | 39 | func testIsSigned() { 40 | XCTAssert(Self .signed.allSatisfy({ $0.isSigned })) 41 | XCTAssert(Self.unsigned.allSatisfy({ !$0.isSigned })) 42 | } 43 | 44 | func testNBKBinaryInteger() { 45 | XCTAssertEqual(10, types.compactMap({ $0 as any NBKBinaryInteger.Type }).count) 46 | } 47 | 48 | func testNBKBitPatternConvertible() { 49 | XCTAssertEqual(10, types.compactMap({ $0 as any NBKBitPatternConvertible.Type }).count) 50 | } 51 | 52 | func testNBKCoreInteger() { 53 | XCTAssertEqual(10, types.compactMap({ $0 as any NBKCoreInteger.Type }).count) 54 | } 55 | 56 | func testNBKFixedWidthInteger() { 57 | XCTAssertEqual(10, types.compactMap({ $0 as any NBKFixedWidthInteger.Type }).count) 58 | } 59 | 60 | func testNBKSignedInteger() { 61 | XCTAssertEqual(05, types.compactMap({ $0 as? any NBKSignedInteger.Type }).count) 62 | } 63 | 64 | func testNBKUnsignedInteger() { 65 | XCTAssertEqual(05, types.compactMap({ $0 as? any NBKUnsignedInteger.Type }).count) 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitTests/Private/NBK+Data.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | import XCTest 12 | 13 | //*============================================================================* 14 | // MARK: * NBK x Data 15 | //*============================================================================* 16 | 17 | final class NBKTestsOnData: XCTestCase { 18 | 19 | //=------------------------------------------------------------------------= 20 | // MARK: Tests 21 | //=------------------------------------------------------------------------= 22 | 23 | func testWithUnsafeTemporaryAllocationWithCount() { 24 | for count in 0 ... 1_000 { 25 | NBK.withUnsafeTemporaryAllocation(of: UInt.self, count: count) { 26 | XCTAssertEqual ($0.count,count) 27 | XCTAssertNotNil($0.baseAddress) 28 | } 29 | } 30 | } 31 | 32 | func testUnwrappingUnsafeBufferPointer() { 33 | NBK.withUnsafeTemporaryAllocation(of: UInt8.self, count: 1) { 34 | XCTAssertNil (NBK.unwrapping(UnsafeBufferPointer(start: nil, count: 0))) 35 | XCTAssertNotNil(NBK.unwrapping(UnsafeBufferPointer(start: $0.baseAddress!, count: 0))) 36 | XCTAssertNotNil(NBK.unwrapping(UnsafeBufferPointer(start: $0.baseAddress!, count: 1))) 37 | } 38 | } 39 | 40 | func testUnwrappingUnsafeMutableBufferPointer() { 41 | NBK.withUnsafeTemporaryAllocation(of: UInt8.self, count: 1) { 42 | XCTAssertNil( NBK.unwrapping(UnsafeMutableBufferPointer(start: nil, count: 0))) 43 | XCTAssertNotNil(NBK.unwrapping(UnsafeMutableBufferPointer(start: $0.baseAddress!, count: 0))) 44 | XCTAssertNotNil(NBK.unwrapping(UnsafeMutableBufferPointer(start: $0.baseAddress!, count: 1))) 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitTests/Private/NBK+Endianness.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | import XCTest 12 | 13 | //*============================================================================* 14 | // MARK: * NBK x Endianness 15 | //*============================================================================* 16 | 17 | final class NBKTestsOnEndianness: XCTestCase { 18 | 19 | //=------------------------------------------------------------------------= 20 | // MARK: Tests 21 | //=------------------------------------------------------------------------= 22 | 23 | func testIsLittleOrBigEndian() { 24 | #if _endian(big) 25 | XCTAssertTrue (NBK.isBigEndian) 26 | XCTAssertFalse(NBK.isLittleEndian) 27 | #elseif _endian(little) 28 | XCTAssertFalse(NBK.isBigEndian) 29 | XCTAssertTrue (NBK.isLittleEndian) 30 | #endif 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitTests/Private/NBK+Sign.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | import XCTest 12 | 13 | //*============================================================================* 14 | // MARK: * NBK x Sign 15 | //*============================================================================* 16 | 17 | final class NBKTestsOnSign: XCTestCase { 18 | 19 | typealias T = FloatingPointSign 20 | 21 | //=------------------------------------------------------------------------= 22 | // MARK: Tests 23 | //=------------------------------------------------------------------------= 24 | 25 | func testASCII() { 26 | XCTAssertEqual(NBK.ascii(T.plus ), UInt8(ascii: "+")) 27 | XCTAssertEqual(NBK.ascii(T.minus), UInt8(ascii: "-")) 28 | } 29 | 30 | func testBit() { 31 | XCTAssertEqual(NBK.bit(T.plus ), false) 32 | XCTAssertEqual(NBK.bit(T.minus), true ) 33 | } 34 | 35 | func testSign() { 36 | XCTAssertEqual(NBK.sign(false), T.plus ) 37 | XCTAssertEqual(NBK.sign(true ), T.minus) 38 | } 39 | 40 | //=------------------------------------------------------------------------= 41 | // MARK: Tests x Transformations 42 | //=------------------------------------------------------------------------= 43 | 44 | func testNot() { 45 | XCTAssertEqual(NBK.not(T.plus ), T.minus) 46 | XCTAssertEqual(NBK.not(T.minus), T.plus ) 47 | } 48 | 49 | func testAnd() { 50 | XCTAssertEqual(NBK.and(T.plus, T.plus ), T.plus ) 51 | XCTAssertEqual(NBK.and(T.plus, T.minus), T.plus ) 52 | XCTAssertEqual(NBK.and(T.minus, T.plus ), T.plus ) 53 | XCTAssertEqual(NBK.and(T.minus, T.minus), T.minus) 54 | } 55 | 56 | func testOr() { 57 | XCTAssertEqual(NBK.or (T.plus, T.plus ), T.plus ) 58 | XCTAssertEqual(NBK.or (T.plus, T.minus), T.minus) 59 | XCTAssertEqual(NBK.or (T.minus, T.plus ), T.minus) 60 | XCTAssertEqual(NBK.or (T.minus, T.minus), T.minus) 61 | } 62 | 63 | func testXor() { 64 | XCTAssertEqual(NBK.xor(T.plus, T.plus ), T.plus ) 65 | XCTAssertEqual(NBK.xor(T.plus, T.minus), T.minus) 66 | XCTAssertEqual(NBK.xor(T.minus, T.plus ), T.minus) 67 | XCTAssertEqual(NBK.xor(T.minus, T.minus), T.plus ) 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitTests/Private/NBKCyclicIterator.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | import XCTest 12 | 13 | //*============================================================================* 14 | // MARK: * NBK x Cyclic Iterator 15 | //*============================================================================* 16 | 17 | final class NBKCyclicIteratorTests: XCTestCase { 18 | 19 | typealias T = NBK.CyclicIterator 20 | 21 | //=------------------------------------------------------------------------= 22 | // MARK: Tests 23 | //=------------------------------------------------------------------------= 24 | 25 | func testInit() { 26 | XCTAssertNil( T([ ] as [UInt])) 27 | XCTAssertNotNil(T([1 ] as [UInt])) 28 | XCTAssertNotNil(T([1, 2 ] as [UInt])) 29 | XCTAssertNotNil(T([1, 2, 3] as [UInt])) 30 | } 31 | 32 | func testIterating() { 33 | NBKAssertCyclicIterator(T([1, 2, 3] as [UInt])!, 0, [ ] as [UInt]) 34 | NBKAssertCyclicIterator(T([1, 2, 3] as [UInt])!, 1, [1 ] as [UInt]) 35 | NBKAssertCyclicIterator(T([1, 2, 3] as [UInt])!, 2, [1, 2 ] as [UInt]) 36 | NBKAssertCyclicIterator(T([1, 2, 3] as [UInt])!, 3, [1, 2, 3 ] as [UInt]) 37 | NBKAssertCyclicIterator(T([1, 2, 3] as [UInt])!, 4, [1, 2, 3, 1 ] as [UInt]) 38 | NBKAssertCyclicIterator(T([1, 2, 3] as [UInt])!, 5, [1, 2, 3, 1, 2 ] as [UInt]) 39 | NBKAssertCyclicIterator(T([1, 2, 3] as [UInt])!, 6, [1, 2, 3, 1, 2, 3] as [UInt]) 40 | } 41 | } 42 | 43 | //*============================================================================* 44 | // MARK: * NBK x Cyclic Iterator x Assertions 45 | //*============================================================================* 46 | 47 | private func NBKAssertCyclicIterator( 48 | _ iterator: NBK.CyclicIterator, _ count: Int, _ expectation: [Base.Element], 49 | file: StaticString = #file, line: UInt = #line) where Base.Element: Equatable { 50 | var iterator = iterator 51 | var elements = [Base.Element]() 52 | 53 | for _ in 0 ..< count { 54 | elements.append(iterator.next()) 55 | } 56 | 57 | XCTAssertEqual(elements, expectation, file: file, line: line) 58 | } 59 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitTests/Private/NBKIntegerDescription+RadixSolution.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | import XCTest 12 | 13 | //*============================================================================* 14 | // MARK: * NBK x Integer Description x Radix Solution 15 | //*============================================================================* 16 | 17 | final class NBKIntegerDescriptionTestsOnRadixSolution: XCTestCase { 18 | 19 | //=------------------------------------------------------------------------= 20 | // MARK: Tests 21 | //=------------------------------------------------------------------------= 22 | 23 | func testAnyRadixSolution() { 24 | func whereIs(_ element: T.Type) where T: NBKCoreInteger & NBKUnsignedInteger { 25 | for radix in 2 ... 36 { 26 | let solution = NBK.IntegerDescription.AnyRadixSolution(radix) 27 | XCTAssertEqual(solution.base, T(radix)) 28 | 29 | var product = HL(0 as T, 1 as T) 30 | for _ in 0 ..< solution.exponent { 31 | XCTAssert(product.high.isZero) 32 | product = product.low.multipliedFullWidth(by: T.Magnitude(radix)) 33 | } 34 | 35 | XCTAssertEqual(product.low, solution.power) 36 | XCTAssertEqual(product.low.isZero, [2, 4, 16].contains(radix)) 37 | XCTAssertEqual(product.high, /**/ [2, 4, 16].contains(radix) ? 1 : 0) 38 | XCTAssertEqual(product.low.multipliedReportingOverflow(by: T.Magnitude(radix)).overflow, ![2, 4, 16].contains(radix)) 39 | } 40 | } 41 | 42 | for element: any (NBKCoreInteger & NBKUnsignedInteger).Type in [UInt.self, UInt8.self, UInt16.self, UInt32.self, UInt64.self] { 43 | whereIs(element) 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitTests/Private/NBKStrictBinaryInteger+Complements.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | import XCTest 12 | 13 | private typealias X = [UInt] 14 | private typealias X64 = [UInt64] 15 | private typealias X32 = [UInt32] 16 | 17 | //*============================================================================* 18 | // MARK: * NBK x Strict Binary Integer x Complements x Sub Sequence 19 | //*============================================================================* 20 | 21 | final class NBKStrictBinaryIntegerTestsOnComplementsAsSubSequence: XCTestCase { 22 | 23 | //=------------------------------------------------------------------------= 24 | // MARK: Tests 25 | //=------------------------------------------------------------------------= 26 | 27 | func testOnesComplement() { 28 | NBKAssertOnesComplement([ 0, 0, 0, 0], [~0, ~0, ~0, ~0]) 29 | NBKAssertOnesComplement([~0, ~0, ~0, ~0], [ 0, 0, 0, 0]) 30 | NBKAssertOnesComplement([ 1, 2, 3, 4], [~1, ~2, ~3, ~4]) 31 | NBKAssertOnesComplement([~1, ~2, ~3, ~4], [ 1, 2, 3, 4]) 32 | } 33 | 34 | func testTwosComplement() { 35 | NBKAssertTwosComplement([ 0, 0, 0, 0], [ 0, 0, 0, 0]) 36 | NBKAssertTwosComplement([~0, ~0, ~0, ~0], [ 1, 0, 0, 0]) 37 | NBKAssertTwosComplement([ 1, 2, 3, 4], [~0, ~2, ~3, ~4]) 38 | NBKAssertTwosComplement([~1, ~2, ~3, ~4], [ 2, 2, 3, 4]) 39 | } 40 | } 41 | 42 | //*============================================================================* 43 | // MARK: * NBK x Strict Binary Integer x Complements x Assertions 44 | //*============================================================================* 45 | 46 | private func NBKAssertOnesComplement( 47 | _ operand: [UInt], _ result: [UInt], 48 | file: StaticString = #file, line: UInt = #line) { 49 | //=------------------------------------------= 50 | typealias SBI = NBK.SBISS 51 | //=------------------------------------------= 52 | brr: do { 53 | var operand = operand 54 | SBI.formOnesComplement(&operand) 55 | XCTAssertEqual(operand, result, file: file, line: line) 56 | } 57 | } 58 | 59 | private func NBKAssertTwosComplement( 60 | _ operand: [UInt], _ result: [UInt], 61 | file: StaticString = #file, line: UInt = #line) { 62 | //=------------------------------------------= 63 | typealias SBI = NBK.SBISS 64 | //=------------------------------------------= 65 | brr: do { 66 | var operand = operand 67 | SBI.formTwosComplement(&operand) 68 | XCTAssertEqual(operand, result, file: file, line: line) 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitTests/Private/NBKStrictBinaryInteger+Words.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | import XCTest 12 | 13 | private typealias X = [UInt] 14 | private typealias X64 = [UInt64] 15 | private typealias X32 = [UInt32] 16 | 17 | //*============================================================================* 18 | // MARK: * NBK x Strict Binary Integer x Words 19 | //*============================================================================* 20 | 21 | final class NBKStrictBinaryIntegerTestsOnWords: XCTestCase { 22 | 23 | //=------------------------------------------------------------------------= 24 | // MARK: Tests 25 | //=------------------------------------------------------------------------= 26 | 27 | func testWithUnsafeWordsOfInt() { 28 | NBKAssertWithBitPatternOf(Int(-4), [~3]) 29 | NBKAssertWithBitPatternOf(Int(-3), [~2]) 30 | NBKAssertWithBitPatternOf(Int(-2), [~1]) 31 | NBKAssertWithBitPatternOf(Int(-1), [~0]) 32 | NBKAssertWithBitPatternOf(Int( 0), [ 0]) 33 | NBKAssertWithBitPatternOf(Int( 1), [ 1]) 34 | NBKAssertWithBitPatternOf(Int( 2), [ 2]) 35 | NBKAssertWithBitPatternOf(Int( 3), [ 3]) 36 | } 37 | 38 | func testWithUnsafeWordsOfUInt() { 39 | NBKAssertWithBitPatternOf(UInt(0), [ 0]) 40 | NBKAssertWithBitPatternOf(UInt(1), [ 1]) 41 | NBKAssertWithBitPatternOf(UInt(2), [ 2]) 42 | NBKAssertWithBitPatternOf(UInt(3), [ 3]) 43 | NBKAssertWithBitPatternOf(UInt(4), [ 4]) 44 | NBKAssertWithBitPatternOf(UInt(5), [ 5]) 45 | NBKAssertWithBitPatternOf(UInt(6), [ 6]) 46 | NBKAssertWithBitPatternOf(UInt(7), [ 7]) 47 | } 48 | } 49 | 50 | //*============================================================================* 51 | // MARK: * NBK x Strict Binary Integer x Words x Assertions 52 | //*============================================================================* 53 | 54 | private func NBKAssertWithBitPatternOf( 55 | _ lhs: T, _ rhs: [T.BitPattern], 56 | file: StaticString = #file, line: UInt = #line) { 57 | //=------------------------------------------= 58 | typealias SBI = NBK.StrictBinaryInteger> 59 | //=------------------------------------------= 60 | SBI.withUnsafeBufferPointer(to: lhs.bitPattern) { 61 | XCTAssertEqual(Array($0), rhs, file: file, line: line) 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitTests/Private/NBKStrictUnsignedInteger+Multiplication.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | import XCTest 12 | 13 | private typealias X = [UInt] 14 | private typealias X64 = [UInt64] 15 | private typealias X32 = [UInt32] 16 | 17 | //*============================================================================* 18 | // MARK: * NBK x Strict Unsigned Integer x Multiplication x Sub Sequence 19 | //*============================================================================* 20 | 21 | final class NBKStrictUnsignedIntegerTestsOnMultiplicationAsSubSequence: XCTestCase { 22 | 23 | //=------------------------------------------------------------------------= 24 | // MARK: Tests x Digit + Digit 25 | //=------------------------------------------------------------------------= 26 | 27 | func testMultiplicationByDigitWithAddition() { 28 | NBKAssertMultiplicationByDigitWithAddition([~0, ~0, ~0, ~0] as X, 0, 0, [ 0, 0, 0, 0] as X, 0) 29 | NBKAssertMultiplicationByDigitWithAddition([~0, ~0, ~0, ~0] as X, 0, ~0, [~0, 0, 0, 0] as X, 0) 30 | NBKAssertMultiplicationByDigitWithAddition([~0, ~0, ~0, ~0] as X, ~0, 0, [ 1, ~0, ~0, ~0] as X, ~1) 31 | NBKAssertMultiplicationByDigitWithAddition([~0, ~0, ~0, ~0] as X, ~0, ~0, [ 0, 0, 0, 0] as X, ~0) 32 | } 33 | } 34 | 35 | //*============================================================================* 36 | // MARK: * NBK x Strict Unsigned Integer x Multiplication x Assertions 37 | //*============================================================================* 38 | 39 | private func NBKAssertMultiplicationByDigitWithAddition( 40 | _ lhs: [UInt], _ rhs: UInt, _ addend: UInt, _ product: [UInt], _ high: UInt, 41 | file: StaticString = #file, line: UInt = #line) { 42 | //=------------------------------------------= 43 | typealias T = NBK.SUISS 44 | //=------------------------------------------= 45 | // multiplication: digit + digit 46 | //=------------------------------------------= 47 | brr: do { 48 | var lhs = lhs 49 | let top = T.multiply(&lhs, by: rhs, add: addend) 50 | XCTAssertEqual(lhs, product, file: file, line: line) 51 | XCTAssertEqual(top, high, file: file, line: line) 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitTests/Private/NBKTupleBinaryInteger+Complements.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | import XCTest 12 | 13 | //*============================================================================* 14 | // MARK: * NBK x Tuple Binary Integer x Complements 15 | //*============================================================================* 16 | 17 | final class NBKTupleBinaryIntegerTestsOnComplements: XCTestCase { 18 | 19 | typealias TS = NBK.TupleBinaryInteger< Int64> 20 | typealias TU = NBK.TupleBinaryInteger 21 | 22 | typealias S1 = TS.Wide1 23 | typealias S2 = TS.Wide2 24 | typealias S3 = TS.Wide3 25 | 26 | typealias U1 = TU.Wide1 27 | typealias U2 = TU.Wide2 28 | typealias U3 = TU.Wide3 29 | 30 | //=------------------------------------------------------------------------= 31 | // MARK: Tests 32 | //=------------------------------------------------------------------------= 33 | 34 | func testMagnitude() { 35 | NBKAssertMagnitude(S2(~1, ~1), U2( 1, 2)) 36 | NBKAssertMagnitude(S2(~0, 0), U2( 1, 0)) 37 | NBKAssertMagnitude(S2( 0, 0), U2( 0, 0)) 38 | NBKAssertMagnitude(S2( 1, 0), U2( 1, 0)) 39 | NBKAssertMagnitude(S2( 1, 2), U2( 1, 2)) 40 | 41 | NBKAssertMagnitude(U2(~1, ~1), U2(~1, ~1)) 42 | NBKAssertMagnitude(U2(~0, 0), U2(~0, 0)) 43 | NBKAssertMagnitude(U2( 0, 0), U2( 0, 0)) 44 | NBKAssertMagnitude(U2( 1, 0), U2( 1, 0)) 45 | NBKAssertMagnitude(U2( 1, 2), U2( 1, 2)) 46 | } 47 | } 48 | 49 | //*============================================================================* 50 | // MARK: * NBK x Tuple Binary Integer x Complements x Assertions 51 | //*============================================================================* 52 | 53 | private func NBKAssertMagnitude( 54 | _ value: NBK.Wide2, _ magnitude: NBK.Wide2, 55 | file: StaticString = #file, line: UInt = #line) { 56 | //=------------------------------------------= 57 | typealias T = NBK.TupleBinaryInteger 58 | //=------------------------------------------= 59 | let result = T.magnitude(of: value) 60 | //=------------------------------------------= 61 | if let value = value as? NBK.Wide2 { 62 | XCTAssert(result == value, file: file, line: line) 63 | } else { 64 | XCTAssert(High.isSigned, file: file, line: line) 65 | } 66 | //=------------------------------------------= 67 | XCTAssert(result == magnitude, file: file, line: line) 68 | XCTAssert(T.Magnitude.magnitude(of: result) == magnitude, file: file, line: line) 69 | } 70 | -------------------------------------------------------------------------------- /Tests/NBKCoreKitTests/Private/NBKTupleBinaryInteger+Multiplication.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | import XCTest 12 | 13 | //*============================================================================* 14 | // MARK: * NBK x Tuple Binary Integer x Multiplication 15 | //*============================================================================* 16 | 17 | final class NBKTupleBinaryIntegerTestsOnMultiplication: XCTestCase { 18 | 19 | typealias TS = NBK.TupleBinaryInteger< Int64> 20 | typealias TU = NBK.TupleBinaryInteger 21 | 22 | typealias S1 = TS.Wide1 23 | typealias S2 = TS.Wide2 24 | typealias S3 = TS.Wide3 25 | 26 | typealias U1 = TU.Wide1 27 | typealias U2 = TU.Wide2 28 | typealias U3 = TU.Wide3 29 | 30 | //=------------------------------------------------------------------------= 31 | // MARK: Tests 32 | //=------------------------------------------------------------------------= 33 | 34 | func testMultiplying213() { 35 | NBKAssertMultiplication213(U2( 1, 2), 3, U3( 0, 3, 6)) 36 | NBKAssertMultiplication213(U2(~1, ~2), ~3, U3(~4, 1, 12)) 37 | NBKAssertMultiplication213(U2(~0, ~0), ~0, U3(~1, ~0, 1)) 38 | } 39 | } 40 | 41 | //*============================================================================* 42 | // MARK: * NBK x Tuple Binary Integer x Multiplication x Assertions 43 | //*============================================================================* 44 | 45 | private func NBKAssertMultiplication213( 46 | _ lhs: NBK.Wide2, _ rhs: High, _ product: NBK.Wide3, 47 | file: StaticString = #file, line: UInt = #line) { 48 | //=------------------------------------------= 49 | typealias T = NBK.TupleBinaryInteger 50 | //=------------------------------------------= 51 | let (high, mid, low) = T.multiplying213(lhs, by: rhs) 52 | XCTAssertEqual(low, product.low, file: file, line: line) 53 | XCTAssertEqual(mid, product.mid, file: file, line: line) 54 | XCTAssertEqual(high, product.high, file: file, line: line) 55 | } 56 | -------------------------------------------------------------------------------- /Tests/NBKDoubleWidthKitBenchmarks/NBKDoubleWidth+Random.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | #if !DEBUG 11 | 12 | import NBKCoreKit 13 | import NBKDoubleWidthKit 14 | import XCTest 15 | 16 | private typealias X64 = NBK.U256X64 17 | private typealias X32 = NBK.U256X32 18 | 19 | //*============================================================================* 20 | // MARK: * NBK x Double Width x Random x Int256 21 | //*============================================================================* 22 | 23 | final class NBKDoubleWidthBenchmarksOnRandomAsInt256: XCTestCase { 24 | 25 | typealias T = Int256 26 | 27 | //=------------------------------------------------------------------------= 28 | // MARK: Tests 29 | //=------------------------------------------------------------------------= 30 | 31 | func testRandomInRangeUsingSystemRandomNumberGenerator() { 32 | var range = NBK.blackHoleIdentity((T.min / 2) ... (T.max / 2)) 33 | var randomness = NBK.blackHoleIdentity(SystemRandomNumberGenerator()) 34 | 35 | for _ in 0 ..< 50_000 { 36 | NBK.blackHole(T.random(in: range, using: &randomness)) 37 | NBK.blackHoleInoutIdentity(&range) 38 | NBK.blackHoleInoutIdentity(&randomness) 39 | } 40 | } 41 | } 42 | 43 | //*============================================================================* 44 | // MARK: * NBK x Double Width x Random x UInt256 45 | //*============================================================================* 46 | 47 | final class NBKDoubleWidthBenchmarksOnRandomAsUInt256: XCTestCase { 48 | 49 | typealias T = UInt256 50 | 51 | //=------------------------------------------------------------------------= 52 | // MARK: Tests 53 | //=------------------------------------------------------------------------= 54 | 55 | func testRandomInRangeUsingSystemRandomNumberGenerator() { 56 | var range = NBK.blackHoleIdentity((T.min / 2) ... (T.max / 2)) 57 | var randomness = NBK.blackHoleIdentity(SystemRandomNumberGenerator()) 58 | 59 | for _ in 0 ..< 50_000 { 60 | NBK.blackHole(T.random(in: range, using: &randomness)) 61 | NBK.blackHoleInoutIdentity(&range) 62 | NBK.blackHoleInoutIdentity(&randomness) 63 | } 64 | } 65 | } 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /Tests/NBKFlexibleWidthKitBenchmarks/Models/NBKFibonacciXL.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | #if !DEBUG 11 | 12 | import NBKCoreKit 13 | import NBKFlexibleWidthKit 14 | import XCTest 15 | 16 | private typealias X = [UInt] 17 | private typealias X64 = [UInt64] 18 | private typealias X32 = [UInt32] 19 | 20 | //*============================================================================* 21 | // MARK: * NBK x FibonacciXL 22 | //*============================================================================* 23 | 24 | final class NBKFibonacciXLBenchmarks: XCTestCase { 25 | 26 | typealias T = NBKFibonacciXL 27 | 28 | //=------------------------------------------------------------------------= 29 | // MARK: Tests 30 | //=------------------------------------------------------------------------= 31 | 32 | func testPrime222() { 33 | var index = NBK.blackHoleIdentity(UInt(1399)) 34 | 35 | for _ in 0 ..< 10_000 { 36 | NBK.blackHole(T(index)) 37 | NBK.blackHoleInoutIdentity(&index) 38 | } 39 | } 40 | 41 | func testPrime333() { 42 | var index = NBK.blackHoleIdentity(UInt(2239)) 43 | 44 | for _ in 0 ..< 10_000 { 45 | NBK.blackHole(T(index)) 46 | NBK.blackHoleInoutIdentity(&index) 47 | } 48 | } 49 | 50 | func testPrime555() { 51 | var index = NBK.blackHoleIdentity(UInt(4019)) 52 | 53 | for _ in 0 ..< 10_000 { 54 | NBK.blackHole(T(index)) 55 | NBK.blackHoleInoutIdentity(&index) 56 | } 57 | } 58 | 59 | func testPrime777() { 60 | var index = NBK.blackHoleIdentity(UInt(5903)) 61 | 62 | for _ in 0 ..< 10_000 { 63 | NBK.blackHole(T(index)) 64 | NBK.blackHoleInoutIdentity(&index) 65 | } 66 | } 67 | 68 | //=------------------------------------------------------------------------= 69 | // MARK: Tests x No Loop 70 | //=------------------------------------------------------------------------= 71 | 72 | /// https://www.wolframalpha.com/input?i2d=true&i=fibonacci+10000000 73 | /// 74 | /// - Note: The 10,000,000th element contains 2,089,877 decimal digits. 75 | /// 76 | func testNoLoop10000000() { 77 | NBK.blackHole(T(NBK.blackHoleIdentity(10_000_000))) 78 | } 79 | } 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /Tests/NBKFlexibleWidthKitBenchmarks/NBKFlexibleWidth+Addition.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | #if !DEBUG 11 | 12 | import NBKCoreKit 13 | import NBKFlexibleWidthKit 14 | import XCTest 15 | 16 | private typealias X = [UInt] 17 | private typealias X64 = [UInt64] 18 | private typealias X32 = [UInt32] 19 | 20 | //*============================================================================* 21 | // MARK: * NBK x Flexible Width x Addition x UIntXL 22 | //*============================================================================* 23 | 24 | final class NBKFlexibleWidthBenchmarksOnAdditionAsUIntXL: XCTestCase { 25 | 26 | typealias T = UIntXL 27 | 28 | //=------------------------------------------------------------------------= 29 | // MARK: Tests 30 | //=------------------------------------------------------------------------= 31 | 32 | func testAdd() { 33 | var lhs = NBK.blackHoleIdentity(T(x64:[~0, ~1, ~2, ~3] as X64)) 34 | var rhs = NBK.blackHoleIdentity(T(x64:[ 0, 1, 2, 3] as X64)) 35 | 36 | for _ in 0 ..< 5_000_000 { 37 | NBK.blackHole(lhs += rhs) 38 | NBK.blackHoleInoutIdentity(&lhs) 39 | NBK.blackHoleInoutIdentity(&rhs) 40 | } 41 | } 42 | 43 | func testAdding() { 44 | var lhs = NBK.blackHoleIdentity(T(x64:[~0, ~1, ~2, ~3] as X64)) 45 | var rhs = NBK.blackHoleIdentity(T(x64:[ 0, 1, 2, 3] as X64)) 46 | 47 | for _ in 0 ..< 1_000_000 { 48 | NBK.blackHole(lhs + rhs) 49 | NBK.blackHoleInoutIdentity(&lhs) 50 | NBK.blackHoleInoutIdentity(&rhs) 51 | } 52 | } 53 | 54 | //=------------------------------------------------------------------------= 55 | // MARK: Tests x Digit 56 | //=------------------------------------------------------------------------= 57 | 58 | func testAddDigit() { 59 | var lhs = NBK.blackHoleIdentity(T(x64:[~0, ~1, ~2, ~3] as X64)) 60 | var rhs = NBK.blackHoleIdentity(UInt.max) 61 | 62 | for _ in 0 ..< 5_000_000 { 63 | NBK.blackHole(lhs += rhs) 64 | NBK.blackHoleInoutIdentity(&lhs) 65 | NBK.blackHoleInoutIdentity(&rhs) 66 | } 67 | } 68 | 69 | func testAddingDigit() { 70 | var lhs = NBK.blackHoleIdentity(T(x64:[~0, ~1, ~2, ~3] as X64)) 71 | var rhs = NBK.blackHoleIdentity(UInt.max) 72 | 73 | for _ in 0 ..< 1_000_000 { 74 | NBK.blackHole(lhs + rhs) 75 | NBK.blackHoleInoutIdentity(&lhs) 76 | NBK.blackHoleInoutIdentity(&rhs) 77 | } 78 | } 79 | } 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /Tests/NBKFlexibleWidthKitBenchmarks/NBKFlexibleWidth+Multiplication.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | #if !DEBUG 11 | 12 | import NBKCoreKit 13 | import NBKFlexibleWidthKit 14 | import XCTest 15 | 16 | private typealias X = [UInt] 17 | private typealias X64 = [UInt64] 18 | private typealias X32 = [UInt32] 19 | 20 | //*============================================================================* 21 | // MARK: * NBK x Flexible Width x Multiplication x UIntXL 22 | //*============================================================================* 23 | 24 | final class NBKFlexibleWidthBenchmarksOnMultiplicationAsUIntXL: XCTestCase { 25 | 26 | typealias T = UIntXL 27 | 28 | //=------------------------------------------------------------------------= 29 | // MARK: Tests 30 | //=------------------------------------------------------------------------= 31 | 32 | func testMultiplying() { 33 | var lhs = NBK.blackHoleIdentity(T(x64:[~1, ~2, ~3, ~4] as X64)) 34 | var rhs = NBK.blackHoleIdentity(T(x64:[ 1, 2, 3, 4] as X64)) 35 | 36 | for _ in 0 ..< 1_000_000 { 37 | NBK.blackHole(lhs * rhs) 38 | NBK.blackHoleInoutIdentity(&lhs) 39 | NBK.blackHoleInoutIdentity(&rhs) 40 | } 41 | } 42 | 43 | //=------------------------------------------------------------------------= 44 | // MARK: Tests x Digit 45 | //=------------------------------------------------------------------------= 46 | 47 | func testMultiplyingByDigit() { 48 | var lhs = NBK.blackHoleIdentity(T(x64:[~1, ~2, ~3, ~4] as X64)) 49 | var rhs = NBK.blackHoleIdentity(UInt.max) 50 | 51 | for _ in 0 ..< 1_000_000 { 52 | NBK.blackHole(lhs * rhs) 53 | NBK.blackHoleInoutIdentity(&lhs) 54 | NBK.blackHoleInoutIdentity(&rhs) 55 | } 56 | } 57 | 58 | //=------------------------------------------------------------------------= 59 | // MARK: Tests x Square 60 | //=------------------------------------------------------------------------= 61 | 62 | func testMultiplyingBySquaring() { 63 | var base = NBK.blackHoleIdentity(T(x64:[~1, ~2, ~3, ~4] as X64)) 64 | 65 | for _ in 0 ..< 1_000_000 { 66 | NBK.blackHole(base.squared()) 67 | NBK.blackHoleInoutIdentity(&base) 68 | NBK.blackHoleInoutIdentity(&base) 69 | } 70 | } 71 | } 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /Tests/NBKFlexibleWidthKitBenchmarks/NBKFlexibleWidth+Subtraction.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | #if !DEBUG 11 | 12 | import NBKCoreKit 13 | import NBKFlexibleWidthKit 14 | import XCTest 15 | 16 | private typealias X = [UInt] 17 | private typealias X64 = [UInt64] 18 | private typealias X32 = [UInt32] 19 | 20 | //*============================================================================* 21 | // MARK: * NBK x Flexible Width x Subtraction x UIntXL 22 | //*============================================================================* 23 | 24 | final class NBKFlexibleWidthBenchmarksOnSubtractionAsUIntXL: XCTestCase { 25 | 26 | typealias T = UIntXL 27 | 28 | //=------------------------------------------------------------------------= 29 | // MARK: Tests 30 | //=------------------------------------------------------------------------= 31 | 32 | func testSubtract() { 33 | var lhs = NBK.blackHoleIdentity(T(x64:[~0, ~1, ~2, ~3] as X64)) 34 | var rhs = NBK.blackHoleIdentity(T(x64:[ 0, 1, 2, 3] as X64)) 35 | 36 | for _ in 0 ..< 5_000_000 { 37 | NBK.blackHole(lhs -= rhs) 38 | NBK.blackHoleInoutIdentity(&lhs) 39 | NBK.blackHoleInoutIdentity(&rhs) 40 | } 41 | } 42 | 43 | func testSubtracting() { 44 | var lhs = NBK.blackHoleIdentity(T(x64:[~0, ~1, ~2, ~3] as X64)) 45 | var rhs = NBK.blackHoleIdentity(T(x64:[ 0, 1, 2, 3] as X64)) 46 | 47 | for _ in 0 ..< 1_000_000 { 48 | NBK.blackHole(lhs - rhs) 49 | NBK.blackHoleInoutIdentity(&lhs) 50 | NBK.blackHoleInoutIdentity(&rhs) 51 | } 52 | } 53 | 54 | //=------------------------------------------------------------------------= 55 | // MARK: Tests x Digit 56 | //=------------------------------------------------------------------------= 57 | 58 | func testSubtractDigit() { 59 | var lhs = NBK.blackHoleIdentity(T(x64:[~0, ~1, ~2, ~3] as X64)) 60 | var rhs = NBK.blackHoleIdentity(UInt.max) 61 | 62 | for _ in 0 ..< 5_000_000 { 63 | NBK.blackHole(lhs -= rhs) 64 | NBK.blackHoleInoutIdentity(&lhs) 65 | NBK.blackHoleInoutIdentity(&rhs) 66 | } 67 | } 68 | 69 | func testSubtractingDigit() { 70 | var lhs = NBK.blackHoleIdentity(T(x64:[~0, ~1, ~2, ~3] as X64)) 71 | var rhs = NBK.blackHoleIdentity(UInt.max) 72 | 73 | for _ in 0 ..< 1_000_000 { 74 | NBK.blackHole(lhs - rhs) 75 | NBK.blackHoleInoutIdentity(&lhs) 76 | NBK.blackHoleInoutIdentity(&rhs) 77 | } 78 | } 79 | } 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /Tests/NBKFlexibleWidthKitBenchmarks/NBKFlexibleWidth+Words.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | #if !DEBUG 11 | 12 | import NBKCoreKit 13 | import NBKFlexibleWidthKit 14 | import XCTest 15 | 16 | private typealias X = [UInt] 17 | private typealias X64 = [UInt64] 18 | private typealias X32 = [UInt32] 19 | 20 | //*============================================================================* 21 | // MARK: * NBK x Flexible Width x Words x UIntXL 22 | //*============================================================================* 23 | 24 | final class NBKFlexibleWidthBenchmarksOnWordsAsUIntXL: XCTestCase { 25 | 26 | typealias T = UIntXL 27 | 28 | //=------------------------------------------------------------------------= 29 | // MARK: Tests 30 | //=------------------------------------------------------------------------= 31 | 32 | func testFromWords() { 33 | var abc = NBK.blackHoleIdentity([ 0, 0, 0, 0] as X) 34 | var xyz = NBK.blackHoleIdentity([~0, ~0, ~0, ~0] as X) 35 | 36 | for _ in 0 ..< 250_000 { 37 | NBK.blackHole(T(words: abc)) 38 | NBK.blackHole(T(words: xyz)) 39 | 40 | NBK.blackHoleInoutIdentity(&abc) 41 | NBK.blackHoleInoutIdentity(&xyz) 42 | } 43 | } 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /Tests/NBKFlexibleWidthKitBenchmarks/NBKFlexibleWidth.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | import NBKFlexibleWidthKit 12 | import XCTest 13 | 14 | private typealias X = [UInt] 15 | private typealias X64 = [UInt64] 16 | private typealias X32 = [UInt32] 17 | 18 | //*============================================================================* 19 | // MARK: * NBK x Flexible Width x UIntXL 20 | //*============================================================================* 21 | 22 | extension NBKFlexibleWidth.Magnitude { 23 | 24 | //=------------------------------------------------------------------------= 25 | // MARK: Initializers 26 | //=------------------------------------------------------------------------= 27 | 28 | static let min256 = Self(x64:[ 0, 0, 0, 0] as X64) 29 | static let max256 = Self(x64:[~0, ~0, ~0, ~0] as X64) 30 | 31 | //=------------------------------------------------------------------------= 32 | // MARK: Initializers 33 | //=------------------------------------------------------------------------= 34 | 35 | init(x32: [UInt32]) { 36 | self.init(words: NBKChunkedInt(x32)) 37 | } 38 | 39 | init(x64: [UInt64]) { 40 | self.init(words: NBKChunkedInt(x64)) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /Tests/NBKFlexibleWidthKitTests/NBKFlexibleWidth+Update.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | import NBKFlexibleWidthKit 12 | import XCTest 13 | 14 | private typealias X = [UInt] 15 | private typealias X64 = [UInt64] 16 | private typealias X32 = [UInt32] 17 | 18 | //*============================================================================* 19 | // MARK: * NBK x Flexible Width x Update x UIntXL 20 | //*============================================================================* 21 | 22 | final class NBKFlexibleWidthTestsOnUpdateAsUIntXL: XCTestCase { 23 | 24 | typealias T = UIntXL 25 | typealias M = UIntXL 26 | 27 | //=------------------------------------------------------------------------= 28 | // MARK: Tests 29 | //=------------------------------------------------------------------------= 30 | 31 | func testUpdate() { 32 | NBKAssertUpdate(T(words:[ 0, 0, 0, 0 ] as X)) 33 | NBKAssertUpdate(T(words:[~0, ~0, ~0, ~0/2 + 0] as X)) 34 | NBKAssertUpdate(T(words:[ 0, 0, 0, ~0/2 + 1] as X)) 35 | NBKAssertUpdate(T(words:[~0, ~0, ~0, ~0 ] as X)) 36 | } 37 | 38 | func testUpdateAsDigit() { 39 | NBKAssertUpdateAsDigit(T.self, UInt.min) 40 | NBKAssertUpdateAsDigit(T.self, UInt.max/2 + 0) 41 | NBKAssertUpdateAsDigit(T.self, UInt.max/2 + 1) 42 | NBKAssertUpdateAsDigit(T.self, UInt.max) 43 | } 44 | } 45 | 46 | //*============================================================================* 47 | // MARK: * NBK x Flexible Width x Update x Assertions 48 | //*============================================================================* 49 | 50 | private func NBKAssertUpdate(_ value: T, file: StaticString = #file, line: UInt = #line) { 51 | XCTAssertEqual({ var x = T(words:[0, 0, 0, 0] as X); x.update(value); return x }(), value, file: file, line: line) 52 | XCTAssertEqual({ var x = T(words:[1, 2, 3, 4] as X); x.update(value); return x }(), value, file: file, line: line) 53 | } 54 | 55 | private func NBKAssertUpdateAsDigit(_ type: T.Type, _ value: T.Digit, file: StaticString = #file, line: UInt = #line) { 56 | NBKAssertUpdate(T(digit: value), file: file, line: line) 57 | XCTAssertEqual({ var x = T(words:[0, 0, 0, 0] as X); x.update(value); return x }(), T(digit: value), file: file, line: line) 58 | XCTAssertEqual({ var x = T(words:[1, 2, 3, 4] as X); x.update(value); return x }(), T(digit: value), file: file, line: line) 59 | } 60 | -------------------------------------------------------------------------------- /Tests/NBKFlexibleWidthKitTests/NBKFlexibleWidth.swift: -------------------------------------------------------------------------------- 1 | //=----------------------------------------------------------------------------= 2 | // This source file is part of the Numberick open source project. 3 | // 4 | // Copyright (c) 2023 Oscar Byström Ericsson 5 | // Licensed under Apache License, Version 2.0 6 | // 7 | // See http://www.apache.org/licenses/LICENSE-2.0 for license information. 8 | //=----------------------------------------------------------------------------= 9 | 10 | import NBKCoreKit 11 | import NBKFlexibleWidthKit 12 | import XCTest 13 | 14 | private typealias X = [UInt] 15 | private typealias X64 = [UInt64] 16 | private typealias X32 = [UInt32] 17 | 18 | //*============================================================================* 19 | // MARK: * NBK x Flexible Width x UIntXL 20 | //*============================================================================* 21 | 22 | extension NBKFlexibleWidth.Magnitude { 23 | 24 | //=------------------------------------------------------------------------= 25 | // MARK: Constants 26 | //=------------------------------------------------------------------------= 27 | 28 | static let min256 = Self(x64:[ 0, 0, 0, 0] as X64) 29 | 30 | static let max256 = Self(x64:[~0, ~0, ~0, ~0] as X64) 31 | 32 | static let basket: [Self] = (-5 ... 5).lazy.map(UInt.init(bitPattern:)).flatMap({[ 33 | Self(words:[$0 ] as X), 34 | Self(words:[$0, $0 &+ 1 ] as X), 35 | Self(words:[$0, $0 &+ 1, $0 &+ 2 ] as X), 36 | Self(words:[$0, $0 &+ 1, $0 &+ 2, $0 &+ 3] as X), 37 | ]}) 38 | 39 | //=------------------------------------------------------------------------= 40 | // MARK: Initializers 41 | //=------------------------------------------------------------------------= 42 | 43 | init(x32: [UInt32]) { 44 | self.init(words: NBKChunkedInt(x32)) 45 | } 46 | 47 | init(x64: [UInt64]) { 48 | self.init(words: NBKChunkedInt(x64)) 49 | } 50 | } 51 | --------------------------------------------------------------------------------