Bug 129479

Summary: Implement Number.prototype.clz()
Product: WebKit Reporter: Dániel Bátyai <dbatyai.u-szeged>
Component: JavaScriptCoreAssignee: Nobody <webkit-unassigned>
Status: RESOLVED FIXED    
Severity: Normal CC: commit-queue, ggaren, oliver, sam, syoichi
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Proposed patch
none
Proposed patch none

Description Dániel Bátyai 2014-02-28 03:27:44 PST
There is a Number.prototype.clz() specification in the ES6 standard, which is not yet implemented in JSC.
Comment 1 Dániel Bátyai 2014-02-28 04:42:23 PST
Created attachment 225453 [details]
Proposed patch
Comment 2 Anders Carlsson 2014-02-28 06:44:40 PST
Comment on attachment 225453 [details]
Proposed patch

View in context: https://bugs.webkit.org/attachment.cgi?id=225453&action=review

> Source/JavaScriptCore/runtime/NumberPrototype.cpp:472
> +    int zeroCount = 0;
> +    for (int i = 31; i >= 0; i--) {
> +        if (!(number >> i))
> +            zeroCount++;
> +        else
> +            break;
> +    }
> +    return JSValue::encode(jsString(exec, String::numberToStringECMAScript(zeroCount)));

I think this should be a separate function and also use __builtin_clz when building with compilers that support it (GCC, clang).
Comment 3 Geoffrey Garen 2014-02-28 10:45:55 PST
Comment on attachment 225453 [details]
Proposed patch

Is coz really supposed to return a string? That seems impossible, given that its goal is performance.
Comment 4 Geoffrey Garen 2014-02-28 10:46:03 PST
*clz
Comment 5 Sam Weinig 2014-03-01 11:07:04 PST
(In reply to comment #3)
> (From update of attachment 225453 [details])
> Is [clz] really supposed to return a string? That seems impossible, given that its goal is performance.

http://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.prototype.clz

Seems like the answer is it is not supposed to return a string.
Comment 6 Dániel Bátyai 2014-03-03 09:18:58 PST
Created attachment 225661 [details]
Proposed patch

Modified according to comments.

Unfortunately, __builtin_clz(0) was returning 31 instead of the 32 specified in the standard, so I had to add a check for that.
Comment 7 WebKit Commit Bot 2014-03-04 03:21:34 PST
Comment on attachment 225661 [details]
Proposed patch

Clearing flags on attachment: 225661

Committed r165047: <http://trac.webkit.org/changeset/165047>
Comment 8 WebKit Commit Bot 2014-03-04 03:21:36 PST
All reviewed patches have been landed.  Closing bug.