1

Could anyone please explain me the operation of ada.unchecked_coversion(float, UnsignedInteger). What this operation will be performed ? Could anyone please clarify this with example. Thanks a lot in advance.

2 Answers2

6

From here

An unchecked conversion is a bit-for-bit copy without regard to the meanings attached to those bits and bit positions by either the source or the destination type. The source bit pattern can easily be meaningless in the context of the destination type. Unchecked conversions can create values that violate type constraints on subsequent operations. Unchecked conversion of objects mismatched in size has implementation-dependent results.

So it should be used with care and only for types that have "same" bit representation. e.g it wouldn't probably work to cast float to int as those types could be represented differently in memory.

Here are two variables

  • A: source float designated by address 0x249fd8c with value 1.0
  • B: target Integer designated by address 0x249fd88 (unitialized)

before conversion

enter image description here

and after calling unchecked conversion

enter image description here

As you can see whole operation is just bit by bit copy of source variable to target variable

Hope this answers your questions

Timur Samkharadze
  • 1,737
  • 1
  • 17
  • 33
2

Normally an unchecked conversion has no effect at run time. It does not result in any code being generated. It is just permission to the compiler to interpret the bytes of the source value as the representation of a value of the target type. Most importantly, it does not cause a copy.

Jeffrey R. Carter
  • 3,033
  • 9
  • 10
  • Seems like at least GNAT does generate "movl" instruction that copy value to another variable, like [this](https://ibb.co/fXkMfNb) or I'm interpreting it wrong? – Timur Samkharadze Jan 19 '23 at 13:50
  • 2
    An unchecked conversion does not copy a value to another variable. You are assigning the result of the unchecked conversion to a variable. The assignment causes the copy, not the unchecked conversion. – Jeffrey R. Carter Jan 20 '23 at 08:11