Skip to content

Commit 933f930

Browse files
committed
Add float/1 BIF
Signed-off-by: Jakub Gonet <jakub.gonet@swmansion.com>
1 parent 9bf2202 commit 933f930

File tree

7 files changed

+79
-0
lines changed

7 files changed

+79
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010

1111
- Added the ability to run beams from the CLI for Generic Unix platform (it was already possible with nodejs and emscripten).
1212
- Added preliminary support for ESP32P4 (no networking support yet).
13+
- Support for `float/1` BIF.
1314

1415
### Fixed
1516

src/libAtomVM/bif.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,27 @@ term bif_erlang_trunc_1(Context *ctx, uint32_t fail_label, int live, term arg1)
11871187
}
11881188
}
11891189

1190+
term bif_erlang_float_1(Context *ctx, uint32_t fail_label, int live, term arg1)
1191+
{
1192+
if (term_is_float(arg1)) {
1193+
return arg1;
1194+
}
1195+
1196+
if (!term_is_any_integer(arg1)) {
1197+
RAISE_ERROR_BIF(fail_label, BADARG_ATOM);
1198+
}
1199+
1200+
avm_float_t fresult = term_conv_to_float(arg1);
1201+
if (UNLIKELY(!isfinite(fresult))) {
1202+
RAISE_ERROR_BIF(fail_label, BADARITH_ATOM);
1203+
}
1204+
1205+
if (UNLIKELY(memory_ensure_free_with_roots(ctx, FLOAT_SIZE, live, ctx->x, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
1206+
RAISE_ERROR_BIF(fail_label, OUT_OF_MEMORY_ATOM);
1207+
}
1208+
return term_from_float(fresult, &ctx->heap);
1209+
}
1210+
11901211
typedef int64_t (*bitwise_op)(int64_t a, int64_t b);
11911212

11921213
static inline term bitwise_helper(Context *ctx, uint32_t fail_label, int live, term arg1, term arg2, bitwise_op op)

src/libAtomVM/bif.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ term bif_erlang_ceil_1(Context *ctx, uint32_t fail_label, int live, term arg1);
8181
term bif_erlang_floor_1(Context *ctx, uint32_t fail_label, int live, term arg1);
8282
term bif_erlang_round_1(Context *ctx, uint32_t fail_label, int live, term arg1);
8383
term bif_erlang_trunc_1(Context *ctx, uint32_t fail_label, int live, term arg1);
84+
term bif_erlang_float_1(Context *ctx, uint32_t fail_label, int live, term arg1);
8485

8586
term bif_erlang_bor_2(Context *ctx, uint32_t fail_label, int live, term arg1, term arg2);
8687
term bif_erlang_band_2(Context *ctx, uint32_t fail_label, int live, term arg1, term arg2);

src/libAtomVM/bifs.gperf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ erlang:ceil/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_er
7777
erlang:floor/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_erlang_floor_1}
7878
erlang:round/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_erlang_round_1}
7979
erlang:trunc/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_erlang_trunc_1}
80+
erlang:float/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_erlang_float_1}
8081
erlang:bor/2, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif2_ptr = bif_erlang_bor_2}
8182
erlang:band/2, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif2_ptr = bif_erlang_band_2}
8283
erlang:bxor/2, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif2_ptr = bif_erlang_bxor_2}

tests/erlang_tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ compile_erlang(float2bin2scientific)
394394
compile_erlang(float2bin2)
395395
compile_erlang(float2list2decimals)
396396
compile_erlang(float2list2scientific)
397+
compile_erlang(float_bif)
397398
compile_erlang(float2list2)
398399
compile_erlang(bin2float)
399400
compile_erlang(list2float)
@@ -868,6 +869,7 @@ add_custom_target(erlang_test_modules DEPENDS
868869
float2bin2.beam
869870
float2list2decimals.beam
870871
float2list2scientific.beam
872+
float_bif.beam
871873
float2list2.beam
872874
bin2float.beam
873875
list2float.beam

tests/erlang_tests/float_bif.erl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
%
2+
% This file is part of AtomVM.
3+
%
4+
% Copyright 2025 Jakub Gonet <jakub.gonet@swmansion.com>
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(float_bif).
22+
23+
-export([start/0]).
24+
% For remote function calls
25+
-export([id/1, check_one/1]).
26+
27+
-define(ID(Arg), ?MODULE:id(Arg)).
28+
29+
start() ->
30+
true = 1.0 =:= float(?ID(1)),
31+
true = 1.0 =:= float(?ID(1.0)),
32+
ok =
33+
try float(?ID("1")) of
34+
_ ->
35+
unreachable
36+
catch
37+
error:badarg ->
38+
ok
39+
end,
40+
ok = ?MODULE:check_one(?ID(1)),
41+
ok = ?MODULE:check_one(?ID(1.0)),
42+
error = ?MODULE:check_one(?ID(atom)),
43+
0.
44+
45+
id(X) ->
46+
X.
47+
48+
% Tests float/1 in guards
49+
check_one(T) when float(T) =:= 1.0 ->
50+
ok;
51+
check_one(_T) ->
52+
error.

tests/test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ struct Test tests[] = {
444444
TEST_CASE_EXPECTED(float2bin2decimals, 255),
445445
TEST_CASE_EXPECTED(float2bin2, 31),
446446
TEST_CASE_EXPECTED(float2list2scientific, 31),
447+
TEST_CASE(float_bif),
447448
TEST_CASE_EXPECTED(float2list2decimals, 255),
448449
TEST_CASE_EXPECTED(float2list2, 31),
449450
TEST_CASE_EXPECTED(bin2float, 511),

0 commit comments

Comments
 (0)