code: def float32_to_float8e5m2(  # noqa: PLR0911
      fval: float,
      scale: float = 1.0,
      fn: bool = False,
      uz: bool = False,
      saturate: bool = True,
  ) -> int:
      """Convert a float32 value to a float8, e5m2 (as int).
  
      Args:
          fval: float to convert
          scale: scale, divide *fval* by *scale* before casting it
          fn: no infinite values
          uz: no negative zero
          saturate: if True, any value out of range included inf becomes
              the maximum value, otherwise, it becomes NaN. The
              description of operator Cast fully describes the
              differences.
  
      Returns:
          converted float
      """
      x = fval / scale
      b = int.from_bytes(struct.pack("<f", np.float32(x)), "little")
      ret = (b & 0x80000000) >> 24  # sign
  
      if fn and uz:
          if (b & 0x7FC00000) == 0x7FC00000:  # noqa: PLR2004
              return 0x80
          if (b & 0x7FFFFFFF) == 0x7F800000:  # noqa: PLR2004
              # inf
              if saturate:
                  return ret | 0x7F
              return 0x80
          e = (b & 0x7F800000) >> 23  # exponent
          m = b & 0x007FFFFF  # mantissa
  
          if e < 109:  # noqa: PLR2004
              ret = 0
          elif e < 112:  # noqa: PLR2004
              # denormalized number
              ex = e - 111
              if ex >= -1:
                  ret |= 1 << (1 + ex)
                  ret |= m >> (22 - ex)
              elif m > 0:
                  ret |= 1
              else:
                  ret = 0
              mask = 1 << (21 - ex)
              if m & mask and (
                  ret & 1
                  or m & (mask - 1) > 0
                  or (
                      m & mask
                      and m & (mask << 1)
                      and m & (mask - 1) == 0
                  )
              ):
                  # rounding
                  ret += 1
          elif e < 143:  # noqa: PLR2004
              # normalized number
              ex = e - 111
              ret |= ex << 2
              ret |= m >> 21
              if m & 0x100000 and ((m & 0xFFFFF) or (m & 0x200000)):
                  if (ret & 0x7F) < 0x7F:  # noqa: PLR2004
                      # rounding
                      ret += 1
                  elif not saturate:
                      ret = 0x80
          elif e == 255 and m == 0:  # inf  # noqa: PLR2004
              ret = 0x80
          elif saturate:
              ret |= 0x7F  # last possible number
          else:
              ret = 0x80
          return int(ret)
      elif not fn and not uz:
          if (b & 0x7FC00000) == 0x7FC00000:  # noqa: PLR2004
              return 0x7F | ret
          if np.isinf(x):
              if saturate:
                  return 0x7B | ret
              return 0x7C | ret
          e = (b & 0x7F800000) >> 23  # exponent
          m = b & 0x007FFFFF  # mantissa
  
          if e != 0:
              if e < 110:  # noqa: PLR2004
                  pass
              elif e < 113:  # noqa: PLR2004
                  # denormalized number
                  ex = e - 112
                  if ex >= -1:
                      ret |= 1 << (1 + ex)
                      ret |= m >> (22 - ex)
                  elif m > 0:
                      ret |= 1
                  mask = 1 << (21 - ex)
                  if m & mask and (
                      ret & 1
                      or m & (mask - 1) > 0
                      or (
                          m & mask
                          and m & (mask << 1)
                          and m & (mask - 1) == 0
                      )
                  ):
                      # rounding
                      ret += 1
              elif e < 143:  # noqa: PLR2004
                  # normalized number
                  ex = e - 112
                  ret |= ex << 2
                  ret |= m >> 21
                  if m & 0x100000 and ((m & 0xFFFFF) or (m & 0x200000)):
                      if (ret & 0x7F) < 0x7B:  # noqa: PLR2004
                          # rounding
                          ret += 1
                      elif saturate:
                          ret |= 0x7B
                      else:
                          ret |= 0x7C
              elif saturate:
                  ret |= 0x7B
              else:
                  ret |= 0x7C
          return int(ret)
      else:
          raise NotImplementedError(
              "fn and uz must be both False or True."
          )
  
imports: import collections.abc
  import numbers
  import struct
  from cmath import isnan
  from typing import Any, Callable, Dict, KeysView, List, Optional, Sequence, Tuple, TypeVar, Union, cast
  import google.protobuf.message
  import numpy
  from onnx import IR_VERSION, AttributeProto, FunctionProto, GraphProto, MapProto, ModelProto, NodeProto, OperatorSetIdProto, OptionalProto, SequenceProto, SparseTensorProto, TensorProto, TensorShapeProto, TrainingInfoProto, TypeProto, ValueInfoProto, defs, mapping, subbyte
label_desc: Use image node_id 14 for calling a global function with example usage: float32_to_float8e5m2(fval, scale, fn, uz, saturate) and returns: int, int, int, int, unknown, unknown, unknown, unknown