Skip to content

Comments

Use portable-atomic on platforms without u64 atomics#23

Open
linkmauve wants to merge 1 commit intonot-fl3:masterfrom
linkmauve:portable-atomic
Open

Use portable-atomic on platforms without u64 atomics#23
linkmauve wants to merge 1 commit intonot-fl3:masterfrom
linkmauve:portable-atomic

Conversation

@linkmauve
Copy link
Contributor

This has been tested on powerpc-unknown-linux-gnu, which supports atomics only up to u32. Without this commit, building quad-rand would fail with:

error[E0432]: unresolved import `core::sync::atomic::AtomicU64`
 --> src/lib.rs:5:26
  |
5 | use core::sync::atomic::{AtomicU64, Ordering};
  |                          ^^^^^^^^^
  |                          |
  |                          no `AtomicU64` in `sync::atomic`
  |                          help: a similar name exists in the module: `AtomicU32`

This has been tested on powerpc-unknown-linux-gnu, which supports
atomics only up to u32.  Without this commit, building quad-rand would
fail with:
error[E0432]: unresolved import `core::sync::atomic::AtomicU64`
 --> src/lib.rs:5:26
  |
5 | use core::sync::atomic::{AtomicU64, Ordering};
  |                          ^^^^^^^^^
  |                          |
  |                          no `AtomicU64` in `sync::atomic`
  |                          help: a similar name exists in the module: `AtomicU32`
rand = { version = "0.8", optional = true }

[target.'cfg(not(target_has_atomic = "64"))'.dependencies]
portable-atomic = "1"
Copy link
Owner

Choose a reason for hiding this comment

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

To be honest, for quad-rand I would rather use two AtomicU32. portable-atomic is a really big crate and we use so little of it. Even with default-features = false cargo will always download all this code, and iirc it even downloads feature-gated crates.

Copy link
Contributor Author

@linkmauve linkmauve Jan 4, 2026

Choose a reason for hiding this comment

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

Do you suggest lowering RandGenerator::state to an AtomicU32 and changing the algorithm, or using two of them and decomposing the operation into non-atomic operations?

I don’t directly use quad-rand, but it is a dependency of kira which I’m using in my game engine, so I don’t have much opinion on that matter.

Edit: My bad, I had misread your message. I’m not sure I know the maths to emulate u64 wrapping add and multiply using u32, and besides that would render the whole operation non-atomic. Maybe using a Mutex<u64> would be nicer?

Copy link
Owner

Choose a reason for hiding this comment

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

I doubt it would be possible to keep it atomic if AtomicU64 is not supported?

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.

2 participants