refactor(nix-compat/wire/bytes): branchless padding computation
Change-Id: Ie07c2516a485c78afa6f9a3c8256e9708c4c42c5 Reviewed-on: https://cl.tvl.fyi/c/depot/+/11548 Tested-by: BuildkiteCI Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
		
							parent
							
								
									88f49c8351
								
							
						
					
					
						commit
						b3305ea6e2
					
				
					 1 changed files with 8 additions and 7 deletions
				
			
		| 
						 | 
				
			
			@ -119,14 +119,10 @@ pub async fn write_bytes<W: AsyncWriteExt + Unpin, B: AsRef<[u8]>>(
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
/// Computes the number of bytes we should add to len (a length in
 | 
			
		||||
/// bytes) to be alined on 64 bits (8 bytes).
 | 
			
		||||
/// bytes) to be aligned on 64 bits (8 bytes).
 | 
			
		||||
fn padding_len(len: u64) -> u8 {
 | 
			
		||||
    let modulo = len % 8;
 | 
			
		||||
    if modulo == 0 {
 | 
			
		||||
        0
 | 
			
		||||
    } else {
 | 
			
		||||
        8 - modulo as u8
 | 
			
		||||
    }
 | 
			
		||||
    let aligned = len.wrapping_add(7) & !7;
 | 
			
		||||
    aligned.wrapping_sub(len) as u8
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
| 
						 | 
				
			
			@ -229,4 +225,9 @@ mod tests {
 | 
			
		|||
            .build();
 | 
			
		||||
        assert_ok!(write_bytes(&mut mock, &input).await)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn padding_len_u64_max() {
 | 
			
		||||
        assert_eq!(padding_len(u64::MAX), 1);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue