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
add support for divMod operation #10886
Comments
Removed Type-Defect label. |
But that would only work for small numbers. |
This comment was originally written by @tatumizer Yes, you are right. I don't know how to do it syntactically, that's the |
This comment was originally written by @tatumizer If dart compiler learns how to inline callbacks, we can do simply this: and we can use same pattern everywhere where we need to return several |
I am not in favor of adding divMod. It should be straightforward and likely more efficient to optimize code so that only one division is issued where possible. |
This comment was originally written by @tatumizer Upon more thinking, I tend to agree. One division and one multiplication is |
This comment was originally written by @tatumizer Please close the issue, it's fixed already for all kinds of ints. (Compiler recognizes the pattern x/y followed by x%y and vice versa and generates only one division) |
This issue was originally filed by @tatumizer
For efficient int->string conversions, we need a fast divMod function returning both quotient and remainder in one step.
Since dart cannot return multiple values, and doesn't have output parameters, it's syntactically challenging.
Compiler, in principle, can be intelligent enough to figure out that
var q=a~/10, r=a%10 can utilize result of division, but in dart, there's a simpler choice.
We can take advantage of Uint32x4 type, and add function divMod like the following:
var xyzw=new Uint32x4(a, 10, 0, 0);
xyzw.divMod(); // in place, returns q in z, r in w
var q=xyzw.z, r=xyzw.w;
To divide iteratively, just set xyzw.x=xyzw.z and invoke divMod again, etc.
The text was updated successfully, but these errors were encountered: