<div dir="ltr">Christof,<div>Yes, you are right. For this example keeping them as local variables will be better. This code is just a scratch and I actually didn't think about the life cycle of those variables. My first plan was to write an xsenstoOSC converter. But when my skills are considered, I will need to read a lot of documentation for this and I don't have time. I just have to save the day.</div><div>Thanks.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Christof Ressi <<a href="mailto:christof.ressi@gmx.at">christof.ressi@gmx.at</a>>, 6 Nis 2019 Cmt, 19:16 tarihinde şunu yazdı:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">> Using a union would be a bit more efficient as it doesn't copy any memory.<br>
<br>
an optimizing compiler will very likely generate identical assembly. look here, how the call to memcpy is reduced to a single 'movss' instruction: <a href="https://godbolt.org/z/n5ueWD" rel="noreferrer" target="_blank">https://godbolt.org/z/n5ueWD</a><br>
<br>
@Arda: shouldn't 'wrd' and 'o' be local variables instead of struct members? or do you need somehow to cache the value for later?<br>
<br>
Christof<br>
<br>
<br>
Gesendet: Samstag, 06. April 2019 um 17:27 Uhr<br>
Von: "Martin Peach" <<a href="mailto:chakekatzil@gmail.com" target="_blank">chakekatzil@gmail.com</a>><br>
An: Pd-List <<a href="mailto:pd-list@lists.iem.at" target="_blank">pd-list@lists.iem.at</a>><br>
Betreff: Re: [PD] Getting 32 bit floating point number from 4 sequential bytes<br>
<br>
On Sat, Apr 6, 2019 at 10:54 AM Arda Eden <<a href="mailto:ardaeden@gmail.com" target="_blank">ardaeden@gmail.com</a>[mailto:<a href="mailto:ardaeden@gmail.com" target="_blank">ardaeden@gmail.com</a>]> wrote:<br>
<br>
With this method, I got the compiler warning about breaking the strict aliasing rule:<br>
uint8_t bytes[4] = {12, 34, 56, 78};<br>
float f = *(float*)bytes;<br>
 <br>
Yes, modern c doesn't like type punning.<br>
 <br>
<br>
 <br>
 <br>
This is my code, and for now, it is working properly. But I am not sure if this is an efficient way or not.<br>
 <br>
typedef struct _xsensparse<br>
{<br>
    t_object x_obj;<br>
    uint8_t wrd[4];<br>
    t_float o;<br>
    t_outlet *f1_out, *f2_out;<br>
} t_xsensparse;<br>
 <br>
static void xsensparse_list(t_xsensparse *x, t_symbol *s, int argc, t_atom *argv)<br>
{<br>
    for(int i=0; i<argc; i++) {<br>
    x->wrd[i]=(uint8_t)atom_getfloat(argv+3-i);<br>
    memcpy(&x->o, &x->wrd, 4);<br>
    }<br>
    post("%f",  x->o);<br>
    outlet_float(x->f1_out, x->o);<br>
}<br>
 <br>
I think the memcpy statement should be outside the for loop. As it is, it operates the first three times uselessly.<br>
Using a union would be a bit more efficient as it doesn't copy any memory.<br>
 <br>
Martin<br>
 _______________________________________________ <a href="mailto:Pd-list@lists.iem.at" target="_blank">Pd-list@lists.iem.at</a> mailing list UNSUBSCRIBE and account-management -> <a href="https://lists.puredata.info/listinfo/pd-list%5Bhttps://lists.puredata.info/listinfo/pd-list%5D" rel="noreferrer" target="_blank">https://lists.puredata.info/listinfo/pd-list[https://lists.puredata.info/listinfo/pd-list]</a><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div>Arda EDEN<br>Yıldız Teknik Üniversitesi<br></div><div>Sanat ve Tasarım Fakültesi<br></div><div>Müzik ve Sahne Sanatları Bölümü<br></div><div>Duysal (Ses) Sanatları Tasarımı Programı<br></div><div>İstanbul/Türkiye<br><br></div><div>Yildiz Technical University<br></div><div>Faculty of Art and Design<br></div><div>Department of Music and Performing Arts<br></div><div>Audio Design Program<br></div><div>Istanbul/Turkey<br></div></div></div>