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

add support for divMod operation #10886

Closed
DartBot opened this issue May 26, 2013 · 7 comments
Closed

add support for divMod operation #10886

DartBot opened this issue May 26, 2013 · 7 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-obsolete Closed as the reported issue is no longer relevant P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented May 26, 2013

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.

@sethladd
Copy link
Contributor

Removed Type-Defect label.
Added Type-Enhancement, Area-Library, Triaged labels.

@floitschG
Copy link
Contributor

But that would only work for small numbers.
divmod is something that is pretty useful for bignums too. It would be a shame if uint32s and bignums didn't share the same interface.

@DartBot
Copy link
Author

DartBot commented May 28, 2013

This comment was originally written by @tatumizer


Yes, you are right. I don't know how to do it syntactically, that's the
problem. Maybe introduce class DivMod, but in such manner that the instance
we create once, can be reused for iterative divisions. I don't know the
best format of it, can't find any pattern that does something similar.

@DartBot
Copy link
Author

DartBot commented May 28, 2013

This comment was originally written by @tatumizer


If dart compiler learns how to inline callbacks, we can do simply this:
divmod(a, 10, (q,r) { /*do something with q, r *})

and we can use same pattern everywhere where we need to return several
values.
But it should be inlined. Maybe this is ok?

@ghost
Copy link

ghost commented May 29, 2013

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.

@DartBot
Copy link
Author

DartBot commented May 29, 2013

This comment was originally written by @tatumizer


Upon more thinking, I tend to agree. One division and one multiplication is
needed to find both q and r. this multiplication is eliminated in divMod,
but there will be certain overhead in divMod anyay.
Multiplication is relatively cheap operation these days, so we won't get
too much benefit.

@DartBot
Copy link
Author

DartBot commented Apr 9, 2015

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)

@DartBot DartBot added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Apr 9, 2015
@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed triaged labels Feb 29, 2016
@lrhn lrhn added the closed-obsolete Closed as the reported issue is no longer relevant label Sep 12, 2016
@lrhn lrhn closed this as completed Sep 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-obsolete Closed as the reported issue is no longer relevant P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants