Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add registry for numerical type size and endianness #17

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 105 additions & 12 deletions src/numeric-types.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "numeric-types.h"

#include <glib.h>
#include <string.h>

#define DEFINE_NUMERIC(type) \
Expand Down Expand Up @@ -54,17 +55,6 @@ numeric_value_set_##type (GValue *val, numeric_##type x) \
g_value_set_boxed (val, ptr); \
}

DEFINE_NUMERIC (int128)
DEFINE_NUMERIC (uint128)
DEFINE_NUMERIC (float80)
DEFINE_NUMERIC (float128)
DEFINE_NUMERIC (decimal32)
DEFINE_NUMERIC (decimal64)
DEFINE_NUMERIC (decimal128)
DEFINE_NUMERIC (complex)
DEFINE_NUMERIC (complex80)
DEFINE_NUMERIC (complex128)

#define DEFINE_NUMERIC_WITH_BYTESWAP(type,gtype,stype,routine,order) \
DEFINE_NUMERIC (type) \
numeric_##type \
Expand Down Expand Up @@ -93,6 +83,102 @@ numeric_##type##_to_##gtype (numeric_##type num) \
return data.v_##gtype; \
}

typedef struct _NumericTypeInfo
{
GType type;
const gchar *name;
gsize width;
NumericByteOrder byte_order;
arteymix marked this conversation as resolved.
Show resolved Hide resolved
} NumericTypeInfo;

GArray *TYPE_TABLE = NULL;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this declaration static.


void
numeric_type_register_static (GType type,
const gchar *name,
gsize width,
NumericByteOrder byte_order)
arteymix marked this conversation as resolved.
Show resolved Hide resolved
{
if (TYPE_TABLE == NULL)
{
TYPE_TABLE = g_array_new (FALSE, FALSE, sizeof (NumericTypeInfo));
}

NumericTypeInfo ti = {
.type = type,
.name = g_strdup (name),
.width = width,
.byte_order = byte_order};

g_array_append_val (TYPE_TABLE,
ti);
}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if we could enumerate these types at runtime so that the we could be forward compatibly if new types are defined in GLib.


NumericTypeInfo*
numeric_type_get_info (GType type)
{
gint i;
for (i = 0; i < TYPE_TABLE->len; i++)
{
NumericTypeInfo* ti = &g_array_index (TYPE_TABLE, NumericTypeInfo, i);
if (ti->type == type)
{
return ti;
}
}

g_return_val_if_reached (NULL);
}

GTypeInfo *type_database;

/**
* numeric_type_get_name:
* Obtain the number of bytes occupied by @numeric_type.
*/
const gchar *
numeric_type_get_name (GType numeric_type)
{
NumericTypeInfo *type_info;
type_info = numeric_type_get_info (numeric_type);
return type_info->name;
}

/**
* numeric_type_get_width:
* Obtain the number of bytes occupied by @numeric_type.
*/
gsize
numeric_type_get_width (GType numeric_type)
{
NumericTypeInfo *type_info;
type_info = numeric_type_get_info (numeric_type);
return type_info->width;
}

/**
* numeric_type_get_byte_order:
* Obtain the byte order of @numeric_type.
*/
NumericByteOrder
numeric_type_get_byte_order (GType numeric_type)
{
NumericTypeInfo *type_info;
type_info = numeric_type_get_info (numeric_type);
return type_info->byte_order;
}

DEFINE_NUMERIC (int128)
DEFINE_NUMERIC (uint128)
DEFINE_NUMERIC (float80)
DEFINE_NUMERIC (float128)
DEFINE_NUMERIC (decimal32)
DEFINE_NUMERIC (decimal64)
DEFINE_NUMERIC (decimal128)
DEFINE_NUMERIC (complex)
DEFINE_NUMERIC (complex80)
DEFINE_NUMERIC (complex128)

DEFINE_NUMERIC_WITH_BYTESWAP (int_le, int, gint, GINT, LE)
DEFINE_NUMERIC_WITH_BYTESWAP (int_be, int, gint, GINT, BE)
DEFINE_NUMERIC_WITH_BYTESWAP (uint_le, uint, guint, GUINT, LE)
Expand All @@ -112,11 +198,18 @@ DEFINE_NUMERIC_WITH_BYTESWAP (double_be, double, gint32, GINT64, BE)

#if HAVE_LIBDFP
extern void register_printf_dfp (void);
#endif

__attribute__ ((constructor))
static void
numeric_types_init (void)
{
numeric_type_register_static (numeric_decimal128_get_type (),
"decimal128",
sizeof (numeric_decimal128),
NUMERIC_BYTE_ORDER_UNKNOWN);

#if HAVE_LIBDFP
arteymix marked this conversation as resolved.
Show resolved Hide resolved
register_printf_dfp ();
}
#endif
}
22 changes: 22 additions & 0 deletions src/numeric-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@

G_BEGIN_DECLS

struct NumericTypeInfo;

/**
* @NUMERIC_BYTE_ORDER_UNKNOWN: If unknown or implementation-defined
*/
typedef enum _NumericByteOrder
{
NUMERIC_BYTE_ORDER_UNKNOWN,
NUMERIC_BYTE_ORDER_LITTLE_ENDIAN,
NUMERIC_BYTE_ORDER_BIG_ENDIAN,
NUMERIC_BYTE_ORDER_PDP_ENDIAN
} NumericByteOrder;

void numeric_type_register_static (GType numeric_type,
const gchar *name,
gsize width,
NumericByteOrder byte_order);

const gchar * numeric_type_get_name (GType numeric_type);
gsize numeric_type_get_width (GType numeric_type);
NumericByteOrder numeric_type_get_byte_order (GType numeric_type);

#define DEFINE_NUMERIC_PROTOTYPE(type,ctype) \
typedef ctype numeric_##type; \
GType numeric_##type##_get_type (void) G_GNUC_CONST; \
Expand Down
5 changes: 5 additions & 0 deletions tests/numeric-c-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <stdio.h>

#include <glib.h>
#include <assert.h>

#if HAVE_LIBDFP
#include <dfp/stdlib.h>
Expand All @@ -33,6 +34,10 @@ test_decimal128 (void)
{
numeric_decimal128 a;

g_assert_cmpstr (numeric_type_get_name (NUMERIC_TYPE_DECIMAL128), ==, "decimal128");
assert (numeric_type_get_width (NUMERIC_TYPE_DECIMAL128) == 16);
assert (numeric_type_get_byte_order (NUMERIC_TYPE_DECIMAL128) == NUMERIC_BYTE_ORDER_UNKNOWN);

#if HAVE_LIBDFP
a = strtod128 ("0.1", NULL);
#else
Expand Down