Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the API definitions to a new package, CheckedArithmeticCore #8

Merged
merged 1 commit into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/UnitTest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Unit test

on:
push:
pull_request:
defaults:
run:
shell: bash
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
julia-version: ['1.0', '1', 'nightly']
os: [ubuntu-latest, windows-latest, macOS-latest]
julia-arch: [x64]
include:
- os: ubuntu-latest
julia-version: '1'
julia-arch: x86
steps:
- uses: actions/checkout@v2

- name: "Set up Julia"
uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.julia-version }}
arch: ${{ matrix.julia-arch }}
- run: julia --project -e 'using Pkg; Pkg.develop([PackageSpec(path="CheckedArithmeticCore")])'

- name: Cache artifacts
uses: actions/cache@v1
env:
cache-name: cache-artifacts
with:
path: ~/.julia/artifacts
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
restore-keys: |
${{ runner.os }}-test-${{ env.cache-name }}-
${{ runner.os }}-test-
${{ runner.os }}-

- name: "Unit Test"
uses: julia-actions/julia-runtest@master
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v1
with:
file: lcov.info
15 changes: 0 additions & 15 deletions .travis.yml

This file was deleted.

19 changes: 19 additions & 0 deletions CheckedArithmeticCore/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2019 Tim Holy <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
13 changes: 13 additions & 0 deletions CheckedArithmeticCore/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name = "CheckedArithmeticCore"
uuid = "740b204e-26e5-40b1-866a-9c367e60c4b6"
authors = ["Tim Holy <[email protected]>"]
version = "0.1.0"

[compat]
julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
7 changes: 7 additions & 0 deletions CheckedArithmeticCore/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# CheckedArithmeticCore

CheckedArithmeticCore is a component of
[CheckedArithmetic](https://github.com/JuliaMath/CheckedArithmetic.jl).

This package provides a set of low-level APIs for the packages which provide
numeric types and their arithmetic.
65 changes: 65 additions & 0 deletions CheckedArithmeticCore/src/CheckedArithmeticCore.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module CheckedArithmeticCore

export safearg_type, safearg, safeconvert, accumulatortype, acc

"""
newT = CheckedArithmeticCore.safearg_type(::Type{T})

Return a "reasonably safe" type `newT` for computation with numbers of type `T`.
For example, for `UInt8` one might return `UInt128`, because one is much less likely
to overflow with `UInt128`.
"""
function safearg_type end


"""
xsafe = CheckedArithmeticCore.safearg(x)

Return a variant `xsafe` of `x` that is "reasonably safe" for non-overflowing computation.
For numbers, this uses [`CheckedArithmetic.safearg_type`](@ref).
For containers and other non-number types, specialize `safearg` directly.
"""
safearg(x::T) where T = convert(safearg_type(T), x)


"""
xc = safeconvert(T, x)

Convert `x` to type `T`, "safely." This is designed for comparison to results computed by
[`CheckedArithmetic.@check`](@ref), i.e., for arguments converted by
[`CheckedArithmeticCore.safearg`](@ref).
"""
safeconvert(::Type{T}, x) where T = convert(T, x)


"""
Tnew = accumulatortype(op, T1, T2, ...)
Tnew = accumulatortype(T1, T2, ...)

Return a type `Tnew` suitable for accumulation (reduction) of elements of type `T` under
operation `op`.

# Examples

```jldoctest; setup = :(using CheckedArithmetic)
julia> accumulatortype(+, UInt8)
$UInt
```
"""
Base.@pure accumulatortype(op::Function, T1::Type, T2::Type, T3::Type...) =
accumulatortype(op, promote_type(T1, T2, T3...))
Base.@pure accumulatortype(T1::Type, T2::Type, T3::Type...) =
accumulatortype(*, T1, T2, T3...)
accumulatortype(::Type{T}) where T = accumulatortype(*, T)


"""
xacc = acc(x)

Convert `x` to type [`accumulatortype`](@ref)`(typeof(x))`.
"""
acc(x) = convert(accumulatortype(typeof(x)), x)
acc(f::F, x) where F<:Function = convert(accumulatortype(f, typeof(x)), x)


end # module
30 changes: 30 additions & 0 deletions CheckedArithmeticCore/test/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using CheckedArithmeticCore
using Test

struct MyType end
struct MySafeType end
Base.convert(::Type{MySafeType}, ::MyType) = MySafeType()
CheckedArithmeticCore.safearg_type(::Type{MyType}) = MySafeType
CheckedArithmeticCore.accumulatortype(::typeof(+), ::Type{MyType}) = MyType
CheckedArithmeticCore.accumulatortype(::typeof(+), ::Type{MySafeType}) = MySafeType
CheckedArithmeticCore.accumulatortype(::typeof(*), ::Type{MyType}) = MySafeType
CheckedArithmeticCore.accumulatortype(::typeof(*), ::Type{MySafeType}) = MySafeType
Base.promote_rule(::Type{MyType}, ::Type{MySafeType}) = MySafeType

# fallback
@test safearg(MyType()) === MySafeType()

# fallback
@test safeconvert(UInt16, 0x12) === 0x0012

# fallback
@test accumulatortype(MyType) === MySafeType
@test accumulatortype(MyType, MyType) === MySafeType
@test accumulatortype(+, MyType) === MyType
@test accumulatortype(*, MyType) === MySafeType
@test accumulatortype(+, MyType, MySafeType) === MySafeType
@test accumulatortype(*, MySafeType, MyType) === MySafeType

# acc
@test acc(MyType()) === MySafeType()
@test acc(+, MyType()) === MyType()
12 changes: 10 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
name = "CheckedArithmetic"
uuid = "2c4a1fb8-30c1-4c71-8b84-dff8d59868ee"
authors = ["Tim Holy <[email protected]>"]
version = "0.1.0"
version = "0.2.0"

[deps]
CheckedArithmeticCore = "740b204e-26e5-40b1-866a-9c367e60c4b6"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
CheckedArithmeticCore = "0.1"
julia = "1"

[extras]
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Pkg", "Test"]
Loading