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

Add checked, wrapping and saturating arithmetic for div/cld/fld #226

Merged
merged 1 commit into from
Sep 11, 2020

Conversation

kimikage
Copy link
Collaborator

@kimikage kimikage commented Sep 3, 2020

The default arithmetic for div is still checked arithmetic.
This changes the error type for overflow from DivideError to OverflowError.

julia> div(-1Q0f7, -eps(Q0f7))
ERROR: DivideError: integer division error # v0.8.4
ERROR: OverflowError: div(-1.0Q0f7, -0.008Q0f7) overflowed for type Int8 # this PR

This also adds the support for cld and 3-arg div.

The default arithmetic for `div` is still checked arithmetic.
This changes the error type for overflow from `DivideError` to `OverflowError`.
This also adds the support for `cld` and 3-arg `div`.
@codecov
Copy link

codecov bot commented Sep 3, 2020

Codecov Report

Merging #226 into master will increase coverage by 0.22%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #226      +/-   ##
==========================================
+ Coverage   95.19%   95.42%   +0.22%     
==========================================
  Files           6        6              
  Lines         645      677      +32     
==========================================
+ Hits          614      646      +32     
  Misses         31       31              
Impacted Files Coverage Δ
src/FixedPointNumbers.jl 96.15% <100.00%> (+0.53%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 595f7a7...558afd6. Read the comment docs.

@kimikage
Copy link
Collaborator Author

kimikage commented Sep 3, 2020

Benchmark

See #222 (comment) for detail of variables.

for op in (div, fld)
    @btime $op.($z_q0f7 , $w_q0f7 );
    @btime $op.($z_q0f15, $w_q0f15);
    @btime $op.($z_q3f4 , $w_q3f4 );
    @btime $op.($z_q3f12, $w_q3f12);

    @btime $op.($z_n0f8 , $w_n0f8 );
    @btime $op.($z_n0f16, $w_n0f16);
    @btime $op.($z_n4f4 , $w_n4f4 );
    @btime $op.($z_n4f12, $w_n4f12);
end
@btime wrapping_div.($x_q0f7 , $y_q0f7 );
@btime wrapping_fld.($x_q0f7 , $y_q0f7 );
@btime wrapping_cld.($x_q0f7 , $y_q0f7 );
@btime wrapping_div.($x_q0f15, $y_q0f15);
@btime wrapping_div.($x_q3f4 , $y_q3f4 );
@btime wrapping_div.($x_q3f12, $y_q3f12);

@btime wrapping_div.($x_n0f8 , $y_n0f8 );
@btime wrapping_fld.($x_n0f8 , $y_n0f8 );
@btime wrapping_cld.($x_n0f8 , $y_n0f8 );
@btime wrapping_div.($x_n0f16, $y_n0f16);
@btime wrapping_div.($x_n4f4 , $y_n4f4 );
@btime wrapping_div.($x_n4f12, $y_n4f12);

@btime saturating_div.($x_q0f7 , $y_q0f7 );
@btime saturating_fld.($x_q0f7 , $y_q0f7 );
@btime saturating_cld.($x_q0f7 , $y_q0f7 );
@btime saturating_div.($x_q0f15, $y_q0f15);
@btime saturating_div.($x_q3f4 , $y_q3f4 );
@btime saturating_div.($x_q3f12, $y_q3f12);

@btime saturating_div.($x_n0f8 , $y_n0f8 );
@btime saturating_fld.($x_n0f8 , $y_n0f8 );
@btime saturating_cld.($x_n0f8 , $y_n0f8 );
@btime saturating_div.($x_n0f16, $y_n0f16);
@btime saturating_div.($x_n4f4 , $y_n4f4 );
@btime saturating_div.($x_n4f12, $y_n4f12);

div (checked_div)

Fixed before after Normed before after
(z_q0f7 , w_q0f7 ) 2.019 ms 1.541 ms (z_n0f8 , w_n0f8 ) 1.793 ms 1.321 ms
(z_q0f15, w_q0f15) 3.085 ms 2.173 ms (z_n0f16, w_n0f16) 2.427 ms 1.932 ms
(z_q3f4 , w_q3f4 ) 2.019 ms 1.544 ms (z_n4f4 , w_n4f4 ) 1.791 ms 1.291 ms
(z_q3f12, w_q3f12) 3.320 ms 2.428 ms (z_n4f12, w_n4f12) 2.423 ms 2.228 ms

fld (checked_fld)

Fixed before after Normed before after
(z_q0f7 , w_q0f7 ) 3.363 ms 1.540 ms (z_n0f8 , w_n0f8 ) 1.793 ms 1.327 ms
(z_q0f15, w_q0f15) 3.989 ms 2.194 ms (z_n0f16, w_n0f16) 2.431 ms 1.967 ms
(z_q3f4 , w_q3f4 ) 3.356 ms 1.544 ms (z_n4f4 , w_n4f4 ) 1.795 ms 1.312 ms
(z_q3f12, w_q3f12) 4.030 ms 2.227 ms (z_n4f12, w_n4f12) 2.434 ms 2.164 ms

wrapping_*

Fixed Normed
wrapping_div. (x_q0f7 , y_q0f7 ) 496.4 μs (x_n0f8 , y_n0f8 ) 323.1 μs
wrapping_fld. (x_q0f7 , y_q0f7 ) 496.1 μs (x_n0f8 , y_n0f8 ) 309.3 μs
wrapping_cld. (x_q0f7 , y_q0f7 ) 496.6 μs (x_n0f8 , y_n0f8 ) 309.0 μs
wrapping_div. (x_q0f15, y_q0f15) 1.054 ms (x_n0f16, y_n0f16) 909.5 μs
wrapping_div. (x_q3f4 , y_q3f4 ) 496.4 μs (x_n4f4 , y_n4f4 ) 309.9 μs
wrapping_div. (x_q3f12, y_q3f12) 1.105 ms (x_n4f12, y_n4f12) 869.3 μs

saturating_*

Fixed Normed
saturating_div. (x_q0f7 , y_q0f7 ) 319.8 μs (x_n0f8 , y_n0f8 ) 489.3 μs
saturating_fld. (x_q0f7 , y_q0f7 ) 319.7 μs (x_n0f8 , y_n0f8 ) 485.4 μs
saturating_cld. (x_q0f7 , y_q0f7 ) 320.2 μs (x_n0f8 , y_n0f8 ) 485.7 μs
saturating_div. (x_q0f15, y_q0f15) 936.1 μs (x_n0f16, y_n0f16) 1.014 ms
saturating_div. (x_q3f4 , y_q3f4 ) 319.8 μs (x_n4f4 , y_n4f4 ) 485.5 μs
saturating_div. (x_q3f12, y_q3f12) 973.9 μs (x_n4f12, y_n4f12) 967.2 μs

@kimikage
Copy link
Collaborator Author

I'm going to move on to the implementation of checked_rem and checked_mod for now.

Probably most people are not interested in the division as I'm not interested in it.:joy:

@kimikage kimikage merged commit c6b8a9c into JuliaMath:master Sep 11, 2020
@kimikage kimikage deleted the checked_div branch September 11, 2020 13:49
@kimikage kimikage mentioned this pull request Apr 30, 2024
38 tasks
kimikage added a commit to kimikage/FixedPointNumbers.jl that referenced this pull request May 1, 2024
…or div/cld/fld (JuliaMath#226)"

The wrapping and saturating `div`/`cld`/`fld` are not backported.

The default arithmetic for `div` is still checked arithmetic.
This changes the error type for overflow from `DivideError` to `OverflowError`.
This also adds the support for `cld` and 3-arg `div`.
kimikage added a commit to kimikage/FixedPointNumbers.jl that referenced this pull request May 1, 2024
…or div/cld/fld (JuliaMath#226)"

The wrapping and saturating `div`/`cld`/`fld` are not backported.

The default arithmetic for `div` is still checked arithmetic.
This changes the error type for overflow from `DivideError` to `OverflowError`.
This also adds the support for `cld` and 3-arg `div`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant