feat: add as type casting

This commit is contained in:
2026-04-22 22:40:19 +02:00
parent e66a4ee736
commit 041a49e574
13 changed files with 350 additions and 1 deletions
+21
View File
@@ -0,0 +1,21 @@
[code]
fn promote_f32_to_f64(x: f32) -> f64 {
return x as f64;
}
fn demote_f64_to_f32(x: f64) -> f32 {
return x as f32;
}
[harness]
extern double promote_f32_to_f64(float x);
extern float demote_f64_to_f32(double x);
int main() {
if (promote_f32_to_f64(3.5f) != 3.5) return 1;
if (demote_f64_to_f32(2.25) != 2.25f) return 2;
return 0;
}
[expected_return_code]
0
+24
View File
@@ -0,0 +1,24 @@
[code]
fn cast_f32_to_i32(x: f32) -> i32 {
return x as i32;
}
fn cast_f64_to_u64(x: f64) -> u64 {
return x as u64;
}
[harness]
#include <stdint.h>
extern int32_t cast_f32_to_i32(float x);
extern uint64_t cast_f64_to_u64(double x);
int main() {
if (cast_f32_to_i32(3.14f) != 3) return 1;
if (cast_f32_to_i32(-2.9f) != -2) return 2;
if (cast_f64_to_u64(42.999) != 42) return 3;
return 0;
}
[expected_return_code]
0
+24
View File
@@ -0,0 +1,24 @@
[code]
fn cast_i32_to_f32(x: i32) -> f32 {
return x as f32;
}
fn cast_u64_to_f64(x: u64) -> f64 {
return x as f64;
}
[harness]
#include <stdint.h>
extern float cast_i32_to_f32(int32_t x);
extern double cast_u64_to_f64(uint64_t x);
int main() {
if (cast_i32_to_f32(42) != 42.0f) return 1;
if (cast_i32_to_f32(-10) != -10.0f) return 2;
if (cast_u64_to_f64(1337) != 1337.0) return 3;
return 0;
}
[expected_return_code]
0
+23
View File
@@ -0,0 +1,23 @@
[code]
fn truncate_i32_to_i8(x: i32) -> i8 {
return x as i8;
}
fn extend_u8_to_i32(x: u8) -> i32 {
return x as i32;
}
[harness]
#include <stdint.h>
extern int8_t truncate_i32_to_i8(int32_t x);
extern int32_t extend_u8_to_i32(uint8_t x);
int main() {
if (truncate_i32_to_i8(257) != 1) return 1; // 257 = 0x0101, truncated to 8 bits is 0x01
if (extend_u8_to_i32(255) != 255) return 2;
return 0;
}
[expected_return_code]
0