-
Notifications
You must be signed in to change notification settings - Fork 19
Replace FP division with fixed-point shift in atan2 #110
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,7 +148,8 @@ static twin_angle_t twin_atan2_first_quadrant(twin_fixed_t y, twin_fixed_t x) | |
} | ||
} | ||
|
||
return (twin_angle_t) (double) angle / (32768.0) * TWIN_ANGLE_360; | ||
/* Fixed-point conversion: angle * TWIN_ANGLE_360 / 32768 */ | ||
return (twin_angle_t) ((angle * TWIN_ANGLE_360) >> 15); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since which is equivalent to +/* Fixed-point conversion: angle * TWIN_ANGLE_360 / 32768 */
+ return (twin_angle_t) (angle >> 3); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the value range of (angle * TWIN_ANGLE_360) >> 15 with This value exceeds the limits of 16-bit, it may result in integer overflow and unpredictable behavior. To avoid this issue, the multiplication and division can be replaced with a simple bit shift: angle >> 3 |
||
} | ||
|
||
twin_angle_t twin_atan2(twin_fixed_t y, twin_fixed_t x) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this commit also aim to fix the incorrect casting of the variable
angle
fromtwin_angle_t
todouble
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, intentionally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then, why didn't you write this purpose on this commit log ?