1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

Merge pull request #5134 from yurydelendik/fun4

Improves speed of the functions
This commit is contained in:
Jonas Jenwald 2014-08-05 23:51:03 +02:00
commit cb4a847347
4 changed files with 556 additions and 94 deletions

View file

@ -1,7 +1,8 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* globals expect, it, describe, beforeEach, isArray, StringStream,
PostScriptParser, PostScriptLexer, PostScriptEvaluator */
PostScriptParser, PostScriptLexer, PostScriptEvaluator,
PostScriptCompiler*/
'use strict';
@ -413,5 +414,110 @@ describe('function', function() {
expect(stack).toMatchArray(expectedStack);
});
});
describe('PostScriptCompiler', function() {
function check(code, domain, range, samples) {
var compiler = new PostScriptCompiler();
var compiledCode = compiler.compile(code, domain, range);
if (samples === null) {
expect(compiledCode).toBeNull();
} else {
expect(compiledCode).not.toBeNull();
/*jshint -W054 */
var fn = new Function('src', 'srcOffset', 'dest', 'destOffset',
compiledCode);
for (var i = 0; i < samples.length; i++) {
var out = new Float32Array(samples[i].output.length);
fn(samples[i].input, 0, out, 0);
expect(Array.prototype.slice.call(out, 0)).
toMatchArray(samples[i].output);
}
}
}
it('check compiled add', function() {
check([0.25, 0.5, 'add'], [], [0, 1], [{input: [], output: [0.75]}]);
check([0, 'add'], [0, 1], [0, 1], [{input: [0.25], output: [0.25]}]);
check([0.5, 'add'], [0, 1], [0, 1], [{input: [0.25], output: [0.75]}]);
check([0, 'exch', 'add'], [0, 1], [0, 1],
[{input: [0.25], output: [0.25]}]);
check([0.5, 'exch', 'add'], [0, 1], [0, 1],
[{input: [0.25], output: [0.75]}]);
check(['add'], [0, 1, 0, 1], [0, 1],
[{input: [0.25, 0.5], output: [0.75]}]);
check(['add'], [0, 1], [0, 1], null);
});
it('check compiled sub', function() {
check([0.5, 0.25, 'sub'], [], [0, 1], [{input: [], output: [0.25]}]);
check([0, 'sub'], [0, 1], [0, 1], [{input: [0.25], output: [0.25]}]);
check([0.5, 'sub'], [0, 1], [0, 1], [{input: [0.75], output: [0.25]}]);
check([0, 'exch', 'sub'], [0, 1], [-1, 1],
[{input: [0.25], output: [-0.25]}]);
check([0.75, 'exch', 'sub'], [0, 1], [-1, 1],
[{input: [0.25], output: [0.5]}]);
check(['sub'], [0, 1, 0, 1], [-1, 1],
[{input: [0.25, 0.5], output: [-0.25]}]);
check(['sub'], [0, 1], [0, 1], null);
check([1, 'dup', 3, 2, 'roll', 'sub', 'sub'], [0, 1], [0, 1],
[{input: [0.75], output: [0.75]}]);
});
it('check compiled mul', function() {
check([0.25, 0.5, 'mul'], [], [0, 1], [{input: [], output: [0.125]}]);
check([0, 'mul'], [0, 1], [0, 1], [{input: [0.25], output: [0]}]);
check([0.5, 'mul'], [0, 1], [0, 1], [{input: [0.25], output: [0.125]}]);
check([1, 'mul'], [0, 1], [0, 1], [{input: [0.25], output: [0.25]}]);
check([0, 'exch', 'mul'], [0, 1], [0, 1], [{input: [0.25], output: [0]}]);
check([0.5, 'exch', 'mul'], [0, 1], [0, 1],
[{input: [0.25], output: [0.125]}]);
check([1, 'exch', 'mul'], [0, 1], [0, 1],
[{input: [0.25], output: [0.25]}]);
check(['mul'], [0, 1, 0, 1], [0, 1],
[{input: [0.25, 0.5], output: [0.125]}]);
check(['mul'], [0, 1], [0, 1], null);
});
it('check compiled max', function() {
check(['dup', 0.75, 'gt', 7, 'jz', 'pop', 0.75], [0, 1], [0, 1],
[{input: [0.5], output: [0.5]}]);
check(['dup', 0.75, 'gt', 7, 'jz', 'pop', 0.75], [0, 1], [0, 1],
[{input: [1], output: [0.75]}]);
check(['dup', 0.75, 'gt', 5, 'jz', 'pop', 0.75], [0, 1], [0, 1], null);
});
it('check pop/roll/index', function() {
check([1, 'pop'], [0, 1], [0, 1], [{input: [0.5], output: [0.5]}]);
check([1, 3, -1, 'roll'], [0, 1, 0, 1], [0, 1, 0, 1, 0, 1],
[{input: [0.25, 0.5], output: [0.5, 1, 0.25]}]);
check([1, 3, 1, 'roll'], [0, 1, 0, 1], [0, 1, 0, 1, 0, 1],
[{input: [0.25, 0.5], output: [1, 0.25, 0.5]}]);
check([1, 3, 1.5, 'roll'], [0, 1, 0, 1], [0, 1, 0, 1, 0, 1], null);
check([1, 1, 'index'], [0, 1], [0, 1, 0, 1, 0, 1],
[{input: [0.5], output: [0.5, 1, 0.5]}]);
check([1, 3, 'index', 'pop'], [0, 1], [0, 1], null);
check([1, 0.5, 'index', 'pop'], [0, 1], [0, 1], null);
});
it('check input boundaries', function () {
check([], [0, 0.5], [0, 1], [{input: [1], output: [0.5]}]);
check([], [0.5, 1], [0, 1], [{input: [0], output: [0.5]}]);
check(['dup'], [0.5, 0.75], [0, 1, 0, 1],
[{input: [0], output: [0.5, 0.5]}]);
check([], [100, 1001], [0, 10000], [{input: [1000], output: [1000]}]);
});
it('check output boundaries', function () {
check([], [0, 1], [0, 0.5], [{input: [1], output: [0.5]}]);
check([], [0, 1], [0.5, 1], [{input: [0], output: [0.5]}]);
check(['dup'], [0, 1], [0.5, 1, 0.75, 1],
[{input: [0], output: [0.5, 0.75]}]);
check([], [0, 10000], [100, 1001], [{input: [1000], output: [1000]}]);
});
it('compile optimized', function () {
var compiler = new PostScriptCompiler();
var code = [0, 'add', 1, 1, 3, -1, 'roll', 'sub', 'sub', 1, 'mul'];
var compiledCode = compiler.compile(code, [0, 1], [0, 1]);
expect(compiledCode).toEqual(
'dest[destOffset + 0] = Math.max(0, Math.min(1, src[srcOffset + 0]));');
});
});
});