You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: vlib/math/unsafe.v
+42-8
Original file line number
Diff line number
Diff line change
@@ -3,13 +3,24 @@
3
3
// that can be found in the LICENSE file.
4
4
modulemath
5
5
6
+
unionU32_F32 {
7
+
u u32
8
+
f f32
9
+
}
10
+
6
11
// f32_bits returns the IEEE 754 binary representation of f,
7
12
// with the sign bit of f and the result in the same bit position.
8
13
// f32_bits(f32_from_bits(x)) == x.
9
14
@[inline]
10
15
pub fnf32_bits(f f32) u32 {
11
-
p:=*unsafe { &u32(&f) }
12
-
return p
16
+
$if tinyc {
17
+
return*unsafe { &u32(&f) } // this is faster for tcc, but causes `error: dereferencing type-punned pointer will break strict-aliasing rules` on gcc, with -cstrict
18
+
}
19
+
returnunsafe {
20
+
U32_F32{
21
+
f: f
22
+
}.u
23
+
}
13
24
}
14
25
15
26
// f32_from_bits returns the floating-point number corresponding
@@ -18,17 +29,34 @@ pub fn f32_bits(f f32) u32 {
18
29
// f32_from_bits(f32_bits(x)) == x.
19
30
@[inline]
20
31
pub fnf32_from_bits(b u32) f32 {
21
-
p:=*unsafe { &f32(&b) }
22
-
return p
32
+
$if tinyc {
33
+
return*unsafe { &f32(&b) }
34
+
}
35
+
returnunsafe {
36
+
U32_F32{
37
+
u: b
38
+
}.f
39
+
}
40
+
}
41
+
42
+
unionU64_F64 {
43
+
u u64
44
+
f f64
23
45
}
24
46
25
47
// f64_bits returns the IEEE 754 binary representation of f,
26
48
// with the sign bit of f and the result in the same bit position,
27
49
// and f64_bits(f64_from_bits(x)) == x.
28
50
@[inline]
29
51
pub fnf64_bits(f f64) u64 {
30
-
p:=*unsafe { &u64(&f) }
31
-
return p
52
+
$if tinyc {
53
+
return*unsafe { &u64(&f) }
54
+
}
55
+
returnunsafe {
56
+
U64_F64{
57
+
f: f
58
+
}.u
59
+
}
32
60
}
33
61
34
62
// f64_from_bits returns the floating-point number corresponding
0 commit comments