/* ieee754.pk - IEEE Standard for Floating-Point Arithmetic.  */

/* Copyright (C) 2022, 2023, 2024, 2025 The poke authors.  */

/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Reference for conversions:
//   http://www.fox-toolkit.org/ftp/fasthalffloatconversion.pdf

type IEEE754_binary16 =
  struct int<16>
  {
    uint<1> sign;
    uint<5> exp;
    uint<10> frac; // aka significand, aka mantissa.

    method is_nan_p  = int<32>: { return exp == 0x1f && frac != 0; }
    method is_qnan_p = int<32>: { return exp == 0x1f && (frac & 0x200U) != 0; }
    method is_snan_p = int<32>: { return exp == 0x1f && (frac & 0x200U) == 0; }
    method is_inf_p  = int<32>: { return exp == 0x1f && frac == 0; }
    method is_zero_p = int<32>: { return exp == 0 && frac == 0; }
    method is_subnormal_p = int<32>: { return exp == 0 && frac != 0; }
  };

type IEEE754_binary32 =
  struct int<32>
  {
    uint<1> sign;
    uint<8> exp;
    uint<23> frac; // aka significand, aka mantissa.

    method is_nan_p  = int<32>: { return exp == 0xff && frac != 0; }
    method is_qnan_p = int<32>: { return exp == 0xff && (frac & 0x400000U) != 0; }
    method is_snan_p = int<32>: { return exp == 0xff && (frac & 0x400000U) == 0; }
    method is_inf_p  = int<32>: { return exp == 0xff && frac == 0; }
    method is_zero_p = int<32>: { return exp == 0 && frac == 0; }
    method is_subnormal_p = int<32>: { return exp == 0 && frac != 0; }

    method _print = void: { printf ("#<%f32d>", sign:::exp:::frac); }
  };

type IEEE754_binary64 =
  struct int<64>
  {
    uint<1> sign;
    uint<11> exp;
    uint<52> frac; // aka significand, aka mantissa.

    method is_nan_p  = int<32>: { return exp == 0x7ff && frac != 0; }
    method is_qnan_p = int<32>:
      { return exp == 0x7ff && (frac & 0x8000000000000UL) != 0; }
    method is_snan_p = int<32>:
      { return exp == 0x7ff && (frac & 0x8000000000000UL) == 0; }
    method is_inf_p  = int<32>: { return exp == 0x7ff && frac == 0; }
    method is_zero_p = int<32>: { return exp == 0 && frac == 0; }
    method is_subnormal_p = int<32>: { return exp == 0 && frac != 0; }

    method _print = void: { printf ("#<%f64d>", sign:::exp:::frac); }
  };

type Float16 = IEEE754_binary16;
type Float32 = IEEE754_binary32;
type Float64 = IEEE754_binary64;

fun ieee754_16to32 = (IEEE754_binary16 f16) IEEE754_binary32:
{
  if (f16.is_zero_p)
    return IEEE754_binary32 {sign=f16.sign};
  if (f16.is_inf_p)
    return IEEE754_binary32 {sign=f16.sign, exp=-1};
  if (f16.is_nan_p)
    return IEEE754_binary32 {
      sign=f16.sign, exp=-1, frac=f16.frac:::(0 as uint<13>),
    };
  if (f16.is_subnormal_p)
    {
      var f32 = IEEE754_binary32 {sign=f16.sign, exp=0-15+127},
          frac = f16.frac as int<11>;

      // Normalize
      while (1)
        {
          frac = frac <<. 1;
          if (frac < 0)
            break;
          --f32.exp;
        }
      f32.frac = (frac as uint<10>):::(0 as uint<13>);

      return f32;
    }
  return IEEE754_binary32 {
    sign=f16.sign,
    exp=f16.exp - 15 + 127, // Change the bias from 15 to 127.
    frac=f16.frac:::(0 as uint<13>),
  };
}

fun ieee754_32to64 = (IEEE754_binary32 f32) IEEE754_binary64:
{
  if (f32.is_zero_p)
    return IEEE754_binary64 {sign=f32.sign};
  if (f32.is_inf_p)
    return IEEE754_binary64 {sign=f32.sign, exp=-1};
  if (f32.is_nan_p)
    return IEEE754_binary64 {
      sign=f32.sign, exp=-1, frac=f32.frac:::(0 as uint<29>),
    };
  if (f32.is_subnormal_p)
    {
      var f64 = IEEE754_binary64 {sign=f32.sign, exp=0-127+1023},
          frac = f32.frac as int<24>;

      // Normalize
      while (1)
        {
          frac = frac <<. 1;
          if (frac < 0)
            break;
          --f64.exp;
        }
      f64.frac = (frac as uint<23>):::(0 as uint<29>);

      return f64;
    }
  return IEEE754_binary64 {
    sign=f32.sign,
    exp=f32.exp - 127 + 1023, // Change the bias from 127 to 1023.
    frac=f32.frac:::(0 as uint<29>),
  };
}
