
    4)di                         d Z ddlZddlZddlZddlZddlmZ ddlZ G d d          Z edd          Z	 ed	d
          Z
d ZdS )z
The 8-bit float formats used here are from a proposal supported by Graphcore, AMD and Qualcomm.
See https://arxiv.org/abs/2206.02915

    N)binary8_luts_compressedc                   V    e Zd ZdZdedefdZd Zd Zd Zde	d	efd
Z
d	efdZd ZdS )Binary8Formatz88-bit floating point formats based on draft IEEE binary8exp_bitsbiasc                 >    || _         || _        d| _        d| _        d S )N      )r   r   pos_clamp_valueneg_clamp_value)selfr   r   s      T/var/www/menimich/repos/protonApp/venv/lib/python3.11/site-packages/bitstring/fp8.py__init__zBinary8Format.__init__   s&     	))    c                 (    d| j          d| j         dS )NzBinary8Format(exp_bits=z, bias=)r   r   r   s    r   __str__zBinary8Format.__str__   s    KKKtyKKKKr   c                     t           | j        | j        f         \  }}t          j        |          | _        t          j        |          }t          j        dt          |          dz   d|          | _	        d S )N<   f)
r   r   r   zlib
decompresslut_float16_to_binary8structunpacklenlut_binary8_to_float)r   binary8_to_float_compressedfloat16_to_binary8_compresseddecs       r   decompress_lutszBinary8Format.decompress_luts   ss    E\^b^kmqmv]wExB#%B&*o6S&T&T#o9::$*M2Fc#hh!m2F2F2F$L$L!!!r   c                 j    |                                  | _        |                                 | _        d S N)createLUT_for_binary8_to_floatr     createLUT_for_float16_to_binary8r   r   s    r   create_lutszBinary8Format.create_luts!   s0    $($G$G$I$I!&*&K&K&M&M###r   r   returnc                     	 t          j        d|          }n0# t          t           j        f$ r |dk    r| j        n| j        cY S w xY wt                              |d          }| j        |         S )zYGiven a Python float convert to the best float8 (expressed as an integer in 0-255 range).>er   big)	byteorder)	r   packOverflowErrorerrorr   r   int
from_bytesr   )r   r   bf16_ints       r   float_to_int8zBinary8Format.float_to_int8%   s    	KD!$$AAv|, 	K 	K 	K+,q554''d6JJJJ	K ..e.44*733s    *AAc                    dd l }|j                            d| j        z
            }t	          d          }t          d          D ]z}t          j        d|          }t          j        d|          \  }|	                    ||          }t          j        |          rd}n| j                            |          }|||<   {t          |          S )Nr      i   z>Hr,      )gfloatformatsformat_info_p3109r   	bytearrayranger   r/   r   round_floatmathisnanr    indexbytes)	r   r:   fifp16_to_fp8ir4   r   fpfp8_is	            r   r(   z.Binary8Format.createLUT_for_float16_to_binary81   s    ^--a$-.?@@((w 	# 	#AD!$$AtQ''BA##B**Bz"~~ <"177;;"KNN[!!!r   c                    g }t          d          D ]}t          j                            |ddd          }|d         }t          j                            |dd| j        z                      }|d| j        z   d         }|dk    r#t          j        d	          |z   }| j         dz   }n!t          j        d
          |z   }|| j        z  }t          t          j                            |                    dd| j        z
  z  z  }|d|z  z  }|                    |s|n|            t          d          |d<   t          d          |d<   t          d          |d<   t          j	        d|          S )zWCreate a LUT to convert an int in range 0-255 representing a float8 into a Python float   r8   r-   F)lengthendiansignedr      N01g       @      nanr9   infr	   z-infr
   r   )
r>   bitarrayutilint2baba2intr   r   floatappendarray)r   i2frF   r4   signexponentsignificandr   s           r   r'   z,Binary8Format.createLUT_for_binary8_to_floatA   si   s 	. 	.A$$Qqu$MMAQ4D}++AaDM0A.A,BCCHA-../K1}}&/44{B I:>&/44{BDI%hm**;7788CADU<VWAhAJJ,qq1"----,,J,,J--J{3$$$r   N)__name__
__module____qualname____doc__r2   r   r   r$   r)   rY   r6   rC   r(   r'    r   r   r   r      s        BB* *C * * * *L L LM M MN N N
4u 
4 
4 
4 
4 
4"% " " " " % % % % %r   r   r   r8   r         c                  j    t                                            t                                           d S r&   )p4binary_fmtr$   p3binary_fmtrd   r   r   r$   r$   _   s,      """  """""r   )rc   r   r   r[   rU   bitstring.lutsr   r@   r   rh   ri   r$   rd   r   r   <module>rk      s         2 2 2 2 2 2 H% H% H% H% H% H% H% H%X }aa000}ab111# # # # #r   