@@ -285,11 +285,13 @@ impl Size {
285
285
}
286
286
287
287
/// Alignment of a type in bytes, both ABI-mandated and preferred.
288
- /// Since alignments are always powers of 2, we can pack both in one byte,
289
- /// giving each a nibble (4 bits) for a maximum alignment of 2<sup>15</sup> = 32768.
288
+ /// Each field is a power of two, giving the alignment a maximum
289
+ /// value of 2^(2^8 - 1), which is limited by LLVM to a i32, with
290
+ /// a maximum capacity of 2^31 - 1 or 2147483647.
290
291
#[ derive( Copy , Clone , PartialEq , Eq , Hash , Debug ) ]
291
292
pub struct Align {
292
- raw : u8
293
+ abi : u8 ,
294
+ pref : u8 ,
293
295
}
294
296
295
297
impl Align {
@@ -298,7 +300,7 @@ impl Align {
298
300
}
299
301
300
302
pub fn from_bytes ( abi : u64 , pref : u64 ) -> Result < Align , String > {
301
- let pack = |align : u64 | {
303
+ let log2 = |align : u64 | {
302
304
// Treat an alignment of 0 bytes like 1-byte alignment.
303
305
if align == 0 {
304
306
return Ok ( 0 ) ;
@@ -312,39 +314,38 @@ impl Align {
312
314
}
313
315
if bytes != 1 {
314
316
Err ( format ! ( "`{}` is not a power of 2" , align) )
315
- } else if pow > 0x0f {
317
+ } else if pow > 30 {
316
318
Err ( format ! ( "`{}` is too large" , align) )
317
319
} else {
318
320
Ok ( pow)
319
321
}
320
322
} ;
321
323
322
324
Ok ( Align {
323
- raw : pack ( abi) ? | ( pack ( pref) ? << 4 )
325
+ abi : log2 ( abi) ?,
326
+ pref : log2 ( pref) ?,
324
327
} )
325
328
}
326
329
327
330
pub fn abi ( self ) -> u64 {
328
- 1 << ( self . raw & 0xf )
331
+ 1 << self . abi
329
332
}
330
333
331
334
pub fn pref ( self ) -> u64 {
332
- 1 << ( self . raw >> 4 )
335
+ 1 << self . pref
333
336
}
334
337
335
338
pub fn min ( self , other : Align ) -> Align {
336
- let abi = cmp:: min ( self . raw & 0x0f , other. raw & 0x0f ) ;
337
- let pref = cmp:: min ( self . raw & 0xf0 , other. raw & 0xf0 ) ;
338
339
Align {
339
- raw : abi | pref
340
+ abi : cmp:: min ( self . abi , other. abi ) ,
341
+ pref : cmp:: min ( self . pref , other. pref ) ,
340
342
}
341
343
}
342
344
343
345
pub fn max ( self , other : Align ) -> Align {
344
- let abi = cmp:: max ( self . raw & 0x0f , other. raw & 0x0f ) ;
345
- let pref = cmp:: max ( self . raw & 0xf0 , other. raw & 0xf0 ) ;
346
346
Align {
347
- raw : abi | pref
347
+ abi : cmp:: max ( self . abi , other. abi ) ,
348
+ pref : cmp:: max ( self . pref , other. pref ) ,
348
349
}
349
350
}
350
351
}
0 commit comments