Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(381)

Issue 896393004: Add modPow(int exponent, int modulus) method to int abstract class. (Closed)

Created:
5 years, 10 months ago by regis
Modified:
5 years, 10 months ago
CC:
reviews_dartlang.org
Visibility:
Public.

Description

Add modPow(int exponent, int modulus) method to int abstract class. Provide a dart2js implementation. R=floitsch@google.com, lrn@google.com Committed: https://code.google.com/p/dart/source/detail?r=43572

Patch Set 1 #

Total comments: 14

Patch Set 2 : #

Unified diffs Side-by-side diffs Delta from patch set Stats (+48 lines, -12 lines) Patch
M runtime/lib/bigint.dart View 1 1 chunk +6 lines, -3 lines 0 comments Download
M runtime/lib/integers.dart View 1 2 chunks +12 lines, -9 lines 0 comments Download
M sdk/lib/_internal/compiler/js_lib/js_number.dart View 1 1 chunk +22 lines, -0 lines 0 comments Download
M sdk/lib/core/int.dart View 1 1 chunk +8 lines, -0 lines 0 comments Download

Messages

Total messages: 10 (3 generated)
regis
5 years, 10 months ago (2015-02-05 20:14:44 UTC) #2
regis
ping
5 years, 10 months ago (2015-02-06 18:47:36 UTC) #4
Lasse Reichstein Nielsen
lgtm https://codereview.chromium.org/896393004/diff/1/sdk/lib/_internal/compiler/js_lib/js_number.dart File sdk/lib/_internal/compiler/js_lib/js_number.dart (right): https://codereview.chromium.org/896393004/diff/1/sdk/lib/_internal/compiler/js_lib/js_number.dart#newcode372 sdk/lib/_internal/compiler/js_lib/js_number.dart:372: if (m is! int || m <= 0) ...
5 years, 10 months ago (2015-02-06 19:57:04 UTC) #5
Lasse Reichstein Nielsen
but wait for Florian too.
5 years, 10 months ago (2015-02-06 19:57:45 UTC) #6
floitsch
LGTM. https://codereview.chromium.org/896393004/diff/1/sdk/lib/_internal/compiler/js_lib/js_number.dart File sdk/lib/_internal/compiler/js_lib/js_number.dart (right): https://codereview.chromium.org/896393004/diff/1/sdk/lib/_internal/compiler/js_lib/js_number.dart#newcode369 sdk/lib/_internal/compiler/js_lib/js_number.dart:369: // Return pow(this, e) % m. Returns https://codereview.chromium.org/896393004/diff/1/sdk/lib/_internal/compiler/js_lib/js_number.dart#newcode383 ...
5 years, 10 months ago (2015-02-06 20:05:19 UTC) #7
regis
Committed patchset #2 (id:20001) manually as r43572 (presubmit successful).
5 years, 10 months ago (2015-02-06 21:46:38 UTC) #9
regis
5 years, 10 months ago (2015-02-06 21:46:54 UTC) #10
Message was sent while issue was closed.
Thanks!

https://codereview.chromium.org/896393004/diff/1/sdk/lib/_internal/compiler/j...
File sdk/lib/_internal/compiler/js_lib/js_number.dart (right):

https://codereview.chromium.org/896393004/diff/1/sdk/lib/_internal/compiler/j...
sdk/lib/_internal/compiler/js_lib/js_number.dart:369: // Return pow(this, e) %
m.
On 2015/02/06 20:05:19, floitsch wrote:
> Returns

Done.

https://codereview.chromium.org/896393004/diff/1/sdk/lib/_internal/compiler/j...
sdk/lib/_internal/compiler/js_lib/js_number.dart:372: if (m is! int || m <= 0)
throw new ArgumentError(m);
On 2015/02/06 19:57:03, Lasse Reichstein Nielsen wrote:
> Maybe not worth it, but you can use RangeError for the e<0 and m<=0 cases.

Done.

https://codereview.chromium.org/896393004/diff/1/sdk/lib/_internal/compiler/j...
sdk/lib/_internal/compiler/js_lib/js_number.dart:373: if (e < 1) return 1;
On 2015/02/06 19:57:03, Lasse Reichstein Nielsen wrote:
> Why not 
>   if (e == 0) ...

Done.

https://codereview.chromium.org/896393004/diff/1/sdk/lib/_internal/compiler/j...
sdk/lib/_internal/compiler/js_lib/js_number.dart:383: e >>= 1;
On 2015/02/06 19:57:03, Lasse Reichstein Nielsen wrote:
> Using bit-operations restricts e to 2^32. Maybe use  
>   if (e & 1 != 0) {
>     ...
>     e -= 1; 
>   }
>   e ~/ 2;
> ?

I replaced e & 1 != 0 by e.isOdd, since & is a bit operation. I am not sure if
using isOdd makes a difference in dart2js. Decrementing e is actually not
necessary.
I also replaced >>= 1 by ~/= 2.

https://codereview.chromium.org/896393004/diff/1/sdk/lib/_internal/compiler/j...
sdk/lib/_internal/compiler/js_lib/js_number.dart:384: b = (b * b) % m;
On 2015/02/06 19:57:03, Lasse Reichstein Nielsen wrote:
> I guess %m is expensive. Would it be worth it to do:
>   b *= b;
>   if (b > m) b %= m;
> to avoid unnecessary modulos?

The vm is optimizing this case. I do not know if dart2js does it too. I left the
code as is (but fixed the bad indentation).

https://codereview.chromium.org/896393004/diff/1/sdk/lib/core/int.dart
File sdk/lib/core/int.dart (right):

https://codereview.chromium.org/896393004/diff/1/sdk/lib/core/int.dart#newcod...
sdk/lib/core/int.dart:110: * negative or zero.
On 2015/02/06 19:57:04, Lasse Reichstein Nielsen wrote:
> I think we have migrated to writing this as:
> 
>   The [exponent] must be non-negative and [modulus] must be positive.

Done.

Powered by Google App Engine
This is Rietveld 408576698