Signbit, tests of frexp.

This commit is contained in:
Christoffer Lerno
2023-03-18 19:13:17 +01:00
parent a041c53cdd
commit 48a35b3277
4 changed files with 43 additions and 8 deletions

View File

@@ -208,7 +208,7 @@ macro bool may_load_atomic($Type)
}
macro bool is_promotable_to_floatlike($Type) => types::is_floatlike($Type) || types::is_int($Type);
macro bool is_promotable_to_float($Type) => types::is_float($Type) || types::is_int($Type);
macro bool is_same_vector_type($Type1, $Type2)
{

View File

@@ -8,6 +8,7 @@ macro bool @convertable_to(#a, #b) => $checks($typeof(#b) x = #a);
macro bool @is_floatlike(#value) => types::is_floatlike($typeof(#value));
macro bool @is_float(#value) => types::is_float($typeof(#value));
macro bool @is_promotable_to_floatlike(#value) => types::is_promotable_to_floatlike($typeof(#value));
macro bool @is_promotable_to_float(#value) => types::is_promotable_to_float($typeof(#value));
macro bool @is_same_vector_type(#value1, #value2) => types::is_same_vector_type($typeof(#value1), $typeof(#value2));
macro promote_int(x)
{

View File

@@ -329,15 +329,31 @@ macro pow(x, exp)
}
/**
* @require values::@is_float(a) : `The input must be floating type`
* @require values::@is_promotable_to_float(x) : `The input must be integer or floating type`
**/
macro frexp(x, int* e)
{
$if (@typeis(x, float)):
return _frexpf(x, e);
$else:
return _frexp(x, e);
$endif;
$switch($typeof(x)):
$case float:
$case float16:
return _frexpf((float)x, e);
$default:
return _frexp((double)x, e);
$endswitch;
}
/**
* @require values::@is_promotable_to_float(x) : `The input must be integer or floating type`
**/
macro int signbit(x)
{
$switch($typeof(x)):
$case float:
$case float16:
return bitcast((float)x, uint) >> 31;
$default:
return (int)(bitcast((double)x, ulong) >> 63);
$endswitch;
}
/**
@@ -716,4 +732,4 @@ extern fn double _tan(double x) @extern("tan");
extern fn float _tanf(float x) @extern("tanf");
extern fn double _scalbn(double x, int n) @extern("scalbn");
extern fn double _frexp(double x, int* e) @extern("frexp");
extern fn float _frexpf(float x, int* e) @extern("frexp");
extern fn float _frexpf(float x, int* e) @extern("frexpf");

View File

@@ -0,0 +1,18 @@
module std::math @test;
fn void test_frexp()
{
int a;
double z = math::frexp(231.23, &a);
assert((z - 0.903242187) < 0.0000001 && a == 8);
float z2 = math::frexp(231.23f, &a);
assert((z2 - 0.903242187) < 0.0000001 && a == 8);
}
fn void test_signbit()
{
assert(math::signbit(-231.3) == 1);
assert(math::signbit(231.3) == 0);
assert(math::signbit(float.inf) == 0);
assert(math::signbit(-float.inf) == 1);
}