Skip to content

Commit

Permalink
lower x^literal as x^Val{literal} for small integer literals (closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Feb 8, 2017
1 parent 1b7a684 commit 60553fb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ end
^(x::Number, p::Integer) = power_by_squaring(x,p)
^(x, p::Integer) = power_by_squaring(x,p)

# x^p for small literal p is lowered to x^Val{p},
# to enable compile-time optimizations specialized to p.
# However, we still need a fallback that calls the general ^:
^{p}(x, ::Type{Val{p}}) = x^p

# b^p mod m

"""
Expand Down
14 changes: 14 additions & 0 deletions src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,26 @@ value_t fl_julia_scalar(fl_context_t *fl_ctx, value_t *args, uint32_t nargs)
return fl_ctx->F;
}

value_t fl_julia_smallnum(fl_context_t *fl_ctx, value_t *args, uint32_t nargs)
{
argcount(fl_ctx, "julia-smallnum?", nargs, 1);
if (isfixnum(args[0]) && numval(args[0]) < 32 && numval(args[0]) > -32)
return fl_ctx->T;
else if (iscvalue(args[0]) && fl_ctx->jl_sym == cv_type((cvalue_t*)ptr(args[0]))) {
jl_value_t *v = *(jl_value_t**)cptr(args[0]);
if (jl_is_long(v) && jl_unbox_long(v) < 32 && jl_unbox_long(v) > -32)
return fl_ctx->T;
}
return fl_ctx->F;
}

static const builtinspec_t julia_flisp_ast_ext[] = {
{ "defined-julia-global", fl_defined_julia_global },
{ "invoke-julia-macro", fl_invoke_julia_macro },
{ "current-julia-module", fl_current_julia_module },
{ "current-julia-module-counter", fl_current_module_counter },
{ "julia-scalar?", fl_julia_scalar },
{ "julia-smallnum?", fl_julia_smallnum },
{ NULL, NULL }
};

Expand Down
4 changes: 4 additions & 0 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2050,6 +2050,10 @@
(expand-forms
`(call (core _apply) ,f ,@(tuple-wrap argl '())))))

((and (eq? f '^) (length= e 4) (julia-smallnum? (cadddr e)))
(expand-forms
`(call ^ ,(caddr e) (call (core apply_type) (top Val) ,(cadddr e)))))

((and (eq? f '*) (length= e 4))
(expand-transposed-op
e
Expand Down

0 comments on commit 60553fb

Please sign in to comment.