CBMC
boolbv_bswap.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Bit-blasting of bswap
4 
5 Author: Michael Tautschnig
6 
7 \*******************************************************************/
8 
9 #include "boolbv.h"
10 
11 #include <util/bitvector_expr.h>
12 
14 {
15  const std::size_t width = boolbv_width(expr.type());
16 
17  // width must be multiple of bytes
18  const std::size_t byte_bits = expr.get_bits_per_byte();
19  if(width % byte_bits != 0)
20  return conversion_failed(expr);
21 
22  bvt result = convert_bv(expr.op(), width);
23 
24  std::size_t dest_base = width;
25 
26  for(std::size_t src = 0; src < width; ++src)
27  {
28  std::size_t bit_offset = src % byte_bits;
29  if(bit_offset == 0)
30  dest_base -= byte_bits;
31 
32  if(src >= dest_base)
33  break;
34 
35  result[src].swap(result[dest_base + bit_offset]);
36  }
37 
38  return result;
39 }
API to expression classes for bitvectors.
virtual const bvt & convert_bv(const exprt &expr, const std::optional< std::size_t > expected_width={})
Convert expression to vector of literalts, using an internal cache to speed up conversion if availabl...
Definition: boolbv.cpp:37
virtual bvt convert_bswap(const bswap_exprt &expr)
bvt conversion_failed(const exprt &expr)
Print that the expression of x has failed conversion, then return a vector of x's width.
Definition: boolbv.cpp:83
virtual std::size_t boolbv_width(const typet &type) const
Definition: boolbv.h:101
The byte swap expression.
std::size_t get_bits_per_byte() const
typet & type()
Return the type of the expression.
Definition: expr.h:84
const exprt & op() const
Definition: std_expr.h:391
std::vector< literalt > bvt
Definition: literal.h:201