Skip to content

Instantly share code, notes, and snippets.

@allbinmani
Created April 10, 2017 19:46
Show Gist options
  • Save allbinmani/3e82a14b0f47e6069b25aedf944cdac3 to your computer and use it in GitHub Desktop.
Save allbinmani/3e82a14b0f47e6069b25aedf944cdac3 to your computer and use it in GitHub Desktop.
A simple SQRT table generator
// A simple SQRT table generator
//
// Author: orIgo / iNSANE^C!S 2017
// License: Buy-Me-A-Beer License 1.0
//
// Produces a "stretched" sqrt table
// that fits in the range 0-RANGE
// and outputs an asm include file.
//
// Binary file generation is left
// as an exercise to the user.
//
// Settings
const WIDTH = 256;
const HEIGHT = 256;
const OUTSIZE = "w"; // or "w" or "l" or "n" if you like
const RANGE = 255; // set to 15 for "n"
// Defaults, modify if you only want a subset
const START_ROW = 0;
const END_ROW = HEIGHT;
const START_COL = 0;
const END_COL = WIDTH;
// max is known, calculate it for scaling
const max = Math.floor( Math.sqrt( (END_COL*END_COL)+(END_ROW*END_ROW) ) );
const scale = (RANGE/max);
var rows = [];
for(var y=START_ROW; y < END_ROW; y++)
{
var r = [];
for(var x=START_COL; x < END_COL; x++)
{
r.push(Math.sqrt( (x*x)+(y*y) ));
}
rows.push(r);
}
rows.forEach(
function(r)
{
var suffix = OUTSIZE;
r = r.map(
function(cs) {
return Math.floor(cs * scale);
});
if(OUTSIZE === 'n') { // nibble =)
var r2 = [];
for(var i=0; i < r.length; i+=2) {
var acc = ((r[i] & 15)<<4) | (r[i+1]&15);
r2.push(acc);
}
r = r2;
suffix = "b";
}
console.log("\tdc."+suffix+" " + r.join(","));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment