Last active
August 29, 2015 14:09
-
-
Save darkoverlordofdata/c0300b8dcef16db3cafd to your computer and use it in GitHub Desktop.
asm.js with coffee-script wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
asm = require('./bits_asm.js') | |
class Bits | |
SIZE = 1024; | |
bits: null | |
asm: null | |
constructor:(nbits=0) -> | |
@bits = new ArrayBuffer(SIZE) | |
@asm = asm({Uint32Array:Uint32Array, Math:Math}, {}, @bits) | |
get:(index) -> @asm.get(index) | |
set:(index) -> @asm.set(index) | |
clear:(index) -> @asm.clear(index) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(stdlib, env, buffer) { | |
"use asm"; | |
var LENGTH = 0; | |
var BITS = 1; | |
var MEM = new stdlib.Uint32Array(buffer); | |
MEM[LENGTH] = 1; | |
function bits_get(index) { | |
index = index | 0; | |
var word = index >>> 5; | |
if (word >= MEM[LENGTH]) return; | |
return (MEM[BITS+word] & (1 << (index & 0x1f))) != 0; | |
} | |
function bits_set(index) { | |
index = index | 0; | |
var word = index >>> 5; | |
MEM[BITS+word] |= (1 << (index & 0x1f)); | |
} | |
function bits_clear(index) { | |
index = index | 0; | |
var word = index >>> 5; | |
MEM[BITS+word] &= ~(1 << (index & 0x1f)); | |
} | |
return { | |
get : bits_get, | |
set : bits_set, | |
clear : bits_clear | |
}; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe 'Bits tests: ' , -> | |
it "should set and clear bits independantly", -> | |
b1 = new Bits() | |
b2 = new Bits() | |
b1.set(1) | |
b2.set(2) | |
b1.get(1).should.equal true | |
b1.get(2).should.equal false | |
b1.get(3).should.equal false | |
b2.get(1).should.equal false | |
b2.get(2).should.equal true | |
b2.get(3).should.equal false | |
b1.clear(1) | |
b2.clear(2) | |
b1.get(1).should.equal false | |
b2.get(2).should.equal false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment