Permalink
Browse files

trying to fast-path a few common functions

  • Loading branch information...
1 parent 608b38f commit d23ccf19cb36bff5786688680b4a707fc9c1b3f9 Danny Yoo committed Feb 22, 2012
Showing with 31 additions and 2 deletions.
  1. +31 −2 src/js-numbers.js
View
@@ -240,7 +240,21 @@ if (typeof(exports) !== 'undefined') {
// add: scheme-number scheme-number -> scheme-number
- var add = makeNumericBinop(
+ var add = function(x, y) {
+ var sum;
+ if (typeof(x) === 'number' && typeof(y) === 'number') {
+ sum = x + y;
+ if (isOverflow(sum)) {
+ return (makeBignum(x)).add(makeBignum(y));
+ }
+ }
+ if (x instanceof FloatPoint && y instanceof FloatPoint) {
+ return x.add(y);
+ }
+ return addSlow(x, y);
+ };
+
+ var addSlow = makeNumericBinop(
function(x, y) {
var sum = x + y;
if (isOverflow(sum)) {
@@ -284,7 +298,22 @@ if (typeof(exports) !== 'undefined') {
// mulitply: scheme-number scheme-number -> scheme-number
- var multiply = makeNumericBinop(
+ var multiply = function(x, y) {
+ var prod;
+ if (typeof(x) === 'number' && typeof(y) === 'number') {
+ prod = x * y;
+ if (isOverflow(prod)) {
+ return (makeBignum(x)).multiply(makeBignum(y));
+ } else {
+ return prod;
+ }
+ }
+ if (x instanceof FloatPoint && y instanceof FloatPoint) {
+ return x.multiply(y);
+ }
+ return multiplySlow(x, y);
+ };
+ var multiplySlow = makeNumericBinop(
function(x, y) {
var prod = x * y;
if (isOverflow(prod)) {

0 comments on commit d23ccf1

Please sign in to comment.