Skip to content

Commit

Permalink
put memory reinterpretation into mli_math, improve error msg.
Browse files Browse the repository at this point in the history
  • Loading branch information
relleums committed Jun 4, 2024
1 parent 92477d1 commit af6d514
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
14 changes: 14 additions & 0 deletions libs/mli/src/mli_math.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,17 @@ double mli_relative_ratio(const double a, const double b)
{
return fabs(a - b) / (0.5 * (a + b));
}

double mli_interpret_int64_as_double(int64_t i)
{
double f;
memcpy(&f, &i, sizeof(double));
return f;
}

int64_t mli_interpret_double_as_int64(double d)
{
int64_t i;
memcpy(&i, &d, sizeof(int64_t));
return i;
}
3 changes: 3 additions & 0 deletions libs/mli/src/mli_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,7 @@ double mli_linear_interpolate_2d(
const double x1,
const double y1);
double mli_relative_ratio(const double a, const double b);

double mli_interpret_int64_as_double(int64_t i);
int64_t mli_interpret_double_as_int64(double d);
#endif
2 changes: 1 addition & 1 deletion libs/mli/src/mli_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#define MLI_VERSION_MAYOR 1
#define MLI_VERSION_MINOR 9
#define MLI_VERSION_PATCH 5
#define MLI_VERSION_PATCH 6

void mli_logo_fprint(FILE *f);
void mli_authors_and_affiliations_fprint(FILE *f);
Expand Down
39 changes: 15 additions & 24 deletions libs/mli_corsika/src/mli_corsika_Histogram2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "mli_corsika_Histogram2d.h"
#include <string.h>
#include "../../mli/src/chk.h"
#include "../../mli/src/mli_math.h"

struct key {
int32_t x;
Expand All @@ -21,20 +22,6 @@ struct mliCorsikaHistogram2d mliCorsikaHistogram2d_init(void)
return hist;
}

double interpret_int64_as_double(int64_t i)
{
double f;
memcpy(&f, &i, sizeof(double));
return f;
}

int64_t interpret_double_as_int64(double d)
{
int64_t i;
memcpy(&i, &d, sizeof(int64_t));
return i;
}

void mliCorsikaHistogram2d_free(struct mliCorsikaHistogram2d *hist)
{
mliAvlDict_free(&hist->dict);
Expand All @@ -61,12 +48,12 @@ int mliCorsikaHistogram2d_assign(

has = mliAvlDict_get(&hist->dict, key.i8, &ival);
if (has) {
double dval = interpret_int64_as_double(ival);
double dval = mli_interpret_int64_as_double(ival);
dval += weight;
ival = interpret_double_as_int64(dval);
ival = mli_interpret_double_as_int64(dval);

} else {
ival = interpret_double_as_int64(weight);
ival = mli_interpret_double_as_int64(weight);
}
return mliAvlDict_set(&hist->dict, key.i8, ival);
}
Expand All @@ -84,7 +71,7 @@ int mliCorsikaHistogram2d_dumps__(
}

key.i8 = node->key;
dval = interpret_int64_as_double(node->value);
dval = mli_interpret_int64_as_double(node->value);

count = mliIo_write(
f, (const void *)(&key.i4i4.x), sizeof(uint32_t), 1);
Expand All @@ -99,12 +86,12 @@ int mliCorsikaHistogram2d_dumps__(

if (node->avl.left != NULL) {
struct mliAvlNode *left = (struct mliAvlNode *)(node->avl.left);
chk_msg(mliCorsikaHistogram2d_dumps__(left, f), "1");
chk_msg(mliCorsikaHistogram2d_dumps__(left, f), "Failed left");
}
if (node->avl.right != NULL) {
struct mliAvlNode *right =
(struct mliAvlNode *)(node->avl.right);
chk_msg(mliCorsikaHistogram2d_dumps__(right, f), "2");
chk_msg(mliCorsikaHistogram2d_dumps__(right, f), "Failed right");
}

return 1;
Expand All @@ -122,11 +109,15 @@ int mliCorsikaHistogram2d_dumps(
f, (const void *)(&hist->dict.len), sizeof(uint64_t), 1);
chk_msg(count == 1, "Failed to write dict->len.");

chk_msg(mliCorsikaHistogram2d_dumps__(
(const struct mliAvlNode *)hist->dict.tree.root, f),
"woot?")
chk_msg(
mliCorsikaHistogram2d_dumps__(
(const struct mliAvlNode *)hist->dict.tree.root,
f
),
"Failed to write dict."
);

return 1;
return 1;
chk_error:
return 0;
}
Expand Down

0 comments on commit af6d514

Please sign in to comment.