17ec681f3Smrg/*
27ec681f3Smrg * Copyright (C) 2021 Alyssa Rosenzweig <alyssa@rosenzweig.io>
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
207ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
217ec681f3Smrg * SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#ifndef __AGX_MINIFLOAT_H_
257ec681f3Smrg#define __AGX_MINIFLOAT_H_
267ec681f3Smrg
277ec681f3Smrg#include <math.h>
287ec681f3Smrg#include "util/macros.h"
297ec681f3Smrg
307ec681f3Smrg/* AGX includes an 8-bit floating-point format for small dyadic immediates,
317ec681f3Smrg * consisting of 3 bits for the exponent, 4 bits for the mantissa, and 1-bit
327ec681f3Smrg * for sign, in the usual order. Zero exponent has special handling. */
337ec681f3Smrg
347ec681f3Smrgstatic inline float
357ec681f3Smrgagx_minifloat_decode(uint8_t imm)
367ec681f3Smrg{
377ec681f3Smrg   float sign = (imm & 0x80) ? -1.0 : 1.0;
387ec681f3Smrg   signed exp = (imm & 0x70) >> 4;
397ec681f3Smrg   unsigned mantissa = (imm & 0xF);
407ec681f3Smrg
417ec681f3Smrg   if (exp)
427ec681f3Smrg      return ldexpf(sign * (float) (mantissa | 0x10), exp - 7);
437ec681f3Smrg   else
447ec681f3Smrg      return ldexpf(sign * ((float) mantissa), -6);
457ec681f3Smrg}
467ec681f3Smrg
477ec681f3Smrg/* Encodes a float. Results are only valid if the float can be represented
487ec681f3Smrg * exactly, if not the result of this function is UNDEFINED. signbit() is used
497ec681f3Smrg * to ensure -0.0 is handled correctly. */
507ec681f3Smrg
517ec681f3Smrgstatic inline uint8_t
527ec681f3Smrgagx_minifloat_encode(float f)
537ec681f3Smrg{
547ec681f3Smrg   unsigned sign = signbit(f) ? 0x80 : 0;
557ec681f3Smrg   f = fabsf(f);
567ec681f3Smrg
577ec681f3Smrg   /* frac is in [0.5, 1) and f = frac * 2^exp */
587ec681f3Smrg   int exp = 0;
597ec681f3Smrg   float frac = frexpf(f, &exp);
607ec681f3Smrg
617ec681f3Smrg   if (f >= 0.25) {
627ec681f3Smrg      unsigned mantissa = (frac * 32.0);
637ec681f3Smrg      exp -= 5; /* 2^5 = 32 */
647ec681f3Smrg      exp = CLAMP(exp + 7, 0, 7);
657ec681f3Smrg
667ec681f3Smrg      assert(mantissa >= 0x10 && mantissa < 0x20);
677ec681f3Smrg      assert(exp >= 1);
687ec681f3Smrg
697ec681f3Smrg      return sign | (exp << 4) | (mantissa & 0xF);
707ec681f3Smrg   } else {
717ec681f3Smrg      unsigned mantissa = (f * 64.0f);
727ec681f3Smrg      assert(mantissa < 0x10);
737ec681f3Smrg
747ec681f3Smrg      return sign | mantissa;
757ec681f3Smrg   }
767ec681f3Smrg}
777ec681f3Smrg
787ec681f3Smrgstatic inline bool
797ec681f3Smrgagx_minifloat_exact(float f)
807ec681f3Smrg{
817ec681f3Smrg   float f_ = agx_minifloat_decode(agx_minifloat_encode(f));
827ec681f3Smrg   return memcmp(&f, &f_, sizeof(float)) == 0;
837ec681f3Smrg}
847ec681f3Smrg
857ec681f3Smrg#ifndef NDEBUG
867ec681f3Smrgstatic inline void
877ec681f3Smrgagx_minifloat_tests(void)
887ec681f3Smrg{
897ec681f3Smrg   /* Decode some representative values */
907ec681f3Smrg   assert(agx_minifloat_decode(0) == 0.0f);
917ec681f3Smrg   assert(agx_minifloat_decode(25) == 0.390625f);
927ec681f3Smrg   assert(agx_minifloat_decode(135) == -0.109375f);
937ec681f3Smrg   assert(agx_minifloat_decode(255) == -31.0);
947ec681f3Smrg
957ec681f3Smrg   /* Verify exactness */
967ec681f3Smrg   assert(agx_minifloat_exact(0.0f));
977ec681f3Smrg   assert(agx_minifloat_exact(0.390625f));
987ec681f3Smrg   assert(agx_minifloat_exact(-0.109375f));
997ec681f3Smrg   assert(agx_minifloat_exact(-31.0));
1007ec681f3Smrg   assert(!agx_minifloat_exact(3.141f));
1017ec681f3Smrg   assert(!agx_minifloat_exact(2.718f));
1027ec681f3Smrg   assert(!agx_minifloat_exact(1.618f));
1037ec681f3Smrg
1047ec681f3Smrg   /* Check that all values round trip */
1057ec681f3Smrg   for (unsigned i = 0; i < 0x100; ++i) {
1067ec681f3Smrg      float f = agx_minifloat_decode(i);
1077ec681f3Smrg      assert(agx_minifloat_encode(f) == i);
1087ec681f3Smrg      assert(agx_minifloat_exact(f));
1097ec681f3Smrg   }
1107ec681f3Smrg}
1117ec681f3Smrg#endif
1127ec681f3Smrg
1137ec681f3Smrg#endif
114