The default derivation type of trust is I32, but I32 represents a signed number, such as a negative number. If the number is 500, is U32 necessarily faster than I32
let x: u32 = 500
let y: i32 = 500
The default derivation type of trust is I32, but I32 represents a signed number, such as a negative number. If the number is 500, is U32 necessarily faster than I32
let x: u32 = 500
let y: i32 = 500
What operation are you asking about? Just shuffling the bytes around would be the same, since they're both 4 bytes long.
Assuming a large number of operations, is there a speed difference between the two types of calculations
Well, if you are adding or subtracting, both will take the same time (assuming you're not worrying about overflow (even then, with checked arithmetic the difference would be too small to measure))
Most other things should take the same time as well. (except for obvious cases such as checking if the number is less than 0)
What operations, again? Arithmetic will likely be exactly the same, for example (not "the same performance", but "the same processor instructions").
I mean computational performance
There are not "two types of calculations". The same hardware instructions work on signed and unsigned integers. The only difference is how the bits are interpreted (and which results are out of range).
This is true not just of addition/subtraction, but pretty much all ALU instructions, on typical microarchitectures at least. IMUL
and MUL
exist, for instance, but they use the same underlying hardware and have the same performance characteristics when doing analogous things (as far as I know; the specifics may depend on the exact CPU model). In fact, a lot of "unsigned" multiplication will actually use IMUL
because it has additional addressing modes where the high bits don't matter. The labeling of certain opcodes as MUL
or IMUL
reflects usage, not function.
You choose between signed and unsigned based on semantics, not speed. In most cases, the choice will be fairly obvious ("can this be negative?" yes/no).
But that's exactly everyone is asking you to clarify. "computation" and "operation" covers a huge number of possibilities (in fact it covers everything you can do, pretty much). Re-stating the exact same non-informative question in different words won't allow others to offer you useful help.
For assembly, it's all bit patterns. The CPU doesn't know whether integers are signed or not, they are simply bit patterns that can represent integers.
For instance, this Rust code:
pub fn f1(a: i32, b: i32) -> i32 {
a + b
}
pub fn f2(a: u32, b: u32) -> u32 {
a + b
}
will output the following assembly (assuming compiler won't merge identical functions):
f1:
lea eax, [rdi + rsi]
ret
f2:
lea eax, [rdi + rsi]
ret
As it happens: addition, subtraction and multiplication are identical for both signed and unsigned integers.
On the other hand, in general, performing signed division is more expensive than doing unsigned division.
Thank you very much. I see
Thank you
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.