Skip to content

Commit b18d2cc

Browse files
authored
use flisp cprimitives for lowering large longs (#53860)
This addresses the previous limitation to `Base.literal_pow`, where it would only apply to literals between $\pm2\^{61}$ (or 29).
1 parent 1e50a99 commit b18d2cc

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

src/ast.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,8 +717,20 @@ static int julia_to_scm_noalloc1(fl_context_t *fl_ctx, jl_value_t *v, value_t *r
717717

718718
static value_t julia_to_scm_noalloc2(fl_context_t *fl_ctx, jl_value_t *v, int check_valid) JL_NOTSAFEPOINT
719719
{
720-
if (jl_is_long(v) && fits_fixnum(jl_unbox_long(v)))
721-
return fixnum(jl_unbox_long(v));
720+
if (jl_is_long(v)) {
721+
if (fits_fixnum(jl_unbox_long(v))) {
722+
return fixnum(jl_unbox_long(v));
723+
} else {
724+
#ifdef _P64
725+
value_t prim = cprim(fl_ctx, fl_ctx->int64type, sizeof(int64_t));
726+
*((int64_t*)cp_data((cprim_t*)ptr(prim))) = jl_unbox_long(v);
727+
#else
728+
value_t prim = cprim(fl_ctx, fl_ctx->int32type, sizeof(int32_t));
729+
*((int32_t*)cp_data((cprim_t*)ptr(prim))) = jl_unbox_long(v);
730+
#endif
731+
return prim;
732+
}
733+
}
722734
if (check_valid) {
723735
if (jl_is_ssavalue(v))
724736
lerror(fl_ctx, symbol(fl_ctx, "error"), "SSAValue objects should not occur in an AST");

src/flisp/cvalues.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void cv_autorelease(fl_context_t *fl_ctx, cvalue_t *cv)
101101
autorelease(fl_ctx, cv);
102102
}
103103

104-
static value_t cprim(fl_context_t *fl_ctx, fltype_t *type, size_t sz)
104+
value_t cprim(fl_context_t *fl_ctx, fltype_t *type, size_t sz)
105105
{
106106
cprim_t *pcp = (cprim_t*)alloc_words(fl_ctx, CPRIM_NWORDS-1+NWORDS(sz));
107107
pcp->type = type;

src/flisp/flisp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ typedef float fl_float_t;
328328
typedef value_t (*builtin_t)(fl_context_t*, value_t*, uint32_t);
329329

330330
value_t cvalue(fl_context_t *fl_ctx, fltype_t *type, size_t sz) JL_NOTSAFEPOINT;
331+
value_t cprim(fl_context_t *fl_ctx, fltype_t *type, size_t sz) JL_NOTSAFEPOINT;
331332
value_t cvalue_no_finalizer(fl_context_t *fl_ctx, fltype_t *type, size_t sz) JL_NOTSAFEPOINT;
332333
void add_finalizer(fl_context_t *fl_ctx, cvalue_t *cv);
333334
void cv_autorelease(fl_context_t *fl_ctx, cvalue_t *cv);

test/numbers.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2776,6 +2776,20 @@ Base.literal_pow(::typeof(^), ::PR20530, ::Val{p}) where {p} = 2
27762776
@test [2,4,8].^-2 == [0.25, 0.0625, 0.015625]
27772777
@test [2, 4, 8].^-2 .* 4 == [1.0, 0.25, 0.0625] # nested literal_pow
27782778
@test^-2 == exp(-2) inv(ℯ^2) (ℯ^-1)^2 sqrt(ℯ^-4)
2779+
2780+
if Int === Int32
2781+
p = 2147483647
2782+
@test x^p == 1
2783+
@test x^2147483647 == 2
2784+
@test (@fastmath x^p) == 1
2785+
@test (@fastmath x^2147483647) == 2
2786+
elseif Int === Int64
2787+
p = 9223372036854775807
2788+
@test x^p == 1
2789+
@test x^9223372036854775807 == 2
2790+
@test (@fastmath x^p) == 1
2791+
@test (@fastmath x^9223372036854775807) == 2
2792+
end
27792793
end
27802794
module M20889 # do we get the expected behavior without importing Base.^?
27812795
using Test

0 commit comments

Comments
 (0)