@@ -79,6 +79,19 @@ 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
+ repr :: < f16 > ( e, m)
93
+ }
94
+
82
95
pub fn u32_to_f32_bits ( i : u32 ) -> u32 {
83
96
if i == 0 {
84
97
return 0 ;
@@ -122,6 +135,26 @@ mod int_to_float {
122
135
( h as u128 ) << 64
123
136
}
124
137
138
+ pub fn u64_to_f16_bits ( i : u64 ) -> u16 {
139
+ let n = i. leading_zeros ( ) ;
140
+ let i_m = i. wrapping_shl ( n) ; // Mantissa, shifted so the first bit is nonzero
141
+ let m_base: u16 = ( i_m >> shift_f_lt_i :: < u64 , f16 > ( ) ) as u16 ;
142
+
143
+ // Within the upper `F::BITS`, everything except for the signifcand
144
+ // gets truncated
145
+ let d1: u16 = ( i_m >> ( u64:: BITS - f16:: BITS - f16:: SIGNIFICAND_BITS - 1 ) ) . cast ( ) ;
146
+
147
+ // The entire rest of `i_m` gets truncated. Zero the upper `F::BITS` then just
148
+ // check if it is nonzero.
149
+ let d2: u16 = ( i_m << f16:: BITS >> f16:: BITS != 0 ) . into ( ) ;
150
+ let adj = d1 | d2;
151
+
152
+ // Mantissa with implicit bit set
153
+ let m = m_adj :: < f16 > ( m_base, adj) ;
154
+ let e = if i == 0 { 0 } else { exp :: < u64 , f16 > ( n) - 1 } ;
155
+ repr :: < f16 > ( e, m)
156
+ }
157
+
125
158
pub fn u64_to_f32_bits ( i : u64 ) -> u32 {
126
159
let n = i. leading_zeros ( ) ;
127
160
let i_m = i. wrapping_shl ( n) ;
@@ -160,6 +193,26 @@ mod int_to_float {
160
193
repr :: < f128 > ( e, m)
161
194
}
162
195
196
+ pub fn u128_to_f16_bits ( i : u128 ) -> u16 {
197
+ let n = i. leading_zeros ( ) ;
198
+ let i_m = i. wrapping_shl ( n) ; // Mantissa, shifted so the first bit is nonzero
199
+ let m_base: u16 = ( i_m >> shift_f_lt_i :: < u128 , f16 > ( ) ) as u16 ;
200
+
201
+ // Within the upper `F::BITS`, everything except for the signifcand
202
+ // gets truncated
203
+ let d1: u16 = ( i_m >> ( u128:: BITS - f16:: BITS - f16:: SIGNIFICAND_BITS - 1 ) ) . cast ( ) ;
204
+
205
+ // The entire rest of `i_m` gets truncated. Zero the upper `F::BITS` then just
206
+ // check if it is nonzero.
207
+ let d2: u16 = ( i_m << f16:: BITS >> f16:: BITS != 0 ) . into ( ) ;
208
+ let adj = d1 | d2;
209
+
210
+ // Mantissa with implicit bit set
211
+ let m = m_adj :: < f16 > ( m_base, adj) ;
212
+ let e = if i == 0 { 0 } else { exp :: < u128 , f16 > ( n) - 1 } ;
213
+ repr :: < f16 > ( e, m)
214
+ }
215
+
163
216
pub fn u128_to_f32_bits ( i : u128 ) -> u32 {
164
217
let n = i. leading_zeros ( ) ;
165
218
let i_m = i. wrapping_shl ( n) ; // Mantissa, shifted so the first bit is nonzero
@@ -210,6 +263,11 @@ mod int_to_float {
210
263
211
264
// Conversions from unsigned integers to floats.
212
265
intrinsics ! {
266
+ #[ cfg( f16_enabled) ]
267
+ pub extern "C" fn __floatunsihf( i: u32 ) -> f16 {
268
+ f16:: from_bits( int_to_float:: u32_to_f16_bits( i) )
269
+ }
270
+
213
271
#[ arm_aeabi_alias = __aeabi_ui2f]
214
272
pub extern "C" fn __floatunsisf( i: u32 ) -> f32 {
215
273
f32 :: from_bits( int_to_float:: u32_to_f32_bits( i) )
@@ -220,6 +278,17 @@ intrinsics! {
220
278
f64 :: from_bits( int_to_float:: u32_to_f64_bits( i) )
221
279
}
222
280
281
+ #[ ppc_alias = __floatunsikf]
282
+ #[ cfg( f128_enabled) ]
283
+ pub extern "C" fn __floatunsitf( i: u32 ) -> f128 {
284
+ f128:: from_bits( int_to_float:: u32_to_f128_bits( i) )
285
+ }
286
+
287
+ #[ cfg( f16_enabled) ]
288
+ pub extern "C" fn __floatundihf( i: u64 ) -> f16 {
289
+ f16:: from_bits( int_to_float:: u64_to_f16_bits( i) )
290
+ }
291
+
223
292
#[ arm_aeabi_alias = __aeabi_ul2f]
224
293
pub extern "C" fn __floatundisf( i: u64 ) -> f32 {
225
294
f32 :: from_bits( int_to_float:: u64_to_f32_bits( i) )
@@ -230,6 +299,17 @@ intrinsics! {
230
299
f64 :: from_bits( int_to_float:: u64_to_f64_bits( i) )
231
300
}
232
301
302
+ #[ ppc_alias = __floatundikf]
303
+ #[ cfg( f128_enabled) ]
304
+ pub extern "C" fn __floatunditf( i: u64 ) -> f128 {
305
+ f128:: from_bits( int_to_float:: u64_to_f128_bits( i) )
306
+ }
307
+
308
+ #[ cfg( f16_enabled) ]
309
+ pub extern "C" fn __floatuntihf( i: u128 ) -> f16 {
310
+ f16:: from_bits( int_to_float:: u128_to_f16_bits( i) )
311
+ }
312
+
233
313
#[ cfg_attr( target_os = "uefi" , unadjusted_on_win64) ]
234
314
pub extern "C" fn __floatuntisf( i: u128 ) -> f32 {
235
315
f32 :: from_bits( int_to_float:: u128_to_f32_bits( i) )
@@ -240,18 +320,6 @@ intrinsics! {
240
320
f64 :: from_bits( int_to_float:: u128_to_f64_bits( i) )
241
321
}
242
322
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
323
#[ ppc_alias = __floatuntikf]
256
324
#[ cfg( f128_enabled) ]
257
325
pub extern "C" fn __floatuntitf( i: u128 ) -> f128 {
@@ -261,6 +329,11 @@ intrinsics! {
261
329
262
330
// Conversions from signed integers to floats.
263
331
intrinsics ! {
332
+ #[ cfg( f16_enabled) ]
333
+ pub extern "C" fn __floatsihf( i: i32 ) -> f16 {
334
+ int_to_float:: signed( i, int_to_float:: u32_to_f16_bits)
335
+ }
336
+
264
337
#[ arm_aeabi_alias = __aeabi_i2f]
265
338
pub extern "C" fn __floatsisf( i: i32 ) -> f32 {
266
339
int_to_float:: signed( i, int_to_float:: u32_to_f32_bits)
@@ -271,6 +344,17 @@ intrinsics! {
271
344
int_to_float:: signed( i, int_to_float:: u32_to_f64_bits)
272
345
}
273
346
347
+ #[ ppc_alias = __floatsikf]
348
+ #[ cfg( f128_enabled) ]
349
+ pub extern "C" fn __floatsitf( i: i32 ) -> f128 {
350
+ int_to_float:: signed( i, int_to_float:: u32_to_f128_bits)
351
+ }
352
+
353
+ #[ cfg( f16_enabled) ]
354
+ pub extern "C" fn __floatdihf( i: i64 ) -> f16 {
355
+ int_to_float:: signed( i, int_to_float:: u64_to_f16_bits)
356
+ }
357
+
274
358
#[ arm_aeabi_alias = __aeabi_l2f]
275
359
pub extern "C" fn __floatdisf( i: i64 ) -> f32 {
276
360
int_to_float:: signed( i, int_to_float:: u64_to_f32_bits)
@@ -281,6 +365,17 @@ intrinsics! {
281
365
int_to_float:: signed( i, int_to_float:: u64_to_f64_bits)
282
366
}
283
367
368
+ #[ ppc_alias = __floatdikf]
369
+ #[ cfg( f128_enabled) ]
370
+ pub extern "C" fn __floatditf( i: i64 ) -> f128 {
371
+ int_to_float:: signed( i, int_to_float:: u64_to_f128_bits)
372
+ }
373
+
374
+ #[ cfg( f16_enabled) ]
375
+ pub extern "C" fn __floattihf( i: i128 ) -> f16 {
376
+ int_to_float:: signed( i, int_to_float:: u128_to_f16_bits)
377
+ }
378
+
284
379
#[ cfg_attr( target_os = "uefi" , unadjusted_on_win64) ]
285
380
pub extern "C" fn __floattisf( i: i128 ) -> f32 {
286
381
int_to_float:: signed( i, int_to_float:: u128_to_f32_bits)
@@ -291,18 +386,6 @@ intrinsics! {
291
386
int_to_float:: signed( i, int_to_float:: u128_to_f64_bits)
292
387
}
293
388
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
389
#[ ppc_alias = __floattikf]
307
390
#[ cfg( f128_enabled) ]
308
391
pub extern "C" fn __floattitf( i: i128 ) -> f128 {
0 commit comments