[erlang-questions] Accessing A Binary as an Array of Floats/Doubles Within a NIF
Tony Rogvall
tony@REDACTED
Tue Jun 21 01:10:27 CEST 2016
> On 20 jun 2016, at 22:24, Sverker Eriksson <sverker.eriksson@REDACTED> wrote:
>
> Well, reference counted binaries have 8-byte aligned data.
> Heap binaries only half of the time on 32-bit :-)
Did not think of that. I guess that it should be fixed ?
I can see traces of an align flag... Maybe resurrect that flag?
Or better to have something like unit syntax for alignment?
<<F:64/float-native-align:64>>
But alignment just for powers of two.
/Tony
>
> Both driver_alloc_binary() and enif_alloc_binary() guarantee
> 8-byte alignment of the returned payload data.
>
> /Sverker
>
>
> On 06/20/2016 10:07 PM, Tony Rogvall wrote:
>> I just found the answer in binary.c.
>> The answer is that all binary data is always 8 byte aligned.
>> And that is nice :-)
>>
>> /Tony
>>
>>> On 20 jun 2016, at 21:59, Tony Rogvall <tony@REDACTED> wrote:
>>>
>>>> On 20 jun 2016, at 21:50, Sverker Eriksson <sverker.eriksson@REDACTED> wrote:
>>>>
>>>>
>>>> If you use <<1.0/float-native>>
>>>> then it's just type cast to a 'double' pointer:
>>>>
>>>> double* my_array;
>>>>
>>>> my_array = (double*) Bin.data;
>>>>
>>>> printf("%lf", my_array[0]);
>>>>
>>>>
>>>> Default byte order is 'float-big', which will not work on little endian machines (x86).
>>>>
>>>> Depending on architecture you also might have to ensure 8 byte alignment.
>>>> x86 will accept unaligned floats but the performance may suffer.
>>>>
>>> Is there an easy way to ensure 8 byte alignment in Erlang, when constructing binaries?
>>> On a 64-bit machine/arch my guess is that Binary data is 8 byte aligned but
>>> what about a on 32-bit machine/arch?
>>>
>>> /Tony
>>>
>>>> /Sverker, Erlang/OTP
>>>>
>>>>
>>>> On 06/20/2016 09:04 PM, John Duffy wrote:
>>>>> Hi
>>>>> I'm using FreeBSD 10.3 & Erlang 18.3 and I'm trying to get to grips with passing a binary of floats to a NIF for further processing as as array of C doubles.
>>>>> My simple NIF module compiles and I can access my "my_nif_1" function from the erlang shell. I can access the 'size' structure member but I can't figure out how to access the 'data' structure member as a C array. I understand that 'data' is a block of memory of UINT bytes but I can't figure out how to "cast" this to a C array within the NIF.
>>>>> Any help would be mud appreciated.
>>>>> Kind regards
>>>>> John
>>>>> #include "erl_nif.h"
>>>>> static ERL_NIF_TERM my_nif_1(ErlNifEnv * env, int argc, const ERL_NIF_TERM argv[])
>>>>> {
>>>>> ErlNifBinary Bin;
>>>>> if (!enif_inspect_binary(env, argv[0], &Bin)) {
>>>>> return enif_make_badarg(env);
>>>>> }
>>>>> // So far, so good...
>>>>> // From the erl shell...
>>>>> // my_nifs:my_nif_1(<<1.0/float>>). returns a Bin.size of 8
>>>>> // my_nifs:my_nif_1(<<1.0/float,2.0/float>>. returns a Bin.size of 16
>>>>> //
>>>>> // How do I access Bin.data as an array of doubles?
>>>>> //
>>>>> return enif_make_int(env, Bin.size); // For testing... this works.
>>>>> }
>>>>> static ErlNifFunc nif_funcs[] = {
>>>>> {"my_nif_1", 1, my_nif_1}
>>>>> };
>>>>> ERL_NIF_INIT(my_nifs, nif_funcs, NULL, NULL, NULL, NULL);
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> erlang-questions mailing list
>>>>>
>>>>> erlang-questions@REDACTED
>>>>> http://erlang.org/mailman/listinfo/erlang-questions
>>>> _______________________________________________
>>>> erlang-questions mailing list
>>>> erlang-questions@REDACTED
>>>> http://erlang.org/mailman/listinfo/erlang-questions
>>> _______________________________________________
>>> erlang-questions mailing list
>>> erlang-questions@REDACTED
>>> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160621/882018a8/attachment.bin>
More information about the erlang-questions
mailing list