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 Atomic#max and #min for signed enums #13524

Merged
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
21 changes: 21 additions & 0 deletions spec/std/atomic_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ enum AtomicEnum
One
Two
Three
Minus = -1
end

@[Flags]
Expand Down Expand Up @@ -126,6 +127,16 @@ describe Atomic do
atomic.get.should eq(UInt32::MAX)
end

it "#max with signed enum" do
atomic = Atomic.new(AtomicEnum::Two)
atomic.max(AtomicEnum::One).should eq(AtomicEnum::Two)
atomic.get.should eq(AtomicEnum::Two)
atomic.max(AtomicEnum::Three).should eq(AtomicEnum::Two)
atomic.get.should eq(AtomicEnum::Three)
atomic.max(AtomicEnum::Minus).should eq(AtomicEnum::Three)
atomic.get.should eq(AtomicEnum::Three)
end

it "#min with signed" do
atomic = Atomic.new(5)
atomic.min(10).should eq(5)
Expand All @@ -142,6 +153,16 @@ describe Atomic do
atomic.get.should eq(10_u32)
end

it "#min with signed enum" do
atomic = Atomic.new(AtomicEnum::Two)
atomic.min(AtomicEnum::Three).should eq(AtomicEnum::Two)
atomic.get.should eq(AtomicEnum::Two)
atomic.min(AtomicEnum::One).should eq(AtomicEnum::Two)
atomic.get.should eq(AtomicEnum::One)
atomic.min(AtomicEnum::Minus).should eq(AtomicEnum::One)
atomic.get.should eq(AtomicEnum::Minus)
end

it "#set" do
atomic = Atomic.new(1)
atomic.set(2).should eq(2)
Expand Down
16 changes: 14 additions & 2 deletions src/atomic.cr
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,13 @@ struct Atomic(T)
# atomic.get # => 10
# ```
def max(value : T)
{% if T < Int::Signed %}
{% if T < Enum %}
if @value.value.is_a?(Int::Signed)
Ops.atomicrmw(:max, pointerof(@value), value, :sequentially_consistent, false)
else
Ops.atomicrmw(:umax, pointerof(@value), value, :sequentially_consistent, false)
end
{% elsif T < Int::Signed %}
Ops.atomicrmw(:max, pointerof(@value), value, :sequentially_consistent, false)
{% else %}
Ops.atomicrmw(:umax, pointerof(@value), value, :sequentially_consistent, false)
Expand All @@ -144,7 +150,13 @@ struct Atomic(T)
# atomic.get # => 3
# ```
def min(value : T)
{% if T < Int::Signed %}
{% if T < Enum %}
if @value.value.is_a?(Int::Signed)
Ops.atomicrmw(:min, pointerof(@value), value, :sequentially_consistent, false)
else
Ops.atomicrmw(:umin, pointerof(@value), value, :sequentially_consistent, false)
end
{% elsif T < Int::Signed %}
Ops.atomicrmw(:min, pointerof(@value), value, :sequentially_consistent, false)
{% else %}
Ops.atomicrmw(:umin, pointerof(@value), value, :sequentially_consistent, false)
Expand Down