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

Fix #9389 Uint handling for searchsorted* methods #9936

Merged
merged 1 commit into from
Feb 3, 2015
Merged

Fix #9389 Uint handling for searchsorted* methods #9936

merged 1 commit into from
Feb 3, 2015

Conversation

darwindarak
Copy link
Contributor

searchsortedfirst(a::Range{Integer}, x::Real) is not working as expected when typeof(x) == UInt8. This is caused by floor(Integer, -x), where the negation of the unsigned x does not give us what we need. This PR adds a method for searchsortedfirst just to handle unsigned x.

I suppose you could just do floor(Integer, -1x) or floor(Integer, 0-x), but that feels wrong somehow.

@test searchsorted([1:10], 10, by=(x -> x >= 5)) == 5:10
@test searchsorted([1:5, 1:5, 1:5], 1, 6, 10, Base.Order.Forward) == 6:6
@test searchsorted(ones(15), 1, 6, 10, Base.Order.Forward) == 6:10
for R in [Int, Uint8, Float64], T in [Int, Uint8, Float64]
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Seems like the list of types could be much longer? Why don't test BigInt, BigFloat, UInt64, UInt128....?

@darwindarak
Copy link
Contributor Author

That's true, adding them now.

@darwindarak
Copy link
Contributor Author

Now the tests seem to fail for *Int128:

searchsortedfirst(1:3, UInt128(2))
ERROR: error compiling searchsortedfirst: cannot convert type to a julia type

with the same thing when using Int128.

Running @code_native searchsortedfirst(1:3, UInt128(2)) seems to be giving the right results though.

@ViralBShah
Copy link
Member

Cc: @kmsquire

@kmsquire
Copy link
Member

Seems to be a compiler bug. See #9947.

@ivarne
Copy link
Sponsor Member

ivarne commented Jan 28, 2015

@darwindarak Seems like you can just add the types that work, and leave a comment in #9947 about the 128 bit failures and how the failing tests should be re enabled when that bug is fixed.

@ivarne
Copy link
Sponsor Member

ivarne commented Jan 29, 2015

@darwindarak Github doesn't push out notifications when you push commits, so you should usually add a comments when you push.

Can someone review the correctness of this?

@darwindarak
Copy link
Contributor Author

I just found a problem with my implementation that I'm working on fixing right now. I forgot that the promotion rules for - is based on the size of the data type. The current implementation expects Int32(0) - Uint64(1) to give a signed integer.

@darwindarak darwindarak changed the title Fix #9389 Add searchsortedfirst method to find Uint in ranges Fix #9389 Uint handling for searchsorted* methods Jan 29, 2015
@darwindarak
Copy link
Contributor Author

I think I fixed it. searchsortedlast(1:3, Uint64(2)) was returning an unsigned int as the index. I'm guessing that's not the behavior we want?

@kmsquire
Copy link
Member

It should be converted to an Int.

BTW, the fix for the bug above has been committed, so Int128 and UInt128 should work and can be uncommented.

@darwindarak
Copy link
Contributor Author

@kmsquire Thats great, thanks!

Running the tests with Uint128 and Int128 showed that we're missing a promotion rule between them and Float16. I've added the rule in 523b4dc, but it seems to be out of the scope of this PR. Is it ok to keep it here or should I submit a new PR?

@kmsquire
Copy link
Member

Since it's such a trivial change, I think it's fine to just leave it here.

This looks ready to go! Can you please squash the commits?

@darwindarak
Copy link
Contributor Author

Squashed!

Add more types to `searchsorted` tests

Testing with *Int128 types is triggering an compiler error, so they are
commented out for now.  We can add them back to the once #9947 is fixed

Convert Unsigned to Signed before subtraction

Fix Uint handling for `searchsortedlast`

Re-enable `*Int128` tests for searchsorted`

Previously blocked by #9947

Make `Float16`, `*Int128` promote to `Float32`
@darwindarak
Copy link
Contributor Author

Rebased again to include changes from #10003.

kmsquire added a commit that referenced this pull request Feb 3, 2015
Fix #9389 Uint handling for `searchsorted*` methods
@kmsquire kmsquire merged commit 7f9d6f7 into JuliaLang:master Feb 3, 2015
@timholy timholy mentioned this pull request Feb 3, 2015
@darwindarak darwindarak deleted the dd/searchsortedfirst_uint8 branch February 3, 2015 15:16
@kmsquire
Copy link
Member

kmsquire commented Feb 3, 2015

@JuliaBackports. Depends on #9952.

@kmsquire kmsquire added this to the 0.3.6 milestone Feb 3, 2015
@kmsquire kmsquire removed this from the 0.3.6 milestone Feb 3, 2015
tkelman pushed a commit that referenced this pull request Feb 6, 2015
Add more types to `searchsorted` tests

Testing with *Int128 types is triggering an compiler error, so they are
commented out for now.  We can add them back to the once #9947 is fixed

Convert Unsigned to Signed before subtraction

Fix Uint handling for `searchsortedlast`

Re-enable `*Int128` tests for searchsorted`

Previously blocked by #9947

Make `Float16`, `*Int128` promote to `Float32`

(cherry picked from commit c402273,
syntax adjusted for 0.3)
ref PR #9936
@tkelman
Copy link
Contributor

tkelman commented Feb 6, 2015

backported in 707f94f

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.

5 participants