Example #1
0
JL_DLLEXPORT jl_value_t *jl_new_bits(jl_value_t *dt, void *data)
{
    // data may not have the alignment required by the size
    // but will always have the alignment required by the datatype
    jl_ptls_t ptls = jl_get_ptls_states();
    assert(jl_is_datatype(dt));
    jl_datatype_t *bt = (jl_datatype_t*)dt;
    size_t nb = jl_datatype_size(bt);
    // some types have special pools to minimize allocations
    if (nb == 0)               return jl_new_struct_uninit(bt); // returns bt->instance
    if (bt == jl_bool_type)    return (1 & *(int8_t*)data) ? jl_true : jl_false;
    if (bt == jl_uint8_type)   return jl_box_uint8(*(uint8_t*)data);
    if (bt == jl_int64_type)   return jl_box_int64(*(int64_t*)data);
    if (bt == jl_int32_type)   return jl_box_int32(*(int32_t*)data);
    if (bt == jl_int8_type)    return jl_box_int8(*(int8_t*)data);
    if (bt == jl_int16_type)   return jl_box_int16(*(int16_t*)data);
    if (bt == jl_uint64_type)  return jl_box_uint64(*(uint64_t*)data);
    if (bt == jl_uint32_type)  return jl_box_uint32(*(uint32_t*)data);
    if (bt == jl_uint16_type)  return jl_box_uint16(*(uint16_t*)data);
    if (bt == jl_char_type)    return jl_box_char(*(uint32_t*)data);

    jl_value_t *v = jl_gc_alloc(ptls, nb, bt);
    switch (nb) {
    case  1: *(uint8_t*) v = *(uint8_t*)data;    break;
    case  2: *(uint16_t*)v = jl_load_unaligned_i16(data);   break;
    case  4: *(uint32_t*)v = jl_load_unaligned_i32(data);   break;
    case  8: *(uint64_t*)v = jl_load_unaligned_i64(data);   break;
    case 16:
        memcpy(jl_assume_aligned(v, 16), data, 16);
        break;
    default: memcpy(v, data, nb);
    }
    return v;
}
Example #2
0
static jl_value_t *from_td_val(td_val_t *v)
{
    td_tag_t tag = td_typeof(v);
    switch (tag) {
    case TD_INT8:
        return jl_box_int8(td_int8(v));
    case TD_UINT8:
        return jl_box_uint8(td_uint8(v));
    case TD_INT16:
        return jl_box_int16(td_int16(v));
    case TD_UINT16:
        return jl_box_uint16(td_uint16(v));
    case TD_INT32:
        return jl_box_int32(td_int32(v));
    case TD_UINT32:
        return jl_box_uint32(td_uint32(v));
    case TD_INT64:
        return jl_box_int64(td_int64(v));
    case TD_UINT64:
        return jl_box_uint64(td_uint64(v));
    case TD_FLOAT:
        return jl_box_float32(td_float(v));
    case TD_DOUBLE:
        return jl_box_float64(td_double(v));
    case TD_UTF8:
        return jl_pchar_to_string(td_dataptr(v), td_length(v));
    case TD_ARRAY:
        return (jl_value_t*)
               jl_ptr_to_array_1d((jl_value_t*)jl_apply_array_type((jl_datatype_t*)td_type_to_jl(td_eltype(v)), 1),
                                  td_dataptr(v), td_length(v), 0);
    default:
        return jl_nothing;
    }
}
Example #3
0
	inline jl_value_t* box(int16_t val) { return jl_box_int16(val); }