-
-
Notifications
You must be signed in to change notification settings - Fork 670
Why is the supplied code behaving like explaint? #2131
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
Comments
Execution order there is The difference in the second sample results from the compiler inlining |
That's because your code with pc + ((((opcode & 0x1f8) >> 3) - (opcode & 0x200 ? 0x40 : 0)) & 0xFFFF); let's simplify this to pc + (a - b); if It little bit different from C/C++ which always course (ucast) small types to 32-bit. AssemblyScript and Rust always preserve type for all operations. |
If you want to take same effect without change input type you could use this: export function compute(pc: u32, opcode: u16) : u32 {
return pc + (((opcode & 0x1f8) >> 3) as u32 - (opcode & 0x200 ? 0x40 : 0) as u32);
// or
// return pc + (((opcode as u32 & 0x1f8) >> 3) - (opcode as u32 & 0x200 ? 0x40 : 0));
} |
However issue with constant case is real issue. |
Hello @dcodeIO and @MaxGraey
return pc + (((opcode & 0x1f8) >> 3) - (opcode & 0x200 ? 0x40 : 0 as u32)) but not return pc + (((opcode & 0x1f8) >> 3) - (opcode & 0x200 ? 0x40 : 0) as u32) Adding additional braces fixes the above: return pc + (((opcode & 0x1f8) >> 3) - ((opcode & 0x200 ? 0x40 : 0) as u32))
|
Becouse wrap to u16 make sense only for while |
@MaxGraey and what is different between the first statement and the third (added later)? |
However |
The
|
@dcodeIO now I got it. Thanks! Next question: return pc + (((opcode & 0x1f8) >> 3) - (opcode & 0x200 ? 0x40 : 0) as u32) and return pc + (((opcode & 0x1f8) >> 3) - <u32>(opcode & 0x200 ? 0x40 : 0)); ? |
And why behave the third: return pc + (((opcode & 0x1f8) >> 3) - ((opcode & 0x200 ? 0x40 : 0) as u32)) also like the first one? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions! |
Hi,
I have the following function:
The function is called from JS site with the parameters
compute(65, 63457)
.The result of this function call is
65597
. If I change theopcode
tou32
the result is61
, which is the result I expect to get. For me, it looks like some type-overflow. I can also fix it by doing the following things:return pc + (((opcode as u32 & 0x1f8) >> 3) - (opcode & 0x200 ? 0x40 : 0));
return pc + (((opcode & 0x1f8 as u32) >> 3) - (opcode & 0x200 ? 0x40 : 0));
return pc + (((opcode & 0x1f8) >> 3 as u32) - (opcode & 0x200 ? 0x40 : 0));
return pc + (((opcode & 0x1f8) >> 3) as u32 - (opcode & 0x200 ? 0x40 : 0));
Modifying the right statement with
as u32
does nothing.The strange thing is if I do this:
... it also works.
So can someone explain to me what happens here?
The text was updated successfully, but these errors were encountered: