0

I need to turn the result of a comparison into an i32.

The clang compiler with optimizations seems to favor z extend. What do the z extend or s extend instructions do, and especially how are they different to cast?

Which should I prefer?

jzimmerman
  • 74
  • 1
  • 6

1 Answers1

2

Casting reinterprets a bit pattern as another type. The two types have to have exactly the same size. It's not terribly useful, really.

Zero extension takes an integer (such as i32), tacks on zeroes in front and produces an integer of another type (such as i64), ie. it changes the type of an unsigned integer while preserving the value. It's really useful for reading bitfields into a regular uint, for promoting integers as part of arithmetic expressions or function calls, and much else. These are things clang has to do all the time.

You also have sign extension, which does the same thing and is useful in many of the same cases, except that it changes the type of a signed integer while preserving the value.

arnt
  • 8,949
  • 5
  • 24
  • 32