93 lines
2.4 KiB
C
93 lines
2.4 KiB
C
#include <blue/core/bstr.h>
|
|
#include <mie/ctx.h>
|
|
#include <mie/dialect/arith.h>
|
|
#include <mie/dialect/dialect.h>
|
|
#include <mie/macros.h>
|
|
#include <mie/type/type-definition.h>
|
|
#include <mie/type/type.h>
|
|
#include <mie/value.h>
|
|
|
|
struct float_type {
|
|
struct mie_type f_base;
|
|
size_t f_width;
|
|
};
|
|
|
|
static enum mie_status value_print(
|
|
const struct mie_type *ty, const struct mie_value *value, b_stream *out)
|
|
{
|
|
struct float_type *float_ty = (struct float_type *)ty;
|
|
struct mie_float *float_val = (struct mie_float *)value;
|
|
switch (float_ty->f_width) {
|
|
case MIE_FLOAT_32:
|
|
b_stream_write_fmt(
|
|
out, NULL, "%f : f%zu", float_val->f_val.v_32,
|
|
float_ty->f_width);
|
|
break;
|
|
case MIE_FLOAT_64:
|
|
b_stream_write_fmt(
|
|
out, NULL, "%lf : f%zu", float_val->f_val.v_64,
|
|
float_ty->f_width);
|
|
break;
|
|
default:
|
|
b_stream_write_fmt(out, NULL, "NaN : f%zu", float_ty->f_width);
|
|
break;
|
|
}
|
|
|
|
return MIE_SUCCESS;
|
|
}
|
|
|
|
struct mie_type *mie_arith_float_get_type(struct mie_ctx *ctx, size_t bit_width)
|
|
{
|
|
struct mie_type_definition *type_info
|
|
= mie_ctx_get_type_definition(ctx, "arith", "float");
|
|
if (!type_info) {
|
|
return NULL;
|
|
}
|
|
|
|
b_rope rope_i = B_ROPE_CHAR('f'), rope_width = B_ROPE_UINT(bit_width);
|
|
b_rope type_name;
|
|
b_rope_concat(&type_name, &rope_i, &rope_width);
|
|
|
|
mie_id id;
|
|
mie_id_init_ns(&id, mie_id_map_get_ns(&ctx->ctx_types), &type_name);
|
|
|
|
mie_id *target = mie_id_map_get(&ctx->ctx_types, &id);
|
|
if (target) {
|
|
return b_unbox(struct mie_type, target, ty_id);
|
|
}
|
|
|
|
struct float_type *type = (struct float_type *)mie_type_create(type_info);
|
|
if (!type) {
|
|
return NULL;
|
|
}
|
|
|
|
type->f_base.ty_name = b_bstr_fmt("arith.float<%zu>", bit_width);
|
|
type->f_base.ty_instance_size = sizeof(struct mie_float);
|
|
type->f_width = bit_width;
|
|
|
|
mie_id_map_put(&ctx->ctx_types, &type->f_base.ty_id, &type_name);
|
|
return (struct mie_type *)type;
|
|
}
|
|
|
|
static enum mie_status print(
|
|
const struct mie_type_definition *def, const struct mie_type *ty,
|
|
b_stream *out)
|
|
{
|
|
return MIE_SUCCESS;
|
|
}
|
|
|
|
static enum mie_status parse(
|
|
const struct mie_type_definition *def, struct mie_parser *parser,
|
|
struct mie_type **out)
|
|
{
|
|
return MIE_SUCCESS;
|
|
}
|
|
|
|
MIE_TYPE_DEFINITION_BEGIN(mie_arith_float, "float")
|
|
// MIE_TYPE_DEFINITION_FLAGS(MIE_TYPE_DEFINITION_PARAMETISED);
|
|
MIE_TYPE_DEFINITION_STRUCT(struct float_type);
|
|
MIE_TYPE_DEFINITION_PRINT(print);
|
|
MIE_TYPE_DEFINITION_PARSE(parse);
|
|
MIE_TYPE_DEFINITION_VALUE_PRINT(value_print);
|
|
MIE_TYPE_DEFINITION_END()
|