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

Widen small integers only to Int32, not Int #14414

Merged
merged 1 commit into from
Dec 17, 2015

Conversation

eschnett
Copy link
Contributor

Reasons, as described in #14407:

  • 32-bit integer division is significantly faster than 64-bit integer division.
  • Consistency across platforms (widen(Int(8)) will do the same thing on 32 & 64 bit)
  • Smaller integers can have larger gains from vectorization

Changes:

  • Widen small integers only to Int32, not Int
  • Ensure that widen tests actually check the result type, not just the value.
  • Update tests.
  • No documentation update necessary since the actual return type is left unspecified.

Closes #14407.

Reasons, as described in JuliaLang#14407:
- 32-bit integer division is significantly faster than 64-bit integer division.
- Consistency across platforms (widen(Int(8)) will do the same thing on 32 & 64 bit)
- Smaller integers can have larger gains from vectorization

Changes:
- Widen small integers only to Int32, not Int
- Ensure that `widen` tests actually check the result type, not just the value.
- Update tests.
- No documentation update necessary since the actual return type is left unspecified.

Closes JuliaLang#14407.
@ScottPJones
Copy link
Contributor

LGTM 👍

@StefanKarpinski
Copy link
Sponsor Member

Makes sense to me. cc @JeffBezanson

@ViralBShah
Copy link
Member

Yes, this does seem like a good idea.

@tkelman
Copy link
Contributor

tkelman commented Dec 16, 2015

How many places does widen get used? Just trying to get a sense what this might break.

@eschnett
Copy link
Contributor Author

@tkelman I think widen is used in Base (outside of int.jl) mostly for reduction operations, e.g. sum, and for integer factorization.

@vchuravy
Copy link
Member

This might break FixedPointNumbers. I can test this over the weekend.

cc: @timholy

@simonbyrne
Copy link
Contributor

It's used in the rational code, via widemul, but this shouldn't break it. The only constraint that requires is that widen(T) is able to hold the result of *(::T,::T), which is still true.

@@ -339,10 +339,10 @@ typemax(::Type{UInt64}) = 0xffffffffffffffff
@eval typemin(::Type{Int128} ) = $(convert(Int128,1)<<127)
@eval typemax(::Type{Int128} ) = $(box(Int128,unbox(UInt128,typemax(UInt128)>>1)))

widen{T<:Union{Int8, Int16}}(::Type{T}) = Int
widen{T<:Union{Int8, Int16}}(::Type{T}) = Int32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not promote Int8 to Int16?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would have been my first choice as well. However, (a) current behaviour widens to larger integer types, and (b) 16-bit operations are sometimes slower than 32-bit operations (on modern Intel CPUs).

On the other hand, if we go for auto-vectorization, then keeping the type as small as possible would be beneficial.

@JeffBezanson
Copy link
Sponsor Member

Yes, this seems reasonable to me.

@StefanKarpinski
Copy link
Sponsor Member

@eschnett, make a call on whether you think the smaller integer types should widen to Int or something smaller. I'm kind of inclined to go with the smaller type since as you point out it may help with vectorization, and I suspect that in non-vectorized code when the larger type is more efficient, the compiler should trivially be able to use a larger register anyway.

@StefanKarpinski
Copy link
Sponsor Member

Oh, and once you've made a call, feel free to merge.

@eschnett
Copy link
Contributor Author

@StefanKarpinski I think widening to 32 bits instead of Int is a good idea.

eschnett added a commit that referenced this pull request Dec 17, 2015
Widen small integers only to Int32, not Int
@eschnett eschnett merged commit be4cb8a into JuliaLang:master Dec 17, 2015
@eschnett eschnett deleted the eschnett/widen branch December 17, 2015 18:30
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.

8 participants