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

i32_popcnt 算術指令BUG #185

Closed
yanjiuntw opened this issue Feb 24, 2019 · 2 comments
Closed

i32_popcnt 算術指令BUG #185

yanjiuntw opened this issue Feb 24, 2019 · 2 comments
Labels

Comments

@yanjiuntw
Copy link
Member

int32_t value = value1->value.i32;
uint8_t count = 0;
while (value) {
if (value & 0x1) {
count++;
}
value = value >> 1;
}

#L10 這裡 value 如果使用 int32_t 型態,會使後面 #L16 運算有問題。

由於這裡 value 型態為有號數,將使用算數位移
算術位移邏輯位移最大的差別在於,算術位移考慮數值的正負號
因此這裡 value 如果為負值,將會進入死迴圈。

@yanjiuntw
Copy link
Member Author

剛剛查了一下C99規格書
6.5.7 Bitwise shift operators 上寫道:
The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type
or if E1 has a signed type and a nonnegative value, the value of the result is the integral
part of the quotient of E1 divided by the quantity, 2 raised to the power E2. If E1 has a
signed type and a negative value, the resulting value is implementation-defined.

會不會是我的編譯器實作不一樣?

@LuisHsu
Copy link
Collaborator

LuisHsu commented Mar 1, 2019

的確這邊需要考慮有號數的問題,所以加個 unsigned 比較妥當

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants