[latex3-commits] [l3svn] r6913 - Add to l3trial input/output of floating points as bytes

noreply at latex-project.org noreply at latex-project.org
Mon Feb 13 20:05:41 CET 2017


Author: bruno
Date: 2017-02-13 20:05:41 +0100 (Mon, 13 Feb 2017)
New Revision: 6913

Added:
   trunk/l3trial/l3fp-extras/l3fp-interchange.dtx
Modified:
   trunk/l3trial/l3fp-extras/l3fp-extras.ins
Log:
Add to l3trial input/output of floating points as bytes

This is straightforward and required by the IEEE754 standard
(note that we are very far from adhering to it, but that's still
my long-term aim).


Modified: trunk/l3trial/l3fp-extras/l3fp-extras.ins
===================================================================
--- trunk/l3trial/l3fp-extras/l3fp-extras.ins	2017-02-13 15:25:54 UTC (rev 6912)
+++ trunk/l3trial/l3fp-extras/l3fp-extras.ins	2017-02-13 19:05:41 UTC (rev 6913)
@@ -43,6 +43,7 @@
         \from{l3fp-types.dtx}{package}%
         \from{l3fp-symbolic.dtx}{package}%
         \from{l3fp-functions.dtx}{package}%
+        \from{l3fp-interchange.dtx}{package}%
       }%
   }
 

Added: trunk/l3trial/l3fp-extras/l3fp-interchange.dtx
===================================================================
--- trunk/l3trial/l3fp-extras/l3fp-interchange.dtx	                        (rev 0)
+++ trunk/l3trial/l3fp-extras/l3fp-interchange.dtx	2017-02-13 19:05:41 UTC (rev 6913)
@@ -0,0 +1,312 @@
+% \iffalse
+%
+%% File l3fp-interchange.dtx (C) Copyright 2017 The LaTeX3 Project
+%%
+%% It may be distributed and/or modified under the conditions of the
+%% LaTeX Project Public License (LPPL), either version 1.3c of this
+%% license or (at your option) any later version.  The latest version
+%% of this license is in the file
+%%
+%%    http://www.latex-project.org/lppl.txt
+%%
+%% This file is part of the "l3trial bundle" (The Work in LPPL)
+%% and all files in that bundle must be distributed together.
+%%
+%% The released version of this bundle is available from CTAN.
+%%
+%% -----------------------------------------------------------------------
+%%
+%% The development version of the bundle can be found at
+%%
+%%    http://www.latex-project.org/svnroot/experimental/trunk/
+%%
+%% for those people who are interested.
+%%
+%%%%%%%%%%%
+%% NOTE: %%
+%%%%%%%%%%%
+%%
+%%   Snapshots taken from the repository represent work in progress and may
+%%   not work or may contain conflicting material!  We therefore ask
+%%   people _not_ to put them into distributions, archives, etc. without
+%%   prior consultation with the LaTeX Project Team.
+%%
+%% -----------------------------------------------------------------------
+%%
+%
+%<*driver|package>
+\RequirePackage{expl3}
+\GetIdInfo$Id$
+  {L3 Experimental floating point interchange}
+%</driver|package>
+%<*driver>
+\documentclass[full]{l3doc}
+\usepackage{amsmath}
+\begin{document}
+  \DocInput{\jobname.dtx}
+\end{document}
+%</driver>
+% \fi
+%
+% \title{^^A
+%   The \pkg{l3fp-interchange} package\\ Floating point interchange^^A
+%   \thanks{This file describes v\ExplFileVersion,
+%      last revised \ExplFileDate.}^^A
+% }
+%
+% \author{^^A
+%  The \LaTeX3 Project\thanks
+%    {^^A
+%      E-mail:
+%        \href{mailto:latex-team at latex-project.org}
+%          {latex-team at latex-project.org}^^A
+%    }^^A
+% }
+%
+% \date{Released \ExplFileDate}
+%
+% \maketitle
+%
+% \begin{documentation}
+%
+% \end{documentation}
+%
+% \begin{implementation}
+%
+% \section{\pkg{l3fp-interchange} implementation}
+%
+%    \begin{macrocode}
+%<*initex|package>
+%    \end{macrocode}
+%
+%    \begin{macrocode}
+%<@@=fp>
+%    \end{macrocode}
+%
+% This module provides functions to convert from \pkg{l3fp}'s encoding
+% of floating-points to the \textsc{ieee} standard's \texttt{decimal64}
+% encoding.
+%
+% The $64$~bits are manipulated here as $8$~bytes, namely character
+% tokens with character code in $[0,255]$.
+%
+% Currently the implementation is incomplete.  There is no effort to be
+% efficient: instead, the amount of code is minimized.
+%
+% There are actually two variants of that encoding, for now we focus on
+% the ``binary encoding for the significand''.  Below, we denote by
+% $b_0, b_1, \ldots{}, b_{63}$ the $64$ bits.  The (binary-significand)
+% \texttt{decimal64} format consists of a sign bit $S=b_0\in\{0,1\}$, an
+% exponent, and a significand, but the precise size of the exponent and
+% significand differ for different cases.
+% \begin{itemize}
+% \item If $b_1b_2b_3b_4b_5=11111$ then we get a quiet ($b_6=0$) or
+%   signalling ($b_6=1$) \texttt{NaN}, the bits $b_7\cdots b_{13}$ are
+%   ignored, and the payload is given by the bits $b_{14}\cdots b_{63}$,
+%   an integer in $[0,2^{50})$ where values greater or equal to
+%   $10^{15}$ are treated as zero payload;
+% \item If $b_1b_2b_3b_4b_5=11110$ then we get $(-1)^S\infty$ and other
+%   bits are ignored.
+% \item Otherwise, if $b_1b_2=11$ (but $b_3b_4\neq 11$) then the
+%   (biased) exponent is $E=b_3\cdots b_{12}$ and the significand is
+%   $100b_{13}\cdots b_{63}$, unless this exceeds $10^{16}$ in which
+%   case the significand is treated as zero.
+% \item Finally, if $b_1b_2\neq 11$ then the (biased) exponent is
+%   $E=b_1\cdots b_{10}$ and the significand is
+%   $b_{11}b_{12}b_{13}\cdots b_{63}$.
+% \end{itemize}
+% The encoding is canonical if all bits that are ignored in the
+% explanation above are set to zero.  When converting to
+% \texttt{decimal64} the encoding must be canonical.  Namely, since all
+% our \texttt{NaN} have zero payload and no sign,
+% \begin{itemize}
+% \item A \texttt{NaN} can be encoded as $011111$ followed by $0$
+%   (quiet) or $1$ (signalling) then $57$ zero bits.
+% \item An infinity can be encoded as $0$ (positive) or $1$ (negative)
+%   followed by $1111$ then $59$ zero bits.
+% \item A number whose significand is $c\in[2^{53},10^{16})$ can be
+%   encoded as its sign bit, then $1$ and $1$, then the biased exponent
+%   $E$ as a $10$-bit integer in $[0,767]$, then $51$-bits denoting
+%   $c-2^{53}$ (with leading zeros).
+% \item Finally, a number whose significand is $c\in[0,2^{53})$ can be
+%   encoded directly as its sign bit, its $10$-bit exponent, and its
+%   $53$-bit significand.
+% \end{itemize}
+%
+% \subsection{Inputting the interchange format}
+%
+% \begin{macro}[EXP,aux]{\@@_from_bytes_invalid:}
+%   ^^A todo: invalid operation
+%    \begin{macrocode}
+\cs_new:Npn \@@_from_bits_invalid: { \c_nan_fp }
+%    \end{macrocode}
+% \end{macro}
+%
+% \begin{macro}[EXP]{\fp_from_bytes:n}
+%   This reads a \texttt{decimal64} given as $8$ character tokens whose
+%   character code is in the range $[0,255]$ (\enquote{bytes}) and
+%   converts it to \pkg{l3fp} floating-points.  First test things a bit
+%    \begin{macrocode}
+\cs_new:Npn \fp_from_bytes:n #1
+  { \exp_args:Nf \@@_from_bytes:n { \__str_to_other:n {#1} } }
+\cs_new:Npn \@@_from_bytes:n #1
+  {
+    \int_compare:nNnTF { \str_count:n {#1} } = { 8 }
+      { \@@_from_bytes:Nw #1 \q_stop }
+      { \@@_from_bytes_invalid: }
+  }
+\cs_new:Npn \@@_from_bytes:Nw #1
+  {
+    \int_compare:nNnTF { `#1 } < { 128 }
+      { \exp_args:NNf \@@_from_bytes:Nnw + { \int_eval:n { `#1 } } }
+      { \exp_args:NNf \@@_from_bytes:Nnw - { \int_eval:n { `#1 - 128 } } }
+  }
+\cs_new:Npn \@@_from_bytes:Nnw #1#2
+  {
+    \int_compare:nNnTF { \int_div_truncate:nn {#2} {8} } = {15}
+      { \@@_from_bytes_special:Nnw }
+      { \@@_from_bytes_finite:Nnw }
+    #1 {#2}
+  }
+\cs_new:Npn \@@_from_bytes_special:Nnw #1#2#3 \q_stop
+  {
+    \int_case:nnF { \int_div_truncate:nn {#2} {2} }
+      {
+        {63} { \c_nan_fp } % ^^A todo signalling
+        {62} { \c_nan_fp } % ^^A todo quiet
+      }
+      { \token_if_eq_charcode:NNTF #1 + { \c_inf_fp } { \c_minus_inf_fp } }
+  }
+\cs_new:Npn \@@_from_bytes_finite:Nnw #1#2
+  {
+    \int_compare:nNnTF { \int_div_truncate:nn {#2} {32} } = {3}
+      { \@@_from_bytes_finite:NNnNNNNw 1 }
+      { \@@_from_bytes_finite:NNnNNNNw 4 }
+    #1 {#2}
+  }
+\cs_new:Npn \@@_from_bytes_finite:NNnNNNNw #1#2#3#4#5#6#7#8 \q_stop
+  {
+    \@@_parse:n
+      {
+        #2
+        \exp_args:Nf \@@_from_bytes_check:n
+          {
+            \fp_to_int:n
+              {
+                \token_if_eq_charcode:NNT #1 1 { 2^53 + }
+                \int_mod:nn { `#4 } { 8 * #1 } * 2^48
+                + \@@_from_bytes_aux:NNN #5#6#7 * 2^24
+                + \@@_from_bytes_aux:NNN #8
+              }
+          }
+        e
+        \int_eval:n
+          {
+            \int_mod:nn {#3} { 32 * #1 } * 32 / #1
+             + \int_div_truncate:nn { `#4 } { 8 * #1 }
+             - \c_@@_bias_int - 16
+          }
+      }
+  }
+\cs_new:Npn \@@_from_bytes_check:n #1
+  { \int_compare:nNnTF { \str_count:n {#1} } > {16} { 0 } {#1} }
+\cs_new:Npn \@@_from_bytes_aux:NNN #1#2#3
+  { \int_eval:n { (`#1 * 256 + `#2) * 256 + `#3 } }
+\int_const:Nn \c_@@_bias_int { 398 - 16 }
+%    \end{macrocode}
+% \end{macro}
+%
+% \subsection{Outputting the interchange format}
+%
+% \begin{macro}{\fp_to_bytes:n}
+%   ^^A oops, this generates bits instead of bytes!
+%    \begin{macrocode}
+\cs_new:Npn \fp_to_bytes:n #1
+  { \exp_last_unbraced:Nf \@@_to_bytes:w { \@@_parse:n {#1} } }
+\group_begin:
+  \char_set_catcode_other:N \^^00
+  \cs_new:Npn \@@_to_bytes:w \s_@@ \@@_chk:w #1#2#3 ;
+    {
+      \token_if_eq_charcode:NNTF #1 1
+        { \@@_to_bytes_normal:Nnnnnn #2 #3 }
+        {
+          \str_case:nn { #1#2 }
+            {
+              { 0 0 } { \token_to_str:N ^^00 }
+              { 0 2 } { \token_to_str:N ^^80 }
+              { 2 0 } { \token_to_str:N ^^78 }
+              { 2 2 } { \token_to_str:N ^^f8 }
+              { 3 1 } { \token_to_str:N ^^7c }
+            }
+          ^^00 ^^00 ^^00 ^^00 ^^00 ^^00 ^^00
+        }
+    }
+\group_end:
+\cs_new:Npn \@@_to_bytes_normal:Nnnnnn #1#2#3#4#5#6
+  {
+    \fp_compare:nTF { #3 #4 #5 #6 < 2^53 }
+      {
+        \@@_to_bytes_normal:nnnnNNn { #3 #4 } { #5 #6 }
+          { } {6} \c_false_bool
+      }
+      {
+        \int_compare:nNnTF { #5 #6 } < { 54740992 }
+          {
+            \@@_to_bytes_normal:ffffNNn
+              { \int_eval:n { #3 #4 - 90071992 - 1 } }
+              { \int_eval:n { #5 #6 + 45259008 } }
+          }
+          {
+            \@@_to_bytes_normal:ffffNNn
+              { \int_eval:n { #3 #4 - 90071992 } }
+              { \int_eval:n { #5 #6 - 54740992 } }
+          }
+        { } {6} \c_true_bool
+      }
+    #1 {#2}
+  }
+\cs_new:Npn \@@_to_bytes_normal:nnnnNNn #1#2#3#4#5#6#7
+  {
+    \int_compare:nNnTF {#4} = { 0 }
+      {
+        \exp_args:Nf \@@_to_bytes_end:n
+          {
+            \int_eval:n
+              {
+                \token_if_eq_charcode:NNT 2 #6 { 32768 + }
+                \bool_if:NT #5 { 24576 + }
+                ( #7 + \c_@@_bias_int ) * \bool_if:NTF #5 {8} {32}
+                + #2
+              }
+          }
+        #3
+      }
+      {
+        \@@_to_bytes_normal:ffffNNn
+          { \int_div_truncate:nn {#1} {256} }
+          {
+            \int_mod:nn {#1} {256} * 390625
+            + \int_div_truncate:nn {#2} {256}
+          }
+          { \char_generate:nn { \int_mod:nn {#2} {256} } {12} #3 }
+          { \int_eval:n { #4 - 1 } }
+        #5 #6 {#7}
+      }
+  }
+\cs_generate_variant:Nn \@@_to_bytes_normal:nnnnNNn { ffff }
+\cs_new:Npn \@@_to_bytes_end:n #1
+  {
+    \exp_last_unbraced:Nf \exp_last_unbraced:Nf
+      { \char_generate:nn { \int_div_truncate:nn {#1} {256} } {12} }
+      { \char_generate:nn { \int_mod:nn {#1} {256} } {12} }
+  }
+%    \end{macrocode}
+% \end{macro}
+%
+%    \begin{macrocode}
+%</initex|package>
+%    \end{macrocode}
+%
+% \end{implementation}
+%
+% \PrintIndex


Property changes on: trunk/l3trial/l3fp-extras/l3fp-interchange.dtx
___________________________________________________________________
Added: svn:eol-style
   + native
Added: svn:keywords
   + Id



More information about the latex3-commits mailing list