kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

conv_bitcast_f2i.c (760B)


      1 /* Bitcast: reinterpret a float's bits as an int via a union (same size, no
      2  * value conversion). The backend lowers this to an FMOV w,s (FP->GPR move) or
      3  * a store/reload, NOT an FCVT. volatile defeats folding.
      4  *
      5  *   2.0f has IEEE-754 bits 0x40000000. We mask off pieces so the final result
      6  *   is exactly 42 regardless of which bit pattern materializes. */
      7 union fb {
      8   float f;
      9   unsigned int u;
     10 };
     11 int test_main(void) {
     12   volatile float src = 2.0f; /* bits 0x40000000 */
     13   union fb x;
     14   x.f = src;               /* reinterpret as bits, fmov w,s */
     15   unsigned int bits = x.u; /* 0x40000000 */
     16   /* top byte is 0x40 = 64; bottom 24 bits are 0. 64 - 22 = 42. */
     17   unsigned int top = bits >> 24; /* 0x40 = 64 */
     18   return (int)(top - 22);        /* 42 */
     19 }