[PATCH v2 1/5] libqmi-glib: add GDOUBLE_FROM_LE and GFLOAT_FROM_LE

Thomas Weißschuh thomas at t-8ch.de
Sun Feb 11 18:18:54 UTC 2018


On Sun, 2018-02-11T15:12+0100, Aleksander Morgado wrote:
> On Fri, Feb 9, 2018 at 10:07 PM, Thomas Weißschuh <thomas at t-8ch.de> wrote:
> > Lifted from Gstreamer gstutils.h v1.12.4.
> > ---
> >  .../libqmi-glib/libqmi-glib-common.sections        |  11 ++
> >  src/libqmi-glib/qmi-utils.h                        | 131 +++++++++++++++++++++
> >  2 files changed, 142 insertions(+)
> >
> > diff --git a/docs/reference/libqmi-glib/libqmi-glib-common.sections b/docs/reference/libqmi-glib/libqmi-glib-common.sections
> > index bc088e7..bae5a5c 100644
> > --- a/docs/reference/libqmi-glib/libqmi-glib-common.sections
> > +++ b/docs/reference/libqmi-glib/libqmi-glib-common.sections
> > @@ -1208,6 +1208,17 @@ qmi_utils_write_gint64_to_buffer
> >  qmi_utils_write_sized_guint_to_buffer
> >  qmi_utils_write_string_to_buffer
> >  qmi_utils_write_fixed_size_string_to_buffer
> > +<SUBSECTION Byteorder>
> > +GFLOAT_FROM_BE
> > +GFLOAT_FROM_LE
> > +GFLOAT_SWAP_LE_BE
> > +GFLOAT_TO_BE
> > +GFLOAT_TO_LE
> > +GDOUBLE_FROM_BE
> > +GDOUBLE_FROM_LE
> > +GDOUBLE_SWAP_LE_BE
> > +GDOUBLE_TO_BE
> > +GDOUBLE_TO_LE
> >  </SECTION>
> >
> 
> Don't know what GStreamer is doing, but we should not introduce public
> symbols for these operations, especially if they start with the G
> prefix.
> It would be ok to have them defined in qmi-utils.h, but inside the #if
> defined (LIBQMI_GLIB_COMPILATION) section and flagged with
> G_GNUC_INTERNAL (and not documented anywhere in the API)

I assumed wrongly that the header was internal.
Will do.

> If they're internal we can keep them named like that, or better, avoid
> future incompatibilities just naming them as "__GDOUBLE_TO..." and
> such. Or even "__QMIDOUBLE_TO...".

> >  <SECTION>
> > diff --git a/src/libqmi-glib/qmi-utils.h b/src/libqmi-glib/qmi-utils.h
> > index 4fd5199..ca5c9fd 100644
> > --- a/src/libqmi-glib/qmi-utils.h
> > +++ b/src/libqmi-glib/qmi-utils.h
> > @@ -20,6 +20,10 @@
> >   *
> >   * Copyright (C) 2012-2015 Dan Williams <dcbw at redhat.com>
> >   * Copyright (C) 2012-2017 Aleksander Morgado <aleksander at aleksander.es>
> > + * Copyright (C) 1999,2000 Erik Walthinsen <omega at cse.ogi.edu>
> > + * Copyright (C) 2000 Wim Taymans <wtay at chello.be>
> > + * Copyright (C) 2002 Thomas Vander Stichele <thomas at apestaart.org>
> > +
> >   */
> >
> >  #ifndef _LIBQMI_GLIB_QMI_UTILS_H_
> > @@ -653,6 +657,133 @@ G_GNUC_INTERNAL
> >  gchar *__qmi_utils_get_driver (const gchar *cdc_wdm_path);
> >  #endif
> >
> > +/* FIXME: Remove this once we depend on a GLib version with this */
> > +#ifndef GFLOAT_FROM_LE
> 
> Don't think this check will work as it's supposed to. If we ever get
> to the point where GLib provides any of these methods, we could check
> for the specific version of GLib here, or have a configure macro
> checking the support for us during configure. In the meantime (I truly
> have no idea how these endianness things behave in LE/BE either), I'd
> suggest to keep them internal *and* renamed to something not starting
> with G.

In terms of forward compatibility I would prefer to check via #ifdef or
renaming them.
I will rename them in the next series.

> > +/**
> > + * GFLOAT_SWAP_LE_BE:
> > + * @in: input value
> > + *
> > + * Swap byte order of a 32-bit floating point value (float).
> > + *
> > + * Returns: @in byte-swapped.
> > + */
> > +static inline gfloat
> > +GFLOAT_SWAP_LE_BE(gfloat in)
> > +{
> > +  union
> > +  {
> > +    guint32 i;
> > +    gfloat f;
> > +  } u;
> > +
> > +  u.f = in;
> > +  u.i = GUINT32_SWAP_LE_BE (u.i);
> > +  return u.f;
> > +}
> > +
> > +/**
> > + * GDOUBLE_SWAP_LE_BE:
> > + * @in: input value
> > + *
> > + * Swap byte order of a 64-bit floating point value (double).
> > + *
> > + * Returns: @in byte-swapped.
> > + */
> > +static inline gdouble
> > +GDOUBLE_SWAP_LE_BE(gdouble in)
> > +{
> > +  union
> > +  {
> > +    guint64 i;
> > +    gdouble d;
> > +  } u;
> > +
> > +  u.d = in;
> > +  u.i = GUINT64_SWAP_LE_BE (u.i);
> > +  return u.d;
> > +}
> > +
> > +/**
> > + * GDOUBLE_TO_LE:
> > + * @val: value
> > + *
> > + * Convert 64-bit floating point value (double) from native byte order into
> > + * little endian byte order.
> > + */
> > +/**
> > + * GDOUBLE_TO_BE:
> > + * @val: value
> > + *
> > + * Convert 64-bit floating point value (double) from native byte order into
> > + * big endian byte order.
> > + */
> > +/**
> > + * GDOUBLE_FROM_LE:
> > + * @val: value
> > + *
> > + * Convert 64-bit floating point value (double) from little endian byte order
> > + * into native byte order.
> > + */
> > +/**
> > + * GDOUBLE_FROM_BE:
> > + * @val: value
> > + *
> > + * Convert 64-bit floating point value (double) from big endian byte order
> > + * into native byte order.
> > + */
> > +
> > +/**
> > + * GFLOAT_TO_LE:
> > + * @val: value
> > + *
> > + * Convert 32-bit floating point value (float) from native byte order into
> > + * little endian byte order.
> > + */
> > +/**
> > + * GFLOAT_TO_BE:
> > + * @val: value
> > + *
> > + * Convert 32-bit floating point value (float) from native byte order into
> > + * big endian byte order.
> > + */
> > +/**
> > + * GFLOAT_FROM_LE:
> > + * @val: value
> > + *
> > + * Convert 32-bit floating point value (float) from little endian byte order
> > + * into native byte order.
> > + */
> > +/**
> > + * GFLOAT_FROM_BE:
> > + * @val: value
> > + *
> > + * Convert 32-bit floating point value (float) from big endian byte order
> > + * into native byte order.
> > + */
> > +
> > +#if G_BYTE_ORDER == G_LITTLE_ENDIAN
> > +#define GFLOAT_TO_LE(val)    ((gfloat) (val))
> > +#define GFLOAT_TO_BE(val)    (GFLOAT_SWAP_LE_BE (val))
> > +#define GDOUBLE_TO_LE(val)   ((gdouble) (val))
> > +#define GDOUBLE_TO_BE(val)   (GDOUBLE_SWAP_LE_BE (val))
> > +
> > +#elif G_BYTE_ORDER == G_BIG_ENDIAN
> > +#define GFLOAT_TO_LE(val)    (GFLOAT_SWAP_LE_BE (val))
> > +#define GFLOAT_TO_BE(val)    ((gfloat) (val))
> > +#define GDOUBLE_TO_LE(val)   (GDOUBLE_SWAP_LE_BE (val))
> > +#define GDOUBLE_TO_BE(val)   ((gdouble) (val))
> > +
> > +#else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
> > +#error unknown ENDIAN type
> > +#endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
> > +
> > +#define GFLOAT_FROM_LE(val)  (GFLOAT_TO_LE (val))
> > +#define GFLOAT_FROM_BE(val)  (GFLOAT_TO_BE (val))
> > +#define GDOUBLE_FROM_LE(val) (GDOUBLE_TO_LE (val))
> > +#define GDOUBLE_FROM_BE(val) (GDOUBLE_TO_BE (val))
> > +
> > +#endif /* !defined(GFLOAT_FROM_LE) */
> > +
> >  G_END_DECLS
> >
> >  #endif /* _LIBQMI_GLIB_QMI_UTILS_H_ */
> > --
> > 2.16.1
> >
> 
> 
> 
> -- 
> Aleksander
> https://aleksander.es


More information about the libqmi-devel mailing list