Skip to content

Commit 5ad6e37

Browse files
committed
Merge pull request atomvm#475 from fadushin/externalize_floats
Add support for term_to_binary(Float) These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents 79220c2 + 1446d89 commit 5ad6e37

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

src/libAtomVM/externalterm.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,19 @@ static int serialize_term(Context *ctx, uint8_t *buf, term t)
219219
}
220220
return 5;
221221

222+
} else if (term_is_float(t)) {
223+
if (!IS_NULL_PTR(buf)) {
224+
avm_float_t val = term_to_float(t);
225+
buf[0] = NEW_FLOAT_EXT;
226+
union {
227+
uint64_t intvalue;
228+
double doublevalue;
229+
} v;
230+
v.doublevalue = val;
231+
WRITE_64_UNALIGNED(buf + 1, v.intvalue);
232+
}
233+
return NEW_FLOAT_EXT_SIZE;
234+
222235
} else if (term_is_atom(t)) {
223236
AtomString atom_string = globalcontext_atomstring_from_term(ctx->global, t);
224237
size_t atom_len = atom_string_len(atom_string);
@@ -338,7 +351,7 @@ static term parse_external_terms(const uint8_t *external_term_buf, int *eterm_si
338351
} v;
339352
v.intvalue = READ_64_UNALIGNED(external_term_buf + 1);
340353

341-
*eterm_size = 9;
354+
*eterm_size = NEW_FLOAT_EXT_SIZE;
342355
return term_from_float(v.doublevalue, ctx);
343356
}
344357

tests/erlang_tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ compile_erlang(floatneg)
348348
compile_erlang(floatabs)
349349
compile_erlang(floatdiv)
350350
compile_erlang(floatmath)
351+
compile_erlang(floatext)
351352

352353
compile_erlang(boxed_is_not_float)
353354
compile_erlang(float_is_float)
@@ -760,6 +761,7 @@ add_custom_target(erlang_test_modules DEPENDS
760761
floatabs.beam
761762
floatdiv.beam
762763
floatmath.beam
764+
floatext.beam
763765

764766
boxed_is_not_float.beam
765767
float_is_float.beam

tests/erlang_tests/floatext.erl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
%
2+
% This file is part of AtomVM.
3+
%
4+
% Copyright 2022 Fred Dushin <[email protected]>
5+
%
6+
% Licensed under the Apache License, Version 2.0 (the "License");
7+
% you may not use this file except in compliance with the License.
8+
% You may obtain a copy of the License at
9+
%
10+
% http://www.apache.org/licenses/LICENSE-2.0
11+
%
12+
% Unless required by applicable law or agreed to in writing, software
13+
% distributed under the License is distributed on an "AS IS" BASIS,
14+
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
% See the License for the specific language governing permissions and
16+
% limitations under the License.
17+
%
18+
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
19+
%
20+
21+
-module(floatext).
22+
23+
-export([start/0]).
24+
25+
start() ->
26+
true = test_reverse(3.14159265, <<131, 70, 64, 9, 33, 251, 83, 200, 212, 241>>),
27+
true = test_reverse(0.0, <<131, 70, 0, 0, 0, 0, 0, 0, 0, 0>>),
28+
true = test_reverse(negate(0.0), <<131, 70, 128, 0, 0, 0, 0, 0, 0, 0>>),
29+
true = test_reverse(negate(3.14159265), <<131, 70, 192, 9, 33, 251, 83, 200, 212, 241>>),
30+
0.
31+
32+
test_reverse(T, Interop) ->
33+
Bin = erlang:term_to_binary(T),
34+
Bin = Interop,
35+
{X, Used} = erlang:binary_to_term(Bin, [used]),
36+
Used = erlang:byte_size(Bin),
37+
X =:= T.
38+
39+
negate(X) ->
40+
-1 * X.

tests/test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ struct Test tests[] = {
397397
TEST_CASE_EXPECTED(bin2float, 511),
398398
TEST_CASE_EXPECTED(list2float, 511),
399399
TEST_CASE(floatmath),
400+
TEST_CASE(floatext),
400401

401402
TEST_CASE(test_fp_allocate_heap_zero),
402403

0 commit comments

Comments
 (0)