UInt8ClampedArray Bitwise operators?

Daniel Ehrenberg dehrenberg at chromium.org
Wed Aug 12 00:21:33 UTC 2015


Bits and bytes are fundamental to JavaScript too, which is why the
language has, for a long time, included operations like <<, >>, <<<,
&, |, ^ and ~ on Numbers, exposing the ability to manipulate 32-bit
integers. This is comparable to what C provides. Like C, JavaScript
has a relatively minimal standard library, so collections of these are
not included in the standard library itself.

Uint8ClampedArray is more of a historical artifact than something you
should actually aim to use. Uint8Array is probably what you'd want to
get at as a basis for a user-level library.

On Tue, Aug 11, 2015 at 5:20 PM, Daniel Ehrenberg
<littledan at chromium.org> wrote:
> Bits and bytes are fundamental to JavaScript too, which is why the
> language has, for a long time, included operations like <<, >>, <<<,
> &, |, ^ and ~ on Numbers, exposing the ability to manipulate 32-bit
> integers. This is comparable to what C provides. Like C, JavaScript
> has a relatively minimal standard library, so collections of these are
> not included in the standard library itself.
>
> Uint8ClampedArray is more of a historical artifact than something you
> should actually aim to use. Uint8Array is probably what you'd want to
> get at as a basis for a user-level library.
>
> On Tue, Aug 11, 2015 at 11:55 AM, Michael McGlothlin
> <mike.mcglothlin at gmail.com> wrote:
>> SIMD types appear almost as limited in size as using numbers. Often I need to manipulate thousands or millions of bits efficiently so fumbling around with numbers is messy.
>>
>> I've looked at several implementations on npm but none seemed very mature or standard.
>>
>> I guess it's philosophical as much as anything. In my mind the heart of programming is bits and bytes so I find it odd that most programming languages treat bits and bytes as strange uncles. In my mind all other data types are derived from bits and bytes so it should be possible to easily derive my own types this way or see the bits behind the types that are built-in.
>>
>> In this case I'm implementing a bit index (which is something I use a lot) for quickly finding relationships in complex data. Often I want to mess with bits for working with specifics of file formats and network protocols. When working with large numbers it's nice to not worry about limits. And I work directly with bits when doing electronics.
>>
>> I'm not surprised that it's not supported but it seemed to go with the idea of a byte array so I was hopeful. And it seems more generally useful than things like SIMD types.
>>
>> Thanks,
>> Michael McGlothlin
>> Sent from my iPhone
>>
>>> On Aug 10, 2015, at 7:36 PM, Daniel Ehrenberg <littledan at chromium.org> wrote:
>>>
>>> SIMD types have bitwise operators, but SIMD.js exposes just fixed-size
>>> vectors that are optimized to what hardware can optimize certain
>>> operations for. A future extension (the "long SIMD API") may operate
>>> on whole arrays. I wouldn't recommend using SIMD.js unless you really
>>> feel like you're taking advantage of the better performance, and the
>>> particular vector size works for your requirements.
>>>
>>> The point of SIMD is to expose higher-performance hardware features to
>>> users. You may want to use this for implementing bitwise operations in
>>> user code. However, if you don't need that, it may be enough for you
>>> to use existing operators & | ^ ~ etc, in a loop. A search on npm
>>> yields tons of bitarray libraries which probably do this already,
>>> though I haven't assessed how good they are.
>>>
>>> If you were getting at operator overloading in particular, operators
>>> are already well-defined on things like Uint8Array: Roughly speaking,
>>> they will call .valueOf() and, if that results in a Number they will
>>> do the operation on the underlying Number. There's no built-in valueOf
>>> method for that object, but you can always monkeypatch one in. Here's
>>> an example session in the V8 command-line shell:
>>>
>>> d8> Uint8ClampedArray.prototype.valueOf = function() { return 1 }
>>> function () { return 1 }
>>> d8> new Uint8ClampedArray([1, 2]) << 3
>>> 8
>>>
>>> The downside of doing anything beyond existing npm packages and
>>> changing the language is that it increases complexity. What is the
>>> upside you have in mind to building it into the language?
>>>
>>> Have fun!
>>> Dan
>>>
>>>> On Mon, Aug 10, 2015 at 4:35 PM, Isiah Meadows <isiahmeadows at gmail.com> wrote:
>>>> Do SIMD types solve your problem?
>>>>
>>>> https://github.com/tc39/ecmascript_simd
>>>>
>>>>
>>>> On Mon, Aug 10, 2015, 10:58 Michael McGlothlin <mike.mcglothlin at gmail.com>
>>>> wrote:
>>>>>
>>>>> Would there be a downside to extending Bitwise operators to work with
>>>>> typed arrays such as UInt8ClampedArray? To me it seems natural to want to
>>>>> perform bit operations on these. Or is there a better way to make a BitSet
>>>>> that I'm missing?
>>>>>
>>>>>
>>>>> Thanks,
>>>>> Michael McGlothlin
>>>>> Sent from my iPhone
>>>>> _______________________________________________
>>>>> es-discuss mailing list
>>>>> es-discuss at mozilla.org
>>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>
>>>>
>>>> _______________________________________________
>>>> es-discuss mailing list
>>>> es-discuss at mozilla.org
>>>> https://mail.mozilla.org/listinfo/es-discuss
>>>>


More information about the es-discuss mailing list