Skip to content

Commit

Permalink
Add a new helper to convert a long double to an 80-bit RzIL float (#…
Browse files Browse the repository at this point in the history
…4062)

* Add a new helper to convert a `long double` to an 80-bit RzIL float

    * New function: `rz_il_op_new_float_from_f80`
    * New opbuilder: `F80`

* Add test for `rz_il_op_new_float_from_f80`

    * Also add a float format check in `rz_il_sort_pure_eq`
    * Add a few `includes` so that some types are visible
  • Loading branch information
DMaroo authored Jan 3, 2024
1 parent 8fba56b commit 97d3367
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 0 deletions.
18 changes: 18 additions & 0 deletions librz/il/il_opcodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,24 @@ RZ_API RZ_OWN RzILOpFloat *rz_il_op_new_float_from_f64(double f) {
return ret;
}

RZ_API RZ_OWN RzILOpFloat *rz_il_op_new_float_from_f80(long double f) {
RzFloat *value = rz_float_new_from_f80(f);
if (!value) {
return NULL;
}
RzILOpFloat *ret = RZ_NEW0(RzILOpFloat);
if (!ret) {
rz_float_free(value);
return NULL;
}

ret->code = RZ_IL_OP_FLOAT;
ret->op.float_.bv = rz_il_op_new_bitv(value->s);
ret->op.float_.r = value->r;
free(value);
return ret;
}

RZ_API RZ_OWN RzILOpBitVector *rz_il_op_new_fbits(RZ_NONNULL RzILOpFloat *f) {
rz_return_val_if_fail(f, NULL);
RzILOpBitVector *ret;
Expand Down
4 changes: 4 additions & 0 deletions librz/include/rz_il/definitions/sort.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define RZ_IL_SORT_H

#include <rz_types.h>
#include <rz_util/rz_float.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -45,6 +46,9 @@ static inline bool rz_il_sort_pure_eq(RzILSortPure a, RzILSortPure b) {
if (a.type == RZ_IL_TYPE_PURE_BITVECTOR && a.props.bv.length != b.props.bv.length) {
return false;
}
if (a.type == RZ_IL_TYPE_PURE_FLOAT && a.props.f.format != b.props.f.format) {
return false;
}
return true;
}

Expand Down
1 change: 1 addition & 0 deletions librz/include/rz_il/rz_il_opbuilder_begin.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#define BV2F(fmt, bv) rz_il_op_new_float(fmt, bv)
#define F32(f32) rz_il_op_new_float_from_f32(f32)
#define F64(f64) rz_il_op_new_float_from_f64(f64)
#define F80(f80) rz_il_op_new_float_from_f80(f80)
#define F2BV(fl) rz_il_op_new_fbits(fl)
#define IS_FINITE(fl) rz_il_op_new_is_finite(fl)
#define IS_FNAN(fl) rz_il_op_new_is_nan(fl)
Expand Down
1 change: 1 addition & 0 deletions librz/include/rz_il/rz_il_opbuilder_end.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#undef BV2F
#undef F32
#undef F64
#undef F80
#undef F2BV
#undef IS_FINITE
#undef IS_FNAN
Expand Down
1 change: 1 addition & 0 deletions librz/include/rz_il/rz_il_opcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ RZ_API RZ_OWN RzILOpBitVector *rz_il_op_new_loadw(RzILMemIndex mem, RZ_NONNULL R
RZ_API RZ_OWN RzILOpFloat *rz_il_op_new_float(RzFloatFormat format, RZ_NONNULL RzILOpBitVector *bv);
RZ_API RZ_OWN RzILOpFloat *rz_il_op_new_float_from_f32(float f);
RZ_API RZ_OWN RzILOpFloat *rz_il_op_new_float_from_f64(double f);
RZ_API RZ_OWN RzILOpFloat *rz_il_op_new_float_from_f80(long double f);
RZ_API RZ_OWN RzILOpBitVector *rz_il_op_new_fbits(RZ_NONNULL RzILOpFloat *f);
RZ_API RZ_OWN RzILOpBool *rz_il_op_new_is_finite(RZ_NONNULL RzILOpFloat *f);
RZ_API RZ_OWN RzILOpBool *rz_il_op_new_is_nan(RZ_NONNULL RzILOpFloat *f);
Expand Down
1 change: 1 addition & 0 deletions librz/include/rz_util/rz_float.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef RZ_FLOAT_H
#define RZ_FLOAT_H
#include <rz_types.h>
#include <rz_util/rz_bitvector.h>

/**
*
Expand Down
16 changes: 16 additions & 0 deletions test/unit/test_il_validate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,21 @@ static bool test_il_validate_pure_float() {
mu_end;
}

static bool test_il_validate_pure_float80() {
RzILValidateGlobalContext *ctx = rz_il_validate_global_context_new_empty(24);
RzILOpPure *op = rz_il_op_new_float_from_f80(4.2L);
RzILSortPure sort;
RzILValidateReport report;
bool val = rz_il_validate_pure(op, ctx, &sort, &report);
mu_assert_true(val, "valid");
mu_assert_true(rz_il_sort_pure_eq(sort, rz_il_sort_pure_float(RZ_FLOAT_IEEE754_BIN_80)), "sort");
mu_assert_null(report, "no report");
rz_il_op_pure_free(op);

rz_il_validate_global_context_free(ctx);
mu_end;
}

static bool test_il_validate_pure_fbits() {
RzILValidateGlobalContext *ctx = rz_il_validate_global_context_new_empty(24);

Expand Down Expand Up @@ -1745,6 +1760,7 @@ bool all_tests() {
mu_run_test(test_il_validate_effect_repeat);
mu_run_test(test_il_validate_effect_branch);
mu_run_test(test_il_validate_pure_float);
mu_run_test(test_il_validate_pure_float80);
mu_run_test(test_il_validate_pure_fbits);
mu_run_test(test_il_validate_pure_float_bool_uop);
mu_run_test(test_il_validate_pure_float_uop);
Expand Down

0 comments on commit 97d3367

Please sign in to comment.