llvm-mos / llvm-mos-sdk

SDK for developing with the llvm-mos compiler
https://www.llvm-mos.org
Other
255 stars 52 forks source link

Implement libm min and max functions #299

Closed mrk-its closed 4 months ago

mrk-its commented 5 months ago

In RUST cast from float to int (with as keyword) is saturating operation: https://doc.rust-lang.org/reference/expressions/operator-expr.html#numeric-cast

and for now it generates following error:

LLVM ERROR: unable to legalize instruction: %22:_(s32) = G_INTRINSIC intrinsic(@llvm.fptosi.sat), %8:_(s32) (in function: main)
mrk-its commented 5 months ago

As workaround non-saturating cast may be performed with https://doc.rust-lang.org/std/primitive.f32.html#method.to_int_unchecked

mysterymath commented 5 months ago

This apparently lowered to the max and min family of functions in libm; we will need an implementation for those.

mlund commented 4 months ago

Does this involve adding the functions to common/include/math.h, and add a new corresponding math.cpp file to produce a static library m? Also, are comparison operators already available for floats?

mysterymath commented 4 months ago

Does this involve adding the functions to common/include/math.h, and add a new corresponding math.cpp file to produce a static library m? Also, are comparison operators already available for floats?

Yes to all.

mlund commented 4 months ago

Here's a preliminary implementation. Not entirely sure if this should be split into it's own static library with add_platform_library(m, ...)?. https://github.com/llvm-mos/llvm-mos-sdk/compare/main...mlund:llvm-mos-sdk:fminmax?expand=1

mysterymath commented 4 months ago

Here's a preliminary implementation. Not entirely sure if this should be split into it's own static library with add_platform_library(m, ...)?. https://github.com/llvm-mos/llvm-mos-sdk/compare/main...mlund:llvm-mos-sdk:fminmax?expand=1

Separate libm provides no benefit to modern static-linking-only toolchain; best not to follow that particular convention. We should provide an empty stub libm though to make sure -lm is always legal.

You will need to account for NaN: min and max have to return the other argument if one is NaN.