@@ -79,6 +79,25 @@ mod int_to_float {
79
79
F :: from_bits ( conv ( i. unsigned_abs ( ) ) | sign_bit)
80
80
}
81
81
82
+ pub fn u32_to_f16_bits ( i : u32 ) -> u16 {
83
+ let n = i. leading_zeros ( ) ;
84
+ let i_m = i. wrapping_shl ( n) ;
85
+ // Mantissa with implicit bit set
86
+ let m_base: u16 = ( i_m >> shift_f_lt_i :: < u32 , f16 > ( ) ) as u16 ;
87
+ // The entire lower half of `i` will be truncated (masked portion), plus the
88
+ // next `EXPONENT_BITS` bits.
89
+ let adj = ( i_m >> f16:: EXPONENT_BITS | i_m & 0xFF ) as u16 ;
90
+ let m = m_adj :: < f16 > ( m_base, adj) ;
91
+ let e = if i == 0 { 0 } else { exp :: < u32 , f16 > ( n) - 1 } ;
92
+ // Any int can have an exponent out of range for `f16`, unlike other float types.
93
+ // Clamp this.
94
+ if e >= f16:: EXPONENT_MAX as u16 - 1 {
95
+ f16:: INFINITY . to_bits ( )
96
+ } else {
97
+ repr :: < f16 > ( e, m)
98
+ }
99
+ }
100
+
82
101
pub fn u32_to_f32_bits ( i : u32 ) -> u32 {
83
102
if i == 0 {
84
103
return 0 ;
@@ -122,6 +141,30 @@ mod int_to_float {
122
141
( h as u128 ) << 64
123
142
}
124
143
144
+ pub fn u64_to_f16_bits ( i : u64 ) -> u16 {
145
+ let n = i. leading_zeros ( ) ;
146
+ let i_m = i. wrapping_shl ( n) ; // Mantissa, shifted so the first bit is nonzero
147
+ let m_base: u16 = ( i_m >> shift_f_lt_i :: < u64 , f16 > ( ) ) as u16 ;
148
+
149
+ // Within the upper `F::BITS`, everything except for the signifcand
150
+ // gets truncated
151
+ let d1: u16 = ( i_m >> ( u64:: BITS - f16:: BITS - f16:: SIGNIFICAND_BITS - 1 ) ) . cast ( ) ;
152
+
153
+ // The entire rest of `i_m` gets truncated. Zero the upper `F::BITS` then just
154
+ // check if it is nonzero.
155
+ let d2: u16 = ( i_m << f16:: BITS >> f16:: BITS != 0 ) . into ( ) ;
156
+ let adj = d1 | d2;
157
+
158
+ // Mantissa with implicit bit set
159
+ let m = m_adj :: < f16 > ( m_base, adj) ;
160
+ let e = if i == 0 { 0 } else { exp :: < u64 , f16 > ( n) - 1 } ;
161
+ if e >= f16:: EXPONENT_MAX as u16 - 1 {
162
+ f16:: INFINITY . to_bits ( )
163
+ } else {
164
+ repr :: < f16 > ( e, m)
165
+ }
166
+ }
167
+
125
168
pub fn u64_to_f32_bits ( i : u64 ) -> u32 {
126
169
let n = i. leading_zeros ( ) ;
127
170
let i_m = i. wrapping_shl ( n) ;
@@ -160,6 +203,30 @@ mod int_to_float {
160
203
repr :: < f128 > ( e, m)
161
204
}
162
205
206
+ pub fn u128_to_f16_bits ( i : u128 ) -> u16 {
207
+ let n = i. leading_zeros ( ) ;
208
+ let i_m = i. wrapping_shl ( n) ; // Mantissa, shifted so the first bit is nonzero
209
+ let m_base: u16 = ( i_m >> shift_f_lt_i :: < u128 , f16 > ( ) ) as u16 ;
210
+
211
+ // Within the upper `F::BITS`, everything except for the signifcand
212
+ // gets truncated
213
+ let d1: u16 = ( i_m >> ( u128:: BITS - f16:: BITS - f16:: SIGNIFICAND_BITS - 1 ) ) . cast ( ) ;
214
+
215
+ // The entire rest of `i_m` gets truncated. Zero the upper `F::BITS` then just
216
+ // check if it is nonzero.
217
+ let d2: u16 = ( i_m << f16:: BITS >> f16:: BITS != 0 ) . into ( ) ;
218
+ let adj = d1 | d2;
219
+
220
+ // Mantissa with implicit bit set
221
+ let m = m_adj :: < f16 > ( m_base, adj) ;
222
+ let e = if i == 0 { 0 } else { exp :: < u128 , f16 > ( n) - 1 } ;
223
+ if e >= f16:: EXPONENT_MAX as u16 - 1 {
224
+ f16:: INFINITY . to_bits ( )
225
+ } else {
226
+ repr :: < f16 > ( e, m)
227
+ }
228
+ }
229
+
163
230
pub fn u128_to_f32_bits ( i : u128 ) -> u32 {
164
231
let n = i. leading_zeros ( ) ;
165
232
let i_m = i. wrapping_shl ( n) ; // Mantissa, shifted so the first bit is nonzero
@@ -210,6 +277,11 @@ mod int_to_float {
210
277
211
278
// Conversions from unsigned integers to floats.
212
279
intrinsics ! {
280
+ #[ cfg( f16_enabled) ]
281
+ pub extern "C" fn __floatunsihf( i: u32 ) -> f16 {
282
+ f16:: from_bits( int_to_float:: u32_to_f16_bits( i) )
283
+ }
284
+
213
285
#[ arm_aeabi_alias = __aeabi_ui2f]
214
286
pub extern "C" fn __floatunsisf( i: u32 ) -> f32 {
215
287
f32 :: from_bits( int_to_float:: u32_to_f32_bits( i) )
@@ -220,6 +292,17 @@ intrinsics! {
220
292
f64 :: from_bits( int_to_float:: u32_to_f64_bits( i) )
221
293
}
222
294
295
+ #[ ppc_alias = __floatunsikf]
296
+ #[ cfg( f128_enabled) ]
297
+ pub extern "C" fn __floatunsitf( i: u32 ) -> f128 {
298
+ f128:: from_bits( int_to_float:: u32_to_f128_bits( i) )
299
+ }
300
+
301
+ #[ cfg( f16_enabled) ]
302
+ pub extern "C" fn __floatundihf( i: u64 ) -> f16 {
303
+ f16:: from_bits( int_to_float:: u64_to_f16_bits( i) )
304
+ }
305
+
223
306
#[ arm_aeabi_alias = __aeabi_ul2f]
224
307
pub extern "C" fn __floatundisf( i: u64 ) -> f32 {
225
308
f32 :: from_bits( int_to_float:: u64_to_f32_bits( i) )
@@ -230,6 +313,17 @@ intrinsics! {
230
313
f64 :: from_bits( int_to_float:: u64_to_f64_bits( i) )
231
314
}
232
315
316
+ #[ ppc_alias = __floatundikf]
317
+ #[ cfg( f128_enabled) ]
318
+ pub extern "C" fn __floatunditf( i: u64 ) -> f128 {
319
+ f128:: from_bits( int_to_float:: u64_to_f128_bits( i) )
320
+ }
321
+
322
+ #[ cfg( f16_enabled) ]
323
+ pub extern "C" fn __floatuntihf( i: u128 ) -> f16 {
324
+ f16:: from_bits( int_to_float:: u128_to_f16_bits( i) )
325
+ }
326
+
233
327
#[ cfg_attr( target_os = "uefi" , unadjusted_on_win64) ]
234
328
pub extern "C" fn __floatuntisf( i: u128 ) -> f32 {
235
329
f32 :: from_bits( int_to_float:: u128_to_f32_bits( i) )
@@ -240,18 +334,6 @@ intrinsics! {
240
334
f64 :: from_bits( int_to_float:: u128_to_f64_bits( i) )
241
335
}
242
336
243
- #[ ppc_alias = __floatunsikf]
244
- #[ cfg( f128_enabled) ]
245
- pub extern "C" fn __floatunsitf( i: u32 ) -> f128 {
246
- f128:: from_bits( int_to_float:: u32_to_f128_bits( i) )
247
- }
248
-
249
- #[ ppc_alias = __floatundikf]
250
- #[ cfg( f128_enabled) ]
251
- pub extern "C" fn __floatunditf( i: u64 ) -> f128 {
252
- f128:: from_bits( int_to_float:: u64_to_f128_bits( i) )
253
- }
254
-
255
337
#[ ppc_alias = __floatuntikf]
256
338
#[ cfg( f128_enabled) ]
257
339
pub extern "C" fn __floatuntitf( i: u128 ) -> f128 {
@@ -261,6 +343,11 @@ intrinsics! {
261
343
262
344
// Conversions from signed integers to floats.
263
345
intrinsics ! {
346
+ #[ cfg( f16_enabled) ]
347
+ pub extern "C" fn __floatsihf( i: i32 ) -> f16 {
348
+ int_to_float:: signed( i, int_to_float:: u32_to_f16_bits)
349
+ }
350
+
264
351
#[ arm_aeabi_alias = __aeabi_i2f]
265
352
pub extern "C" fn __floatsisf( i: i32 ) -> f32 {
266
353
int_to_float:: signed( i, int_to_float:: u32_to_f32_bits)
@@ -271,6 +358,17 @@ intrinsics! {
271
358
int_to_float:: signed( i, int_to_float:: u32_to_f64_bits)
272
359
}
273
360
361
+ #[ ppc_alias = __floatsikf]
362
+ #[ cfg( f128_enabled) ]
363
+ pub extern "C" fn __floatsitf( i: i32 ) -> f128 {
364
+ int_to_float:: signed( i, int_to_float:: u32_to_f128_bits)
365
+ }
366
+
367
+ #[ cfg( f16_enabled) ]
368
+ pub extern "C" fn __floatdihf( i: i64 ) -> f16 {
369
+ int_to_float:: signed( i, int_to_float:: u64_to_f16_bits)
370
+ }
371
+
274
372
#[ arm_aeabi_alias = __aeabi_l2f]
275
373
pub extern "C" fn __floatdisf( i: i64 ) -> f32 {
276
374
int_to_float:: signed( i, int_to_float:: u64_to_f32_bits)
@@ -281,6 +379,17 @@ intrinsics! {
281
379
int_to_float:: signed( i, int_to_float:: u64_to_f64_bits)
282
380
}
283
381
382
+ #[ ppc_alias = __floatdikf]
383
+ #[ cfg( f128_enabled) ]
384
+ pub extern "C" fn __floatditf( i: i64 ) -> f128 {
385
+ int_to_float:: signed( i, int_to_float:: u64_to_f128_bits)
386
+ }
387
+
388
+ #[ cfg( f16_enabled) ]
389
+ pub extern "C" fn __floattihf( i: i128 ) -> f16 {
390
+ int_to_float:: signed( i, int_to_float:: u128_to_f16_bits)
391
+ }
392
+
284
393
#[ cfg_attr( target_os = "uefi" , unadjusted_on_win64) ]
285
394
pub extern "C" fn __floattisf( i: i128 ) -> f32 {
286
395
int_to_float:: signed( i, int_to_float:: u128_to_f32_bits)
@@ -291,18 +400,6 @@ intrinsics! {
291
400
int_to_float:: signed( i, int_to_float:: u128_to_f64_bits)
292
401
}
293
402
294
- #[ ppc_alias = __floatsikf]
295
- #[ cfg( f128_enabled) ]
296
- pub extern "C" fn __floatsitf( i: i32 ) -> f128 {
297
- int_to_float:: signed( i, int_to_float:: u32_to_f128_bits)
298
- }
299
-
300
- #[ ppc_alias = __floatdikf]
301
- #[ cfg( f128_enabled) ]
302
- pub extern "C" fn __floatditf( i: i64 ) -> f128 {
303
- int_to_float:: signed( i, int_to_float:: u64_to_f128_bits)
304
- }
305
-
306
403
#[ ppc_alias = __floattikf]
307
404
#[ cfg( f128_enabled) ]
308
405
pub extern "C" fn __floattitf( i: i128 ) -> f128 {
0 commit comments