Skip to content

Commit d5ec1c6

Browse files
committed
Convert checked_trunc_[su]int intrinsics to Julia code
1 parent 914c446 commit d5ec1c6

10 files changed

+19
-54
lines changed

base/inference.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,6 @@ add_tfunc(uitofp, 2, 2, bitcast_tfunc, 1)
449449
add_tfunc(sitofp, 2, 2, bitcast_tfunc, 1)
450450
add_tfunc(fptrunc, 2, 2, bitcast_tfunc, 1)
451451
add_tfunc(fpext, 2, 2, bitcast_tfunc, 1)
452-
## checked conversion ##
453-
add_tfunc(checked_trunc_sint, 2, 2, bitcast_tfunc, 3)
454-
add_tfunc(checked_trunc_uint, 2, 2, bitcast_tfunc, 3)
455452
## arithmetic ##
456453
add_tfunc(neg_int, 1, 1, math_tfunc, 1)
457454
add_tfunc(add_int, 2, 2, math_tfunc, 1)
@@ -3745,8 +3742,6 @@ function is_pure_intrinsic(f::IntrinsicFunction)
37453742
return !(f === Intrinsics.pointerref || # this one is volatile
37463743
f === Intrinsics.pointerset || # this one is never effect-free
37473744
f === Intrinsics.llvmcall || # this one is never effect-free
3748-
f === Intrinsics.checked_trunc_sint ||
3749-
f === Intrinsics.checked_trunc_uint ||
37503745
f === Intrinsics.checked_sdiv_int ||
37513746
f === Intrinsics.checked_udiv_int ||
37523747
f === Intrinsics.checked_srem_int ||

base/int.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,22 @@ end
407407

408408
## integer conversions ##
409409

410+
function checked_trunc_sint{To,From}(::Type{To}, x::From)
411+
@_inline_meta
412+
y = trunc_int(To, x)
413+
back = sext_int(From, y)
414+
x == back || throw(InexactError())
415+
y
416+
end
417+
418+
function checked_trunc_uint{To,From}(::Type{To}, x::From)
419+
@_inline_meta
420+
y = trunc_int(To, x)
421+
back = zext_int(From, y)
422+
x == back || throw(InexactError())
423+
y
424+
end
425+
410426
for to in BitInteger_types, from in (BitInteger_types..., Bool)
411427
if !(to === from)
412428
if to.size < from.size

src/codegen.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6231,7 +6231,6 @@ static void init_julia_llvm_env(Module *m)
62316231
global_jlvalue_to_llvm("jl_diverror_exception", &jl_diverror_exception, m);
62326232
global_jlvalue_to_llvm("jl_undefref_exception", &jl_undefref_exception, m);
62336233
global_jlvalue_to_llvm("jl_overflow_exception", &jl_overflow_exception, m);
6234-
global_jlvalue_to_llvm("jl_inexact_exception", &jl_inexact_exception, m);
62356234

62366235
jlRTLD_DEFAULT_var =
62376236
new GlobalVariable(*m, T_pint8,

src/init.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,6 @@ void jl_get_builtin_hooks(void)
786786
jl_stackovf_exception = jl_new_struct_uninit((jl_datatype_t*)core("StackOverflowError"));
787787
jl_diverror_exception = jl_new_struct_uninit((jl_datatype_t*)core("DivideError"));
788788
jl_overflow_exception = jl_new_struct_uninit((jl_datatype_t*)core("OverflowError"));
789-
jl_inexact_exception = jl_new_struct_uninit((jl_datatype_t*)core("InexactError"));
790789
jl_undefref_exception = jl_new_struct_uninit((jl_datatype_t*)core("UndefRefError"));
791790
jl_undefvarerror_type = (jl_datatype_t*)core("UndefVarError");
792791
jl_interrupt_exception = jl_new_struct_uninit((jl_datatype_t*)core("InterruptException"));

src/intrinsics.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -498,24 +498,6 @@ static Value *generic_trunc(jl_codectx_t &ctx, Type *to, Value *x)
498498
return ctx.builder.CreateTrunc(x, to);
499499
}
500500

501-
static Value *generic_trunc_uchecked(jl_codectx_t &ctx, Type *to, Value *x)
502-
{
503-
Value *ans = ctx.builder.CreateTrunc(x, to);
504-
Value *back = ctx.builder.CreateZExt(ans, x->getType());
505-
raise_exception_unless(ctx, ctx.builder.CreateICmpEQ(back, x),
506-
literal_pointer_val(ctx, jl_inexact_exception));
507-
return ans;
508-
}
509-
510-
static Value *generic_trunc_schecked(jl_codectx_t &ctx, Type *to, Value *x)
511-
{
512-
Value *ans = ctx.builder.CreateTrunc(x, to);
513-
Value *back = ctx.builder.CreateSExt(ans, x->getType());
514-
raise_exception_unless(ctx, ctx.builder.CreateICmpEQ(back, x),
515-
literal_pointer_val(ctx, jl_inexact_exception));
516-
return ans;
517-
}
518-
519501
static Value *generic_sext(jl_codectx_t &ctx, Type *to, Value *x)
520502
{
521503
return ctx.builder.CreateSExt(x, to);
@@ -777,10 +759,6 @@ static jl_cgval_t emit_intrinsic(jl_codectx_t &ctx, intrinsic f, jl_value_t **ar
777759
return generic_bitcast(ctx, argv);
778760
case trunc_int:
779761
return generic_cast(ctx, f, generic_trunc, argv, true, true);
780-
case checked_trunc_uint:
781-
return generic_cast(ctx, f, generic_trunc_uchecked, argv, true, true);
782-
case checked_trunc_sint:
783-
return generic_cast(ctx, f, generic_trunc_schecked, argv, true, true);
784762
case sext_int:
785763
return generic_cast(ctx, f, generic_sext, argv, true, true);
786764
case zext_int:

src/intrinsics.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@
6666
ADD_I(sitofp, 2) \
6767
ADD_I(fptrunc, 2) \
6868
ADD_I(fpext, 2) \
69-
/* checked conversion */ \
70-
ADD_I(checked_trunc_sint, 2) \
71-
ADD_I(checked_trunc_uint, 2) \
7269
/* checked arithmetic */ \
7370
ADD_I(checked_sadd_int, 2) \
7471
ADD_I(checked_uadd_int, 2) \

src/jltypes.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ jl_value_t *jl_segv_exception;
121121
JL_DLLEXPORT jl_value_t *jl_diverror_exception;
122122
JL_DLLEXPORT jl_value_t *jl_domain_exception;
123123
JL_DLLEXPORT jl_value_t *jl_overflow_exception;
124-
JL_DLLEXPORT jl_value_t *jl_inexact_exception;
125124
JL_DLLEXPORT jl_value_t *jl_undefref_exception;
126125
jl_value_t *jl_interrupt_exception;
127126
jl_datatype_t *jl_boundserror_type;

src/julia.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,6 @@ extern JL_DLLEXPORT jl_value_t *jl_memory_exception;
530530
extern JL_DLLEXPORT jl_value_t *jl_readonlymemory_exception;
531531
extern JL_DLLEXPORT jl_value_t *jl_diverror_exception;
532532
extern JL_DLLEXPORT jl_value_t *jl_overflow_exception;
533-
extern JL_DLLEXPORT jl_value_t *jl_inexact_exception;
534533
extern JL_DLLEXPORT jl_value_t *jl_undefref_exception;
535534
extern JL_DLLEXPORT jl_value_t *jl_interrupt_exception;
536535
extern JL_DLLEXPORT jl_datatype_t *jl_boundserror_type;

src/julia_internal.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -807,9 +807,6 @@ JL_DLLEXPORT jl_value_t *jl_fptosi(jl_value_t *ty, jl_value_t *a);
807807
JL_DLLEXPORT jl_value_t *jl_fptrunc(jl_value_t *ty, jl_value_t *a);
808808
JL_DLLEXPORT jl_value_t *jl_fpext(jl_value_t *ty, jl_value_t *a);
809809

810-
JL_DLLEXPORT jl_value_t *jl_checked_trunc_sint(jl_value_t *ty, jl_value_t *a);
811-
JL_DLLEXPORT jl_value_t *jl_checked_trunc_uint(jl_value_t *ty, jl_value_t *a);
812-
813810
JL_DLLEXPORT jl_value_t *jl_checked_sadd_int(jl_value_t *a, jl_value_t *b);
814811
JL_DLLEXPORT jl_value_t *jl_checked_uadd_int(jl_value_t *a, jl_value_t *b);
815812
JL_DLLEXPORT jl_value_t *jl_checked_ssub_int(jl_value_t *a, jl_value_t *b);

src/runtime_intrinsics.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -378,15 +378,13 @@ static inline jl_value_t *jl_intrinsiclambda_u1(jl_value_t *ty, void *pa, unsign
378378

379379
typedef void (*intrinsic_cvt_t)(unsigned, void*, unsigned, void*);
380380
typedef unsigned (*intrinsic_cvt_check_t)(unsigned, unsigned, void*);
381-
#define cvt_iintrinsic_checked(LLVMOP, check_op, name) \
381+
#define cvt_iintrinsic(LLVMOP, name) \
382382
JL_DLLEXPORT jl_value_t *jl_##name(jl_value_t *ty, jl_value_t *a) \
383383
{ \
384-
return jl_intrinsic_cvt(ty, a, #name, LLVMOP, check_op); \
384+
return jl_intrinsic_cvt(ty, a, #name, LLVMOP); \
385385
}
386-
#define cvt_iintrinsic(LLVMOP, name) \
387-
cvt_iintrinsic_checked(LLVMOP, NULL, name) \
388386

389-
static inline jl_value_t *jl_intrinsic_cvt(jl_value_t *ty, jl_value_t *a, const char *name, intrinsic_cvt_t op, intrinsic_cvt_check_t check_op)
387+
static inline jl_value_t *jl_intrinsic_cvt(jl_value_t *ty, jl_value_t *a, const char *name, intrinsic_cvt_t op)
390388
{
391389
jl_ptls_t ptls = jl_get_ptls_states();
392390
jl_value_t *aty = jl_typeof(a);
@@ -397,8 +395,6 @@ static inline jl_value_t *jl_intrinsic_cvt(jl_value_t *ty, jl_value_t *a, const
397395
void *pa = jl_data_ptr(a);
398396
unsigned isize = jl_datatype_size(aty);
399397
unsigned osize = jl_datatype_size(ty);
400-
if (check_op && check_op(isize, osize, pa))
401-
jl_throw(jl_inexact_exception);
402398
jl_value_t *newv = jl_gc_alloc(ptls, jl_datatype_size(ty), ty);
403399
op(aty == (jl_value_t*)jl_bool_type ? 1 : isize * host_char_bit, pa,
404400
osize * host_char_bit, jl_data_ptr(newv));
@@ -856,16 +852,6 @@ static inline int all_eq(char *p, char n, char v)
856852
return 0;
857853
return 1;
858854
}
859-
static unsigned check_trunc_sint(unsigned isize, unsigned osize, void *pa)
860-
{
861-
return !all_eq((char*)pa + osize, isize - osize, signbitbyte(pa, isize)); // TODO: assumes little-endian
862-
}
863-
cvt_iintrinsic_checked(LLVMTrunc, check_trunc_sint, checked_trunc_sint)
864-
static unsigned check_trunc_uint(unsigned isize, unsigned osize, void *pa)
865-
{
866-
return !all_eq((char*)pa + osize, isize - osize, 0); // TODO: assumes little-endian
867-
}
868-
cvt_iintrinsic_checked(LLVMTrunc, check_trunc_uint, checked_trunc_uint)
869855

870856
// checked arithmetic
871857
#define check_sadd_int(a,b) \

0 commit comments

Comments
 (0)