Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add float/1 BIF #1518

Open
wants to merge 1 commit into
base: release-0.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

### Fixed

Expand Down
21 changes: 21 additions & 0 deletions src/libAtomVM/bif.c
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,27 @@ term bif_erlang_trunc_1(Context *ctx, uint32_t fail_label, int live, term arg1)
}
}

term bif_erlang_float_1(Context *ctx, uint32_t fail_label, int live, term arg1)
{
if (term_is_float(arg1)) {
return arg1;
}

if (!term_is_any_integer(arg1)) {
RAISE_ERROR_BIF(fail_label, BADARG_ATOM);
}

avm_float_t fresult = term_conv_to_float(arg1);
if (UNLIKELY(!isfinite(fresult))) {
RAISE_ERROR_BIF(fail_label, BADARITH_ATOM);
}

if (UNLIKELY(memory_ensure_free_with_roots(ctx, FLOAT_SIZE, live, ctx->x, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
RAISE_ERROR_BIF(fail_label, OUT_OF_MEMORY_ATOM);
}
return term_from_float(fresult, &ctx->heap);
}

typedef int64_t (*bitwise_op)(int64_t a, int64_t b);

static inline term bitwise_helper(Context *ctx, uint32_t fail_label, int live, term arg1, term arg2, bitwise_op op)
Expand Down
1 change: 1 addition & 0 deletions src/libAtomVM/bif.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ term bif_erlang_ceil_1(Context *ctx, uint32_t fail_label, int live, term arg1);
term bif_erlang_floor_1(Context *ctx, uint32_t fail_label, int live, term arg1);
term bif_erlang_round_1(Context *ctx, uint32_t fail_label, int live, term arg1);
term bif_erlang_trunc_1(Context *ctx, uint32_t fail_label, int live, term arg1);
term bif_erlang_float_1(Context *ctx, uint32_t fail_label, int live, term arg1);

term bif_erlang_bor_2(Context *ctx, uint32_t fail_label, int live, term arg1, term arg2);
term bif_erlang_band_2(Context *ctx, uint32_t fail_label, int live, term arg1, term arg2);
Expand Down
1 change: 1 addition & 0 deletions src/libAtomVM/bifs.gperf
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ erlang:ceil/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_er
erlang:floor/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_erlang_floor_1}
erlang:round/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_erlang_round_1}
erlang:trunc/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_erlang_trunc_1}
erlang:float/1, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif1_ptr = bif_erlang_float_1}
erlang:bor/2, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif2_ptr = bif_erlang_bor_2}
erlang:band/2, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif2_ptr = bif_erlang_band_2}
erlang:bxor/2, {.gcbif.base.type = GCBIFFunctionType, .gcbif.gcbif2_ptr = bif_erlang_bxor_2}
Expand Down
2 changes: 2 additions & 0 deletions tests/erlang_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ compile_erlang(float2bin2scientific)
compile_erlang(float2bin2)
compile_erlang(float2list2decimals)
compile_erlang(float2list2scientific)
compile_erlang(float_bif)
compile_erlang(float2list2)
compile_erlang(bin2float)
compile_erlang(list2float)
Expand Down Expand Up @@ -868,6 +869,7 @@ add_custom_target(erlang_test_modules DEPENDS
float2bin2.beam
float2list2decimals.beam
float2list2scientific.beam
float_bif.beam
float2list2.beam
bin2float.beam
list2float.beam
Expand Down
52 changes: 52 additions & 0 deletions tests/erlang_tests/float_bif.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
%
% This file is part of AtomVM.
%
% Copyright 2025 Jakub Gonet <[email protected]>
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
% You may obtain a copy of the License at
%
% http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS,
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
% See the License for the specific language governing permissions and
% limitations under the License.
%
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
%

-module(float_bif).

-export([start/0]).
% For remote function calls
-export([id/1, check_one/1]).

-define(ID(Arg), ?MODULE:id(Arg)).

start() ->
true = 1.0 =:= float(?ID(1)),
true = 1.0 =:= float(?ID(1.0)),
ok =
try float(?ID("1")) of
_ ->
unreachable
catch
error:badarg ->
ok
end,
ok = ?MODULE:check_one(?ID(1)),
ok = ?MODULE:check_one(?ID(1.0)),
error = ?MODULE:check_one(?ID(atom)),
0.

id(X) ->
X.

% Tests float/1 in guards
check_one(T) when float(T) =:= 1.0 ->
ok;
check_one(_T) ->
error.
1 change: 1 addition & 0 deletions tests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ struct Test tests[] = {
TEST_CASE_EXPECTED(float2bin2decimals, 255),
TEST_CASE_EXPECTED(float2bin2, 31),
TEST_CASE_EXPECTED(float2list2scientific, 31),
TEST_CASE(float_bif),
TEST_CASE_EXPECTED(float2list2decimals, 255),
TEST_CASE_EXPECTED(float2list2, 31),
TEST_CASE_EXPECTED(bin2float, 511),
Expand Down
Loading