Skip to content

Commit

Permalink
use flisp cprimitives for lowering large longs (#53860)
Browse files Browse the repository at this point in the history
This addresses the previous limitation to `Base.literal_pow`, where it
would only apply to literals between $\pm2\^{61}$ (or 29).
  • Loading branch information
mbauman authored Mar 28, 2024
1 parent 1e50a99 commit b18d2cc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -717,8 +717,20 @@ static int julia_to_scm_noalloc1(fl_context_t *fl_ctx, jl_value_t *v, value_t *r

static value_t julia_to_scm_noalloc2(fl_context_t *fl_ctx, jl_value_t *v, int check_valid) JL_NOTSAFEPOINT
{
if (jl_is_long(v) && fits_fixnum(jl_unbox_long(v)))
return fixnum(jl_unbox_long(v));
if (jl_is_long(v)) {
if (fits_fixnum(jl_unbox_long(v))) {
return fixnum(jl_unbox_long(v));
} else {
#ifdef _P64
value_t prim = cprim(fl_ctx, fl_ctx->int64type, sizeof(int64_t));
*((int64_t*)cp_data((cprim_t*)ptr(prim))) = jl_unbox_long(v);
#else
value_t prim = cprim(fl_ctx, fl_ctx->int32type, sizeof(int32_t));
*((int32_t*)cp_data((cprim_t*)ptr(prim))) = jl_unbox_long(v);
#endif
return prim;
}
}
if (check_valid) {
if (jl_is_ssavalue(v))
lerror(fl_ctx, symbol(fl_ctx, "error"), "SSAValue objects should not occur in an AST");
Expand Down
2 changes: 1 addition & 1 deletion src/flisp/cvalues.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void cv_autorelease(fl_context_t *fl_ctx, cvalue_t *cv)
autorelease(fl_ctx, cv);
}

static value_t cprim(fl_context_t *fl_ctx, fltype_t *type, size_t sz)
value_t cprim(fl_context_t *fl_ctx, fltype_t *type, size_t sz)
{
cprim_t *pcp = (cprim_t*)alloc_words(fl_ctx, CPRIM_NWORDS-1+NWORDS(sz));
pcp->type = type;
Expand Down
1 change: 1 addition & 0 deletions src/flisp/flisp.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ typedef float fl_float_t;
typedef value_t (*builtin_t)(fl_context_t*, value_t*, uint32_t);

value_t cvalue(fl_context_t *fl_ctx, fltype_t *type, size_t sz) JL_NOTSAFEPOINT;
value_t cprim(fl_context_t *fl_ctx, fltype_t *type, size_t sz) JL_NOTSAFEPOINT;
value_t cvalue_no_finalizer(fl_context_t *fl_ctx, fltype_t *type, size_t sz) JL_NOTSAFEPOINT;
void add_finalizer(fl_context_t *fl_ctx, cvalue_t *cv);
void cv_autorelease(fl_context_t *fl_ctx, cvalue_t *cv);
Expand Down
14 changes: 14 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2776,6 +2776,20 @@ Base.literal_pow(::typeof(^), ::PR20530, ::Val{p}) where {p} = 2
@test [2,4,8].^-2 == [0.25, 0.0625, 0.015625]
@test [2, 4, 8].^-2 .* 4 == [1.0, 0.25, 0.0625] # nested literal_pow
@test^-2 == exp(-2) inv(ℯ^2) (ℯ^-1)^2 sqrt(ℯ^-4)

if Int === Int32
p = 2147483647
@test x^p == 1
@test x^2147483647 == 2
@test (@fastmath x^p) == 1
@test (@fastmath x^2147483647) == 2
elseif Int === Int64
p = 9223372036854775807
@test x^p == 1
@test x^9223372036854775807 == 2
@test (@fastmath x^p) == 1
@test (@fastmath x^9223372036854775807) == 2
end
end
module M20889 # do we get the expected behavior without importing Base.^?
using Test
Expand Down

0 comments on commit b18d2cc

Please sign in to comment.