1 Comparison between binary and decimal floating-point numbers Nicolas Brisebarre, Christoph Lauter, Marc Mezzarobba, and Jean-Michel Muller Abstract—We introduce an algorithm to compare a binary floating-point (FP) number and a decimal FP number, assuming the “binary encoding” of the decimal formats is used, and with a special emphasis on the basic interchange formats specified by the IEEE 754-2008 standard for FP arithmetic. It is a two-step algorithm: a first pass, based on the exponents only, quickly eliminates most cases, then, when the first pass does not suffice, a more accurate second pass is performed. We provide an implementation of several variants of our algorithm, and compare them. F 1 I NTRODUCTION precision (bits) πmin πmax binary32 binary64 24 −126 +127 53 −1022 +1023 binary128 113 The IEEE 754-2008 Standard for Floating-Point Arith−16382 metic [5] specifies binary (radix-2) and decimal (radix-10) +16383 floating-point number formats for a variety of precisions. The so-called “basic interchange formats” are presented decimal64 decimal128 in Table 1. precision (digits) 16 34 The Standard neither requires nor forbids comparisons πmin −383 −6143 between floating-point numbers of different radices (it πmax +384 +6144 states that floating-point data represented in different formats shall be comparable as long as the operands’ formats have the TABLE 1 same radix). However, such “mixed-radix” comparisons The basic binary and decimal interchange formats may offer several advantages. It is not infrequent to read specified by the IEEE 754-2008 Standard. decimal data from a database and to have to compare it to some binary floating-point number. The comparison may be inaccurate if the decimal number is preliminarily converted to binary, or, respectively, if the binary number decimal followed by a decimal comparison. The compiler is first converted to decimal. emits no warning that the boolean result might not be Consider for instance the following C code: the expected one because of rounding. This kind of strategy may lead to inconsistencies. double x = ...; Consider such a “naïve” approach built as follows: _Decimal64 y = ...; when comparing a binary floating-point number π₯2 of if (x <= y) format β±2 , and a decimal floating-point number π₯10 of ... The standardization of decimal floating-point arithmetic format β±10 , we first convert π₯10 to the binary format β±2 in C [6] is still at draft stage, and compilers supporting (that is, we replace it by the β±2 number nearest π₯10 ), and decimal floating-point arithmetic handle code sequences then we perform the comparison in binary. Denote the <, β 6 , β, > and β. > such as the previous one at their discretion and often comparison operators so defined as β Consider the following variables (all exactly represented in an unsatisfactory way. As it occurs, Intel’s icc 12.1.3 translates this sequence into a conversion from binary to in their respective formats): 55 β π₯ = 3602879701896397/2 , declared as a binary64 number; β N. Brisebarre and J.-M. Muller are with CNRS, Laboratoire LIP, ENS 27 Lyon, INRIA, Université Claude Bernard Lyon 1, Lyon, France. β π¦ = 13421773/2 , declared as a binary32 number; E-mail: nicolas.brisebarre@ens-lyon.fr, jean-michel.muller@ens-lyon.fr β π§ = 1/10, declared as a decimal64 number. β C. Lauter is with Sorbonne Universités, UPMC Univ Paris 06, UMR 7606, LIP6, F-75005, Paris, France. E-mail: christoph.lauter@lip6.fr Then it holds that π₯ β < π¦, but also π¦ β 6 π§ and π§ β 6 π₯. β This work was initiated while M. Mezzarobba was with the Research Such an inconsistent result might for instance suffice to Institute for Symbolic Computation, Johannes Kepler University, Linz, prevent a sorting program from terminating. Austria. M. Mezzarobba is now with CNRS, UMR 7606, LIP6, F-75005, Paris, France; Sorbonne Universités, UPMC Univ Paris 06, UMR 7606, Remark that in principle, π₯2 and π₯10 could both be LIP6, F-75005, Paris, France. E-mail: marc@mezzarobba.net converted to some longer format in such a way that β This work was partly supported by the TaMaDi project of the French the naïve method yields a correct answer. However, that Agence Nationale de la Recherche, and by the Austria Science Fund (FWF) grants P22748-N18 and Y464-N18. method requires a correctly rounded radix conversion, which is an intrinsically more expensive operation than 2 where π2 , π10 , π2 and π10 are integers that satisfy: the comparison itself. An experienced programer is likely to explicitly convert πmin − π2 + 1 6 π2 6 πmax , 2 2 all variables to a larger format. However we believe that min max π10 6 π10 6 π10 , ultimately the task of making sure that the comparisons (1) π2 −1 are consistent and correctly performed should be left to 2 6 π2 6 2π2 − 1, the compiler, and that the only fully consistent way of 1 6 π10 6 10π10 − 1 comparing two numeric variables π₯ and π¦ of a different type is to effectively compare π₯ and π¦, and not to compare with π2 , π10 > 1. (The choice of lower bound for π2 makes an approximation to π₯ to an approximation to π¦. the condition 2π2 −1 6 π2 hold even for subnormal binary In the following, we describe algorithms to perform numbers.) such “exact” comparisons between binary numbers in We assume that the so-called binary encoding (BID) [5], any of the basic binary interchange formats and decimal [8] of IEEE 754-2008 is used for the decimal format1 , so numbers in any of the basic decimal interchange formats. that the integer π10 is easily accessible in binary. Denote Our algorithms were developed with a software imple- by mentation in mind. They end up being pretty generic, but π′10 = ⌈π10 log2 10⌉ , we make no claim as to their applicability to hardware the number of bits that are necessary for representing implementations. It is natural to require that mixed-radix comparisons the decimal significands in binary. When π₯2 and π₯10 have significantly different orders not signal any floating-point exception, except in situations that parallel exceptional conditions specified in the of magnitude, examining their exponents will suffice Standard [5, Section 5.11] for regular comparisons. For to compare them. Hence, we first perform a simple this reason, we avoid floating-point operations that might exponent-based test (Section 3). When this does not sufsignal exceptions, and mostly use integer arithmetic to fice, a second step compares the significands multiplied by suitable powers of 2 and 5. We compute “worst cases” implement our comparisons. that determine how accurate the second step needs to Our method is based on two steps: be (Section 4), and show how to implement it efficiently β Algorithm 1 tries to compare numbers just by (Section 5). Section 6 is an aside describing a simpler examining their floating-point exponents; algorithm that can be used if we only want to decide β when Algorithm 1 is inconclusive, Algorithm 2 whether π₯2 and π₯10 are equal. Finally, Section 7 discusses provides the answer. our implementation of the comparison algorithms and Algorithm 2 admits many variants and depends on a presents experimental results. number of parameters. Tables 7 and 8 suggest suitable parameters for most typical use cases. Implementing the comparison for a given pair of 3 F IRST STEP : ELIMINATING THE “ SIMPLE formats just requires the implementation of Algorithms CASES ” BY EXAMINING THE EXPONENTS 1 and 2. The analysis of the algorithms as a function of all 3.1 Normalization parameters, as presented in Sections 4 to 6, is somewhat As we have technical. These details can be skipped at first reading. 1 6 π10 6 10π10 − 1, This paper is an extended version of the article [2]. there exists a unique π ∈ {0, 1, 2, . . . , π′10 − 1} such that 2 S ETTING AND O UTLINE ′ ′ 2π10 −1 6 2π π10 6 2π10 − 1. We consider a binary format of precision π2 , minimum Our initial problem of comparing π₯2 and π₯10 reduces to exponent πmin and maximum exponent πmax , and a comparing π2 · 2π2 −π2 +1+π and (2π π10 ) · 10π10 −π10 +1 . 2 2 decimal format of precision π10 , minimum exponent πmin The fact that we “normalize” the decimal significand 10 and maximum exponent πmax π10 by a binary shift between two consecutive powers 10 . We want to compare a binary floating-point number π₯2 and a decimal floating- of two is of course questionable; π10 could also be point number π₯10 . normalized into the range 10π10 −1 6 10π‘ · π10 6 10π10 − 1. Without loss of generality we assume π₯2 > 0 and However, hardware support for this operation [11] is not π₯10 > 0 (when π₯2 and π₯10 are negative, the problem widespread. A decimal normalization would thus require reduces to comparing −π₯2 and −π₯10 , and when they a loop, provoking pipeline stalls, whereas the proposed have different signs the comparison is straightforward). binary normalization can exploit an existing hardware The floating-point representations of π₯2 and π₯10 are leading-zero counter with straight-line code. π₯2 = π2 · 2π2 −π2 +1 , π₯10 = π10 · 10π10 −π10 +1 , 1. This encoding is typically used in software implementations of decimal floating-point arithmetic, even if BID-based hardware designs have been proposed [11]. 3 b32/d64 b32/d128 b64/d64 b64/d128 b128/d64 b128/d128 24 16 54 29 24 34 113 88 53 16 54 0 53 34 113 59 113 16 54 −60 113 34 113 −1 −570, 526 18 int32 −6371, 6304 23 int64 −1495, 1422 19 int32 −7296, 7200 23 int64 −16915, 16782 27 int64 −22716, 22560 27 int64 π2 π10 π′10 π€ (1) (1) βmin , βmax π min datatype TABLE 2 The various parameters involved in Step 1 of the comparison algorithm. 3.2 Comparing the Exponents That range is given in Table 2 for the basic IEEE formats. Knowing that range, it is easy to implement π as follows. Define β§ π = π2 , βͺ βͺ βͺ βͺ ′ βͺ βͺ β¨ β = π2 − π10 + π + π10 − π10 + 1, π = π10 · 2π , βͺ βͺ βͺ π = π10 − π10 + 1, βͺ βͺ βͺ β© π€ = π′10 − π2 − 1 so that {οΈ π (2) 2 π₯2 = π · 2 , π Our comparison problem becomes: Compare π · 2β+π€ with π · 5π . We have πmin = 2π2 −1 6 π 6 πmax = 2π2 − 1, ′ ′ πmin = 2π10 −1 6 π 6 πmax = 2π10 − 1. (3) It is clear that πmin · 2β+π€ > πmax · 5π implies π₯2 > π₯10 , while πmax · 2β+π€ < πmin · 5π implies π₯2 < π₯10 . This gives −π′10 π β−2 )·5 <2 ⇒ π₯10 < π₯2 , −π2 β π ⇒ π₯2 < π₯10 . (1 − 2 )·2 <5 (4) In order to compare π₯2 and π₯10 based on these implications, define π(β) = ⌊β · log5 2⌋. Proposition 1. We have π < π(β) ⇒ π₯2 > π₯10 , π > π(β) ⇒ π₯2 < π₯10 . Proof: If π < π(β) then π 6 π(β) − 1, hence π 6 β log5 2 − 1. This implies that 5π 6 (1/5) · 2β < 2β /(4 · ′ (1 − 2−π10 )), therefore, from (4), π₯10 < π₯2 . If π > π(β) then π > π(β) + 1, hence π > β log5 2, so that 5π > 2β > (1 − 2−π2 ) · 2β . This implies, from (4), π₯2 < π₯10 . Now consider the range of β: by (2), β lies between (1) ′ βmin = (πmin − π2 + 1) − πmax 2 10 + π10 − π10 + 1 and (1) βmax = πmax − πmin 2 10 + π10 . with πΏ = ⌊2π log5 2⌉, (1) 2 π₯10 = π · 10 . (1 − 2 π(β) ^ = ⌊πΏ · β · 2−π ⌋, (1) satisfies π(β) = π(β) ^ for all β in the range [βmin , βmax ]. β+π+π€ π Proposition 2. Denote by ⌊·⌉ the nearest integer function. For large enough π ∈ N, the function defined by Proposition 2 is an immediate consequence of the irrationality of log5 2. For known, moderate values of (1) (1) βmin and βmax , the optimal choice π min of π is easy to find and small. For instance, if the binary format is binary64 and the decimal format is decimal64, then π min = 19. Table 2 gives the value of π min for the basic IEEE formats. The product πΏ · β, for β in the indicated range and π = π min , can be computed exactly in (signed or unsigned) integer arithmetic, with the indicated data type. Computing ⌊π · 2−π½ ⌋ of course reduces to a right-shift by π½ bits. Propositions 1 and 2 yield to the following algorithm. Algorithm 1. First, exponent-based step 1 compute β = π2 − π10 + π + π10 − π′10 + 1 and π = π10 − π10 + 1; 2 with the appropriate value of π , compute π(β) = ⌊πΏ · β · 2−π ⌋ using integer arithmetic; 3 if π < π(β) then 4 return “π₯2 > π₯10 ” 5 else if π > π(β) then 6 return “π₯2 < π₯10 ” 7 else (π = π(β)) 8 first step is inconclusive (perform the second step). Note that, when π₯10 admits multiple distinct representations in the precision-π10 decimal format (i.e., when its cohort is non-trivial [5]), the success of the first step may depend on the specific representation passed as input. For instance, assume that the binary and decimal formats are binary64 and decimal64, respectively. Both π΄ = {π10 = 1015 , π10 = 0} and π΅ = {π10 = 1, π10 = 15} are valid representations of the integer 1. Assume we are trying to compare π₯10 = 1 to π₯2 = 2. Using representation π΄, we have π = 4, β = −32, and π(β) = −14 > π = −15, hence the test from Algorithm 1 shows that π₯10 < π₯2 . In 4 contrast, if π₯10 is given in the form π΅, we get π = 53, π(β) = π(2) = 0 = π, and the test is inconclusive. 3.3 We obtain πnorm + πsub 6 min #(π2 × π10 ) How Often is the First Step Enough? {οΈ 1 4 , π10 π2 }οΈ . We may quantify the quality of this first filter as follows. We say that Algorithm 1 succeeds if it answers “π₯2 > π₯10 ” or “π₯2 < π₯10 ” without proceeding to the second step, and fails otherwise. Let π2 and π10 denote the sets of representations of positive, finite numbers in the binary, resp. decimal formats of interest. (In the case of π2 , each number has a single representation.) Assuming zeros, infinities, and NaNs have been handled before, the input of Algorithm 1 may be thought of as a pair (π2 , π10 ) ∈ π2 × π10 . These are rough estimates. One way to get tighter bounds for specific formats is simply to count, for each value of π, the pairs (π2 , π10 ) such that π(β) = π. For instance, in the case of comparison between binary64 and decimal64 floating-point numbers, one can check that the failure rate in the sense of Proposition 3 is less than 0.1%. As a matter of course, pairs (π2 , π10 ) will almost never be equidistributed in practice. Hence the previous estimate should not be interpreted as a probability of Proposition 3. The proportion of input pairs (π2 , π10 ) ∈ success of Step 1. It seems more realistic to assume that π2 × π10 for which Algorithm 1 fails is bounded by a well-written numerical algorithm will mostly perform {οΈ }οΈ comparisons between numbers which are suspected to be 1 4 min , max , max min min close to each other. For instance, in an iterative algorithm π10 − π10 + 1 π2 − π2 + 1 where comparisons are used as part of a convergence test, assuming π2 > 3. it is to be expected that most comparisons need to proceed Proof: The first step fails if and only if π(β) = π. to the second step. Conversely, there are scenarios, e.g., Write β = π2 + π‘, that is, π‘ = π10 − π′10 + 1 − π10 + π. Then checking for out-of-range data, where the first step should be enough. π(β) = π rewrites as ⌊(π2 + π‘) log5 2⌋ = π, 4 which implies −π‘ + π log2 5 6 π2 < −π‘ + (π + 1) log2 5 < −π‘ + π log2 5 + 2.4. The value of π is determined by π10 , so that π‘ and π = π10 − π10 + 1 depend on π10 only. Thus, for any given π10 ∈ π10 , there can be at most 3 values of π2 for which π(β) = π. A similar argument shows that for given π2 and π10 , there exist at most one value of π10 such that π(β) = π. Let π2norm and π2sub be the subsets of π2 consisting of normal and subnormal numbers respectively. Let ππ = πmax − πmin + 1. The number πnorm of pairs (π2 , π10 ) ∈ π π norm π2 × π10 such that π(β) = π satisfies πnorm 6 #{π10 : π10 ∈ π10 } · #{π2 : π2 ∈ π2norm } · #{(π2 , π10 ) : π2 ∈ π2norm ∧ π(β) = π} 6 (10π10 − 1) · 2π2 −1 · min{π2 , 3π10 }. For subnormal π₯2 , we have the bound πsub := #{(π2 , π10 ) ∈ π2sub × π10 : π(β) = π} 6 #{π10 } · #π2sub = (10π10 − 1) · (2π2 −1 − 1). The total number of elements of π2 × π10 for which the first step fails is bounded by πnorm + πsub . This is to be compared with #π2 = π2 · 2π2 −1 + (π2 − 1) · (2π2 −1 − 1) > (π2 + 1) · 2π2 −1 , #π10 = π10 · (10π10 − 1). STEP : A CLOSER LOOK AT THE SIGNIFICANDS 4.1 S ECOND Problem Statement In the following we assume that π = π(β), i.e., π10 − π10 + 1 = ⌊(π2 − π10 + π + π10 − π′10 + 1) log5 (2)⌋ . (Otherwise, the first step already allowed us to compare π₯2 and π₯10 .) Define a function π (β) = We have 5π(β) . 2β+π€ β§ β¨ π (β) · π > π ⇒ π₯10 > π₯2 , π (β) · π < π ⇒ π₯10 < π₯2 , β© π (β) · π = π ⇒ π₯10 = π₯2 . (5) The second test consists in performing this comparison, with π (β) · π replaced by an accurate enough approximation. In order to ensure that an approximate test is indeed equivalent to (5), we need a lower bound π on the minimum nonzero value of β π(β) β β5 π ββ β πβ (π, π) = β β+π€ − β (6) 2 π that may appear at this stage. We want π to be as tight as possible in order to avoid unduly costly computations when approximating π (β) · π. The search space is constrained by the following observations. 5 Proposition 4. Let π and β be defined by Equation (2). The equality π = π(β) implies πmin − π2 − π′10 + 3 πmax + 2 2 6β< 2 . 1 + log5 2 1 + log5 2 (7) Proof: From (2), we get π2 = β + π + π′10 − π − 2. Since πmin − π2 + 1 6 π2 6 πmax and as we assumed 2 2 π = π(β), it follows that πmin − π2 + 1 6 β + π(β) − π + π′10 − 2 6 πmax . 2 2 Therefore we have πmin − π2 + π − π′10 + 3 6 (1 + log5 2)β < πmax + π − π′10 + 3, 2 2 and the bounds (7) follow because 0 6 π 6 π′10 − 1. The binary normalization of π10 , yielding π, implies that 2π | π. If π > 10π10 , then, by (1) and (2), it follows that π > 1 and π is even. As β + π(β) − π 6 πmax − π′10 + 2, 2 ′ we also have π > π . Since π is increasing, there exists β0 such that π ′ > 0 for β > β0 . Table 3 gives the range (7) for β with respect to the comparisons between basic IEEE formats. Required Worst-Case Accuracy Let us now deal with the problem of computing π, considering πβ (π, π) under the constraints given by Equation (3) and Proposition 4. A similar problem was considered by Cornea et al. [3] in the context of correctly rounded binary to decimal conversions. Their techniques yield worst and bad cases for the approximation problem we consider. We will take advantage of them in Section 7 to test our algorithms on many cases when π₯2 and π₯10 are very close. Here, we favor a different approach that is less computationally intensive and mathematically simpler, making the results easier to check either manually or with a proof-assistant. Problem 1. Find the smallest nonzero value of β π(β) β β5 πβ πβ (π, π) = ββ β+π€ − ββ 2 π subject to the constraints β§ π −1 2 6 π 6 2π2 − 1, βͺ βͺ 2 βͺ βͺ ′ ′ βͺ βͺ 2π10 −1 6 π 6 2π10 − 1, βͺ β¨ (2) (2) βmin 6 β 6 βmax , βͺ βͺ βͺ βͺ π is even if π > 10π10 , βͺ βͺ βͺ ′ β© if β > β0 , then 2π | π ⌉οΈ πmin − π2 − π′10 + 3 2 , 1 + log5 2 ⌊οΈ max ⌋οΈ π2 + 2 (2) βmax = , 1 + log 2 ⌈οΈ max 5 ′ ⌉οΈ π2 − π10 + 3 β0 = , 1 + log5 2 π ′ = β + π(β) − πmax + π′10 − 2. 2 (2) ⌈οΈ βmin = Additionally, π satisfies the following properties: 1) if π > 10π10 , then π is even; + π′10 − 2 is nonnegative (which 2) if π ′ = β + π(β) − πmax 2 ′ holds for large enough β), then 2π divides π. 4.2 where We recall how such a problem can be solved using the classical theory of continued fractions [4], [7], [9]. Given πΌ ∈ Q, build two finite sequences (ππ )06π6π and (ππ )06π6π by setting π0 = πΌ and {οΈ ππ = ⌊ππ ⌋ , ππ+1 = 1/(ππ − ππ ) if ππ ΜΈ= ππ . For all 0 6 π 6 π, the rational number ππ 1 = π0 + 1 ππ π1 + .. 1 .+ ππ is called the πth convergent of the continued fraction expansion of πΌ. Observe that the process defining the ππ essentially is the Euclidean algorithm. If we assume π−1 = 1, π−1 = 0, π0 = π0 , π0 = 1, we have ππ+1 = ππ+1 ππ + ππ−1 , ππ+1 = ππ+1 ππ + ππ−1 , and πΌ = ππ /ππ . It is a classical result [7, Theorem 19] that any rational number π/π such that β 1 π ββ β βπΌ − β < 2 π 2π is a convergent of the continued fraction expansion of πΌ. For basic IEEE formats, it turns out that this classical result is enough to solve Problem 1, as cases when it is not precise enough can be handled in an ad hoc way. First consider the case max(0, −π€) 6 β 6 π2 . We can then write |5π(β) π − 2β+π€ π| 2β+π€ π where the numerator and denominator are both integers. If πβ (π, π) ΜΈ= 0, we have |5π(β) π − π2β+π€ | > 1, hence ′ ′ πβ (π, π) > 1/(2β+π€ π) > 2π2 −β−2π10 +1 > 2−2π10 +1 . For the range of β where πβ (π, π) might be zero, the non-zero ′ minimum is thus no less than 2−2π10 +1 . (2) If now π2 + 1 6 β 6 βmax , then β + π€ > π′10 , so 5π(β) ′ β+π€ and 2 are integers, and π2β+π€ is divisible by 2π10 . As ′ π 6 2π10 − 1, we have πβ (π, π) ΜΈ= 0. To start with, assume ′ that πβ (π, π) 6 2−2π10 −1 . Then, the integers π and π 6 ′ 2π10 − 1 satisfy β π(β) β β5 ′ π ββ 1 β − 6 2−2π10 −1 < 2 , β 2β+π€ πβ 2π πβ (π, π) = and hence π/π is a convergent of the continued fraction expansion of πΌ = 5π(β) /2β+π€ . We compute the convergents with denominators less ′ than 2π10 and take the minimum of the |πΌ − ππ /ππ | for 6 (2) (2) βmin , βmax (2) (2) πmin , πmax #π β0 b32/d64 b32/d128 b64/d64 b64/d128 b128/d64 b128/d128 −140, 90 −61, 38 100 54 −181, 90 −78, 38 117 12 −787, 716 −339, 308 648 680 −828, 716 −357, 308 666 639 −11565, 11452 −4981, 4932 9914 11416 −11606, 11452 −4999, 4932 9932 11375 TABLE 3 Range of β for which we may have π = π(β), and size of the table used in the “direct method” of Section 5.1. which there exists π ∈ N such that π = πππ and π = πππ be stated as follows. Recall that π denotes a (tight) lower satisfy the constraints of Problem 1. Notice that this strat- bound on (6). egy only yields the actual minimum nonzero of πβ (π, π) Proposition 6. Assume that π approximates π (β) · π with if we eventually find, for some β, a convergent with ′ ′ relative accuracy π < π/2−π€+2 or better, that is, |πΌ − ππ /ππ | < 2−2π10 −1 . Otherwise, taking π = 2−2π10 −1 π yields a valid (yet not tight) lower bound. (8) |π (β) · π − π| 6 ππ (β) · π < −π€+2 π (β) · π. (2) 2 The case βmin 6 β 6 min(0, −π€) is similar. A numerator of πβ (π, π) then is |2−β−π€ π−5−π(β) π| and a denominator The following implications hold: ′ is 5−π(β) π. We notice that 5−π(β) > 2π10 if and only β§ π2 +1 if β 6 −π′10 . If −π′10 + 1 6 β 6 min(0, −π€) and =⇒ π₯10 > π₯2 , βͺ β¨ π>π+π·2 −β −π(β) πβ (π, π) ΜΈ= 0, we have |2 π − 5 π| > 1, hence π2 +1 (9) π<π−π·2 =⇒ π₯10 < π₯2 , ′ (2) βͺ πβ (π, π) > 1/(5−π(β) π) > 2−2π10 . For βmin 6 β 6 −π′10 , β© π2 +1 |π − π| 6 π · 2 =⇒ π₯10 = π₯2 . we use the same continued fraction tools as above. ′ There remains the case min(0, −π€) 6 β 6 max(0, −π€). Proof: First notice that π (β) 6 2−π€ = 2π2 −π10 +1 for If 0 6 β 6 −π€, a numerator of πβ (π, π) is |5π(β) 2−β−π€ π− ′ all β. Since π < 2π10 , condition (8) implies |π − π (β) · π| < π| and a denominator is π. Hence, if πβ (π, π) ΜΈ= 0, we ′ 2π2 +1 π, and hence |π (β) · π − π| > |π − π| − 2π2 +1 π. have πβ (π, π) > 1/π > 2π10 . If −π€ 6 β 6 0, a numerator Now consider each of the possible cases that appear of πβ (π, π) is |π − 5−π(β) 2β+π€ π| and a denominator is in (9). If |π − π| > 2π2 +1 π, then π (β) · π − π and π − π −π(β) β+π€ −β β+π€ 2π′10 −π2 −1 5 2 π 6 5·2 2 π < 5·2 . It follows ′ that if πβ (π, π) ΜΈ= 0, we have πβ (π, π) > 1/5 · 2−2π10 +π2 +1 . have the same sign. The first two implications from (5) then translate into the corresponding cases from (9). Proposition 5. The minimum nonzero values of πβ (π, π) If finally |π − π| 6 2π2 +1 π, then the triangle inequality under the constraints from Problem 1 for the basic IEEE yields |π (β) · π − π| 6 2π2 +2 π, which implies |π (β) − formats are attained for the parameters given in Table 4. π/π| < 2π2 +2 π/π < 2π2 +π€ π/π 6 π. By definition of π, Proof: This follows from applying the algorithm this cannot happen unless π/π = π (β). This accounts for outlined above to the parameters associated with each the last case. For instance, in the case of binary64–decimal64 combasic IEEE format. Scripts to verify the results are parisons, it is more than enough to compute the prodprovided along with our example implementation of the uct π (β) · π in 128-bit arithmetic. comparison algorithm (see Section 7). Proposition 4 gives the number of values of β to consider in the table of π (β), which grows quite large (23.5 kB 5 I NEQUALITY TESTING for b64/d64 comparisons, 721 kB in the b128/d128 case, As already mentioned, the first step of the full comparison assuming a memory alignment on 8-byte boundaries). algorithm is straightforwardly implementable in 32-bit However, it could possibly be shared with other algoor 64-bit integer arithmetic. The second step reduces to rithms such as binary-decimal conversions. Additionally, comparing π and π (β) · π, and we have seen in Section 4 β is available early in the algorithm flow, so that the that it is enough to know π (β) with a relative accuracy memory latency for the table access can be hidden behind given by the last column of Table 4. We now discuss the first step. The number of elements in the table can be reduced several ways to perform that comparison. The main at the price of a few additional operations. Several difficulty is to efficiently evaluate π (β)·π with just enough consecutive β map to a same value π = π(β). The accuracy. corresponding values of π (β) = 2π(β)·log2 5−β are binary shifts of each other. Hence, we may store the most 5.1 Direct Method significant bits of π (β) as a function of π, and shift the A direct implementation of Equation (5) just replaces π (β) value read off the table by an appropriate amount to with a sufficiently accurate approximation, read in a table recover π (β). The table size goes down by a factor of indexed with β. The actual accuracy requirements can about log2 (5) ≈ 2.3. 7 b32/d64 b32/d128 b64/d64 b64/d128 b128/d64 b128/d128 β π π 50 −159 −275 −818 2546 10378 10888194 11386091 4988915232824583 5148744585188163 7116022508838657793249305056613439 7977485665655127446147737154136553 13802425659501406 8169119658476861812680212016502305 12364820988483254 9254355313724266263661769079234135 13857400902051554 9844227914381600512882010261817769 log2 (π −1 ) 111.40 229.57 113.68 233.58 126.77 237.14 TABLE 4 Worst cases for πβ (π, π) and corresponding lower bounds π. More precisely, define πΉ (π) = 5π 2−π(π) , where π(π) = ⌊π · log2 5⌋. (10) We then have π (β) = πΉ (π(β)) · 2−π(β) with π(β) = β − π(π(β)). The computation of π and π is similar to that of π in Step 1. In addition, we may check that 1 6 πΉ (π(β)) < 2 and 0 6 π(β) 6 3 for all β. Since π(β) is nonnegative, multiplication by 2π(β) can be implemented with a bitshift, not requiring branches. We did not implement this size reduction: instead, we concentrated on another size reduction opportunity that we shall describe now. (the order of the bounds depends on π). The integer 5π fits on π(π) + 1 bits, where π is the function defined by (10). Instead of tabulating π (β) directly, we will use two tables: one containing the most significant bits of 5πΎπ roughly to the precision dictated by the worst cases computed in Section 4, and the other containing the exact value 5π for 0 6 π 6 πΎ − 1. We denote by π1 and π2 the respective precisions (entry sizes in bits) of these tables. Based on these ranges and precisions, we assume π1 > log2 (π −1 ) − π€ + 3, (12) π2 > π(πΎ − 1) + 1. (13) In addition, it is natural to suppose π1 larger than or close to π2 : otherwise, an algorithm using two approximate tables will likely be more efficient. 5.2 Bipartite Table In practice, π2 will typically be one of the available That second table size reduction method uses a bipartite machine word widths, while, for reasons that should table. Additionally, it takes advantage of the fact that, become clear later, π1 will be chosen slightly smaller for small nonnegative π, the exact value of 5π takes less than a word size. For each pair of basic IEEE formats, space than an accurate enough approximation of π (β) Tables 7 and 8 provide values of π, πΎ, π1 , π2 (and other π (for instance, 5 fits on 64 bits for π 6 27). In the case of a parameters whose definition follows later) suitable for typical implementation of comparison between binary64 typical processors. The suggested values were chosen and decimal64 numbers, the bipartite table uses only by optimizing either the table size or the number of 800 bytes of storage. elementary multiplications in the algorithm, with or Most of the overhead in arithmetic operations for without the constraint that πΎ be a power of two. the bipartite table method can be hidden on current It will prove convenient to store the table entries leftprocessors through increased instruction-level parallelism. aligned, in a fashion similar to floating-point significands. The reduction in table size also helps decreasing the Thus, we set probability of cache misses. Recall that we suppose π = π(β), so that β lies π1 (π) = 5πΎπ ·2π1 −1−π(πΎπ) , (2) (2) between bounds βmin , βmax deduced from Proposition 4. π2 (π) = 5π ·2π2 −1−π(π) , (2) (2) Corresponding bounds πmin , πmax on π follow since π is where the power-of-two factors provide for the desired nondecreasing. For some integer “splitting factor” πΎ > 0 and π = ±1, alignment. One can check that write ⌈οΈ ⌉οΈ 2π1 −1 6 π1 (π) < 2π1 , 2π2 −1 6 π2 (π) < 2π2 (14) ππ π = π(β) = π(πΎπ − π), π= , (11) πΎ for all β. The value π (β) now decomposes as so that (οΈ πΎπ )οΈπ (οΈ )οΈπ 5 −β−π€ π (β) = ·2 . 5π π1 (π) π (β) = β+π€ = 2π·(π2 −π1 )−π(β)−π€ (15) 5π 2 π2 (π) Typically, πΎ will be chosen as a power of 2, but other (οΈ )οΈ values can occasionally be useful. With these definitions, where π(β) = β − π · π(πΎπ) − π(π) . (16) we have 0 6 π 6 πΎ − 1, and π lies between ⌈οΈ ⌉οΈ ⌈οΈ ⌉οΈ (2) (2) Equations (10) and (16) imply that π π π min and π max πΎ πΎ β − π log2 5 − 1 < π(β) < β − π log2 5 + 1. 8 As we also have β log5 2 − 1 6 π = π(β) < β log5 2 by definition of π, it follows that Yet additional properties hold that allow for a more efficient final decision step. (17) Proposition 8. Assume π = π(β), and let ΔΜ be the signed integer defined above. For π > 0, the following equivalences hold: 0 6 π(β) 6 3. Now let π > −1 be an integer serving as an adjustment parameter to be chosen later. In practice, we will usually2 choose π = 0 and sometimes π = −1 (see Tables 7 and 8). For most purposes the reader may simply assume π = 0. Define β§ ′ βͺ Δ = π1 (π) · π · 2π −π10 βͺ βͺ Furthermore, βͺ β¨ − π (π) · π · 2π +π1 −π2 +π(β)−π2 −1 , π = +1, 2 βͺ Δ = π1 (π) · π · 2π −π2 −1 βͺ βͺ βͺ ′ β© − π2 (π) · π · 2π +π1 −π2 −π(β)−π10 , π = −1. The relations |π₯2 | > |π₯10 |, |π₯2 | = |π₯10 |, and |π₯2 | < |π₯10 | hold respectively when πΔ < 0, Δ = 0, and πΔ > 0. Proposition 7. Unless π₯2 = π₯10 , we have |Δ| > 2π +1 . Proof: Assume π₯2 ΜΈ= π₯10 . For π = +1, and using (15), the definition of Δ rewrites as (οΈ π )οΈ Δ = π2 (π) π 2π +π1 −π2 +π(β)−π2 −1 π (β) − . π Since π (β) = π/π is equivalent to π₯2 = π₯10 , we know by Proposition 5 that |π (β) − π/π| > π. Together with the bounds (3), (12), (14), and (17), this implies log2 |Δ| > (π2 − 1) + (π′10 − 1) + π + π1 − π2 − π2 − 1 + log2 π = (π1 + log2 π + π€ − 3) + π + 1 > π + 1. Similarly, for π = −1, we have (οΈ π )οΈ Δ = −π1 (π) π 2π −π2 −1 π (β) − , π so that Δ < 0 ⇐⇒ ΔΜ 6 −2π , Δ = 0 ⇐⇒ 0 6 ΔΜ 6 2π , Δ > 0 ⇐⇒ 2π +1 . ΔΜ > if π ∈ {−1, 0} and {οΈ π2 + 1, π1 − π2 + π > π′10 + 3, π = +1, π = −1, (18) then Δ and ΔΜ have the same sign (in particular, ΔΜ = 0 if and only if Δ = 0). Proof: Write the definition of ΔΜ as ⌊οΈ ⌋οΈ ⌊οΈ ⌋οΈ ΔΜ = ⌈π1 (π)⌉π − π2 (π)π ′ where the expressions of π and π ′ depend on π, and ′ define error terms πΏtbl , πΏrnd , πΏrnd by πΏtbl = ⌈π1 (π)⌉ − π1 (π), πΏrnd = ⌊⌈π1 (π)⌉π⌋ − ⌈π1 (π)⌉π, ′ πΏrnd = ⌊π2 (π)π ′ ⌋ − π2 (π)π ′ . The πΏ’s then satisfy 0 6 πΏtbl < 1, ′ −1 < πΏrnd , πΏrnd 6 0. For both possible values of π, we have3 0 6 π 6 2π and hence ′ −1 < ΔΜ − Δ = ππΏtbl + πΏrnd − πΏrnd < 2π + 1. If Δ < 0, Proposition 7 implies that ΔΜ < −2π +1 + 2π + 1 = −2π + 1. log2 |Δ| > (π1 − 1) + (π′10 − 1) + π − π2 − 1 + log2 π It follows that ΔΜ 6 −⌊2π ⌋ since ΔΜ is an integer. By the same reasoning, Δ > 0 implies ΔΜ > ⌊2π +1 ⌋, and Δ = 0 implies 0 6 ΔΜ 6 ⌈2π ⌉. This proves the first claim. when Δ ΜΈ= 0. Now assume that (18) holds. In this case, we have As already explained, the values of π2 (π) are integers ′ πΏrnd = 0, so that −1 < ΔΜ − Δ < 2π . The upper bounds and can be tabulated exactly. In contrast, only an approxon ΔΜ become ΔΜ 6 −⌊2π ⌋ − 1 for Δ < 0 and ΔΜ 6 ⌈2π ⌉ − 1 imation of π1 (π) can be stored. We represent these values for Δ = 0. When −1 6 π 6 0, these estimates respectively as ⌈π1 (π)⌉, hence replacing Δ by the easy-to-compute imply ΔΜ < 0 and ΔΜ = 0, while ΔΜ > ⌊2π +1 ⌋ implies Δ > 0. β§ ⌊οΈ ⌋οΈ π −π′10 The second claim follows. βͺ ΔΜ = ⌈π1 (π)⌉ · π · 2 βͺ βͺ ⌊οΈ ⌋οΈ βͺ The conclusion ΔΜ = 0 = Δ when π₯2 = π₯10 for certain π +π1 −π2 +π(β)−π2 −1 β¨ − π2 (π) · π · 2 , π = +1, parameter choices means that there is no error in the ⌊οΈ ⌋οΈ βͺ ΔΜ = ⌈π1 (π)⌉ · π · 2π −π2 −1 βͺ approximate computation of Δ in these cases. Specifically, βͺ βͺ ⌊οΈ ⌋οΈ β© π +π1 −π2 −π(β)−π′10 the tabulation error πΏtbl and the rounding error πΏrnd cancel − π2 (π) · π · 2 , π = −1. out, thanks to our choice to tabulate the ceiling of π1 (π) Proposition 7 is in principle enough to compare π₯2 with (and not, say, the floor). π₯10 , using a criterion similar to that from Proposition 6. We now describe in some detail the algorithm resulting from Proposition 8, in the case π = +1. In particular, we 2. See the comments following Equations (19) to (21). Yet allowing for positive values of π enlarges the possible choices of parameters will determine integer datatypes on which all steps can be >π +1 to satisfy (21) (resp. (21’)) for highly unusual floating-point formats and implementation environments. For instance, it makes it possible to store the table for π1 in a more compact fashion, using a different word size for the table than for the computation. 3. Actually 0 6 π 6 2π −1 for π = −1: this dissymmetry is related to our choice of always expressing the lower bound on Δ in terms of π rather than using a lower bound on π (β)−1 − π/π when π = −1. 9 performed without overflow, and arrange the powers of in Steps 3 and 4 are left shifts. They can be performed two in the expression of ΔΜ in such a way that computing without overflow on unsigned words of respective widths the floor functions comes down to dropping the least π (π1 ), π (π′10 ) and π (π2 ) since π (π1 ) − π1 > π + 1 and π (π2 ) − π2 > π + 3, both by (20). The same inequality significant words of multiple-word integers. Let π : N → N denote a function satisfying π (π) > π implies that the (positive) integers π΄ and π΅ both fit on for all π. In practice, π (π) will typically be the width of π (π1 )−1 bits, so that their difference can be computed by the smallest “machine word” that holds at least π bits. a subtraction on π (π1 ) bits. The quantity π΄−π΅ computed We further require that (for π = +1): in Step 5 agrees with the previous definition of ΔΜ, and Δ′ has the same sign as ΔΜ, hence Proposition 8 implies ′ ′ π (π10 ) − π10 > 1, (19) that the relation returned in Step 7 is correct. π (π1 ) − π1 > π (π2 ) − π2 + π + 3, (20) When π ∈ {−1, 0} and (18) holds, no low-order bits π (π2 ) − π2 + π (π2 ) − π2 > π (π1 ) − π1 − π + 1. (21) are dropped in Step 4, and Step 6 can be omitted (that is, replaced by Δ′ := ΔΜ). Also observe that a These assumptions are all very mild in view of the version of Step 7 returning −1, 0 or 1 according to the IEEE-754-2008 Standard. Indeed, for all IEEE interchange comparison result can be implemented without branching, formats (including binary16 and formats wider than using bitwise operations on the individual words which 64 bits), the space taken up by the exponent and sign make up the representation of Δ′ in two’s complement leaves us with at least 5 bits of headroom if we choose encoding. for π (π2 ), resp. π (π′10 ) the word width of the format. The algorithm for π = −1 is completely similar, except Even if we force π (π2 ) = π2 and π = 0, this always that (19)–(21) become allows for a choice of π1 , π2 and π (π2 ) satisfying (12), (13) and (19)–(21). π (π2 ) − π2 > 2, (19’) Denote π₯+ = max(π₯, 0) and π₯− = max(−π₯, 0), so that π (π1 ) − π1 > π (π2 ) − π2 + π + 1, (20’) π₯ = π₯+ − π₯− . π (π′10 ) − π′10 + π (π2 ) − π2 > π (π1 ) − π1 − π + 3, (21’) Algorithm 2. Step 2, second method, π = +1. 1 Compute π and π as defined in (11), with π = +1. and π΄ and π΅ are computed as ⌊οΈ ⌋οΈ + − Compute π(β) using (16) and Proposition 2. π΄ = (⌈π1 (π)⌉ · 2π ) · (π2π (π2 )−π2 −π −1 ) · 2−π (π2 ) 2 Read ⌈π1 (π)⌉ from the table, storing it on a π (π1 )-bit ⌊οΈ ⌋οΈ ′ π΅ = π2 (π) · (π2π−π(β) ) · 2π (π1 )−π (π2 )−π (π10 ) , (multiple-)word. 3 Compute with ⌊οΈ ⌋οΈ π+ π (π′10 )−π′10 −π − −π (π′10 ) π΄ = (⌈π1 (π)⌉ · 2 ) · (π2 )·2 π = π (π′10 ) − π′10 + π (π2 ) − π2 − π (π1 ) + π1 + π, by (constant) left shifts followed by a π (π1 )-byrespectively using a π (π1 )-by-π (π2 )-bit and a π (π2 )π (π′10 )-bit multiplication, dropping the least signifiby-π (π′10 ) multiplication. cant word(s) of the result that correspond to π (π′10 ) Tables 7 and 8 suggest parameter choices for comparbits. isons in basic IEEE formats. (We only claim that these are 4 Compute reasonable choices that satisfy all conditions, not that they ⌊οΈ ⌋οΈ are optimal for any well-defined metric.) Observe that, π΅ = π2 (π) · (π2π(β)+π ) · 2π (π1 )−π (π2 )−π (π2 ) though it has the same overall structure, the algorithm where the constant π is given by presented in [2] is not strictly speaking a special case π = π (π2 ) − π2 + π (π2 ) − π2 − π (π1 ) + π1 + π − 1 of Algorithm 2, as the definition of the bipartite table (cf. (11)) is slightly different. in a similar way, using a π (π2 )-by-π (π2 )-bit multiplication. 5 Compute the difference ΔΜ = π΄ − π΅ as a π (π1 )-bit 6 A N E QUALITY T EST Besides deciding the two possible inequalities, the direct signed integer. 6 Compute Δ′ = ⌊ΔΜ2−π −1 ⌋2π +1 by masking the π + 1 and bipartite methods described in the previous sections are able to determine cases when π₯2 is equal to π₯10 . least significant bits of ΔΜ. However, when it comes to solely determine such equality 7 Return β§ ′ βͺ “π₯ < π₯ ” if Δ < 0, 10 2 cases additional properties lead to a simpler and faster β¨ ′ algorithm. “π₯10 = π₯2 ” if Δ = 0, βͺ β© ′ The condition π₯2 = π₯10 is equivalent to “π₯10 > π₯2 ” if Δ > 0. Proposition 9. When either π > 0 or π = −1 and (18) holds, Algorithm 2 correctly decides the ordering between π₯2 and π₯10 . Proof: Hypotheses (19) and (21), combined with the inequalities π(β) > 0 and π > −1, imply that the shifts π · 2β+π€ = π · 5π , (22) and thus implies certain divisibility relations between π, π, 5±π and 2±β±π€ . We now describe an algorithm that takes advantage of these relations to decide (22) using 10 only binary shifts and multiplications on no more than max(π′10 + 2, π2 + 1) bits. Proposition 10. Assume π₯2 = π₯10 . Then, we have eq eq πmin 6 π 6 πmax , −π′10 + 1 6 β 6 π2 . where eq πmin = −⌊π10 (1 + log5 2)⌋, the algorithm correctly returns false. In particular, no table eq eq access occurs unless π lies between πmin and πmax . Now assume that these four conditions hold. In particular, we have π1 = 2−π’ π as π2 = π, and similarly + π1 = 2−π£ π. The bounds on π imply that 5π fits on π2 − bits and 5π fits on π′10 bits. If π and π€ + β are both nonnegative, then π3 = π, and we have eq πmax = ⌊π2 log5 2⌋. Additionally, π −π divides π10 β if π > 0, then 5 divides π, otherwise 5 (and π); β+π€ β if β > −π€, then 2 divides π, otherwise 2−(β+π€) divides π. Proof: First observe that π must be equal to π(β) by Proposition 1, so π > 0 if and only if β > 0. The divisibility properties follow immediately from the coprimality of 2 and 5. When π > 0, they imply 5π 6 π < 2π2 − 1, whence π < π2 log5 2. Additionally, 2−β−π€ π is an integer. Since ′ β > 0, it follows that 2β 6 2−π€ π < 2π10 −π€ and hence β 6 π′10 − π€ − 1 = π2 . If now π < 0, then 5−π divides π = 2π π10 and hence divides π10 , while 2−β divides 2π€ π. Since π10 < 10π10 and 2π€ π < 2π2 +π€ , we have −π < π10 log5 10 and −β < π10 log2 10 < π′10 . These properties translate into Algorithm 3 below. Recall that π₯+ = max(π₯, 0) and π₯− = π₯+ − π₯. (Branchless algorithms exist to compute π₯+ and π₯− [1].) Let π = max(π′10 + 2, π2 + 1). ′ + 5π π1 = 5π(β) 2−(π€+β) π < 2−π€+π10 = 2π2 +1 6 2π . By the same reasoning, if π, π€ + β 6 0, then π3 = π and ′ 5−π 2π€+β π < 5 · 2π10 −1 < 2π . If now π > 0 with β 6 −π€, then π3 = π1 and ′ ′ 5π π < 2β+π10 6 2−π€+π10 = 2π2 +1 6 2π . Similarly, for π 6 0, −β 6 π€, we have 5−π π < 5 · 2−β+π2 6 5 · 2π€+π2 6 2π . In all four cases, − π3 = 5π 2−π’ π and + π3 = 5π 2−π£ π are computed without overflow. Therefore + − π3 π · 2(β+π€) −(β+π€) = π3 π · 5π+ −π− = π · 2β+π€ π · 5π and π₯2 = π₯10 if and only if π3 = π3 , by (22). 7 E XPERIMENTAL RESULTS For each combination of a binary basic IEEE format and a decimal one, we implemented the comparison algorithm Algorithm 3. Equality test. eq eq consisting of the first step (Section 3) combined with 1 If not πmin 6 π 6 πmax then return false. − + the version of the second step based on a bipartite table 2 Set π’ = (π€ + β) and π£ = (π€ + β) . (Section 5.2). We used the parameters suggested in the 3 Using right shifts, compute second part of Table 8, that is for the case when πΎ is a π1 = ⌊2−π’ π⌋, π1 = ⌊2−π£ π⌋. power of two and one tries to reduce table size. The implementation was aided by scripts that take as π’ π£ 4 Using left shifts, compute π2 = 2 π1 and π2 = 2 π1 . + − input the parameters from Tables 2 and 8 and produce C 5 Read 5π and 5π from a table (or compute them). − code with GNU extensions [10]. Non-standard extensions 6 Compute π3 = 5π π1 with a π′10 × π2 → π bit mulare used for 128-bit integer arithmetic and other basic tiplication (meaning that the result can be anything integer operations. No further effort at optimization was if the product does not fit on π bits). made. + 7 Compute π3 = 5π π1 with a π2 × π′10 → π bit Our implementations, the code generator itself, as well multiplication. as various scripts to select the parameters are available 8 Return (π2 = π) ∧ (π2 = π) ∧ (π = π(β)) ∧ (π3 = π3 ). at As a matter of course, the algorithm can return false http://hal.archives-ouvertes.fr/hal-01021928/. as soon as any of the conditions from Step 8 is known The code generator can easily be adapted to produce unsatisfied. other variants of the algorithm. Proposition 11. Algorithm 3 correctly decides whether π₯2 = We tested the implementations on random inputs π₯10 . generated as follows. For each decimal exponent and Proof: First observe that, if any of the conditions each quantum [5, 2.1.42], we generate a few random eq eq π = π(β), πmin 6 π 6 πmax , π2 = π and π2 = π is vio- significands and round the resulting decimal numbers lated, then π₯2 ΜΈ= π₯10 by Propositions 1 and 10. Indeed, upwards and downwards to the considered binary format. π₯2 = π₯10 implies π2 = π and π2 = π due to the (We exclude the decimal exponents that merely yield to divisibility conditions of Proposition 10. In all these cases, underflow or overflow on the binary side.) 11 Special cases (±0, NaNs, Inf) π₯2 , π₯10 of opposite sign π₯2 normal, same sign, “easy” cases π₯2 subnormal, same sign, “easy” cases π₯2 normal, same sign, “hard” cases π₯2 subnormal, same sign, “hard” cases Naïve method converting π₯10 to binary64 (incorrect) min/avg/max Naïve method converting π₯2 to decimal64 (incorrect) min/avg/max Direct method (Section 5.1) min/avg/max Bipartite table method (manual implementation) (Section 5.2) min/avg/max 18/–/164 30/107/188 31/107/184 45/106/178 60/87/186 79/106/167 21/–/178 44/124/179 48/132/180 89/119/179 39/78/180 78/107/179 12/–/82 15/30/76 25/57/118 153/163/189 45/56/139 171/177/189 11/–/74 16/31/73 21/43/95 19/27/73 29/40/119 34/49/94 TABLE 5 Timings (in cycles) for binary64–decimal64 comparisons. Table 6 reports the observed timings for our random test data. The generated code was executed on a system equipped with a quad-core Intel Core i5-3320M processor clocked at 2.6 GHz, running Linux 3.16 in 64 bit mode. We used the gcc 4.9.2 compiler, at optimization level -O3, with the -march=native flag. The measurements were performed using the Read Time Stamp Counter instruction (RDTSC) after pipeline serialization. The serialization and function call overhead was estimated by timing an empty function and subtracted off. The caches were preheated by additional comparison function calls, the results of which were discarded. It can be observed that “balanced” comparisons (d64/b64, d128/b128) perform significantly better than “unbalanced” ones. A quick look at the generated assembly code suggests that there is room for performance improvements, especially in cases involving 128-bit floating point formats. We performed more detailed experiments in the special case of binary64–decimal64 comparisons. In complement to the generated code, we wrote a version of the bipartite table method with some manual optimization, and implemented the method based on direct tabulation of π (β) presented in Section 5.1. We tested these implementations thoroughly with test vectors that extensively cover all floating-point input classes (normal numbers, subnormals, Not-A-Numbers, zeros, and infinities) and exercise all possible values of the normalization parameter π (cf. Section 3) as well as all possible outcomes for both of the algorithm’s steps. Finally, we compared them for performance to the naïve comparison method where one of the inputs is converted d64 d128 b32 b64 b128 41 235 36 266 75 61 TABLE 6 Average run time (in cycles) for the bipartite table method, using the parameters suggested in Table 8 for πΎ = 2π , optimizing table size. to the radix of the other input. These experimental results are reported in Table 5. In the last two rows, “easy” cases are cases when the first step of our algorithm succeeds, while “hard” cases refer to those for which running the second step is necessary. 8 C ONCLUSION Though not foreseen in the IEEE 754-2008 Standard, exact comparisons between floating-point formats of different radices would enrich the current floating-point environment and make numerical software safer and easier to prove. This paper has investigated the feasibility of such comparisons. A simple test has been presented that eliminates most of the comparison inputs. For the remaining cases, two algorithms were proposed, a direct method and a technique based on a more compact bipartite table. For instance, in the case of binary64–decimal64, the bipartite table uses only 800 bytes of table space. Both methods have been proven, implemented and thoroughly tested. According to our experiments, they outperform the naïve comparison technique consisting in conversion of one of the inputs to the respectively other format. Furthermore, they always return a correct answer, which is not the case of the naïve technique. Since the algorithmic problems of exact binary to decimal comparison and correctly rounded radix conversion are related, future investigations should also consider the possible reuse of tables for both problems. Finally, one should mention that considerable additional work is required in order to enable mixed-radix comparisons in the case when the decimal floating-point number is stored in dense-packed-decimal representation. R EFERENCES [1] [2] Jörg Arndt, Matters computational. Ideas, algorithms, source code, Springer, 2011. Nicolas Brisebarre, Christoph Lauter, Marc Mezzarobba, and Jean-Michel Muller, Comparison between binary64 and decimal64 floating-point numbers, ARITH 21 (Austin, Texas, USA) (Alberto Nannarelli, Peter-Michael Seidel, and Ping Tak Peter Tang, eds.), IEEE Computer Society, 2013, pp. 145–152. 12 Marius Cornea, Cristina Anderson, John Harrison, Ping Tak Peter Tang, and Eric Schneider Evgeny Gvozdev, A software implementation of the IEEE 754R decimal floating-point arithmetic using the binary encoding format, IEEE Transactions on Computers 58 (2009), no. 2, 148–162. [4] Godfrey H. Hardy and Edward M. Wright, An introduction to the theory of numbers, Oxford University Press, London, 1979. [5] IEEE Computer Society, IEEE standard for floating-point arithmetic, IEEE Standard 754-2008, August 2008, available at http: //ieeexplore.ieee.org/servlet/opac?punumber=4610933. [6] ISO/IEC JTC 1/SC 22/WG 14, Extension for the programming language C to support decimal floating-point arithmetic, Proposed Draft Technical Report, May 2008. [7] Aleksandr Ya. Khinchin, Continued fractions, Dover, New York, 1997. [8] Jean-Michel Muller, Nicolas Brisebarre, Florent de Dinechin, Claude-Pierre Jeannerod, Vincent Lefèvre, Guillaume Melquiond, Nathalie Revol, Damien Stehlé, and Serge Torres, Handbook of floating-point arithmetic, Birkhäuser, 2010. [9] Oskar Perron, Die Lehre von den Kettenbrüchen, 3rd ed., Teubner, Stuttgart, 1954–57. [10] The GNU project, GNU compiler collection, 1987–2014, available at http://gcc.gnu.org/. [11] Charles Tsen, Sonia González-Navarro, and Michael Schulte, Hardware design of a binary integer decimal-based floating-point adder, ICCD 2007. 25th International Conference on Computer Design, IEEE, 2007, pp. 288–295. [3] Nicolas Brisebarre was born in Bordeaux, France, in 1971. He received his Ph.D. in pure mathematics from the Université Bordeaux I, France, in 1998. He is Chargé de recherche (junior researcher) at CNRS, France, and he is a member of the LIP laboratory (LIP is a joint computer science laboratory of CNRS, École Normale Supérieure de Lyon, INRIA, and Université Claude Bernard Lyon 1). His research interests are in computer arithmetic, number theory, validated computing and computer algebra. Christoph Lauter was born in Amberg, Germany, in 1980. He received this Ph.D. in Computer Science from École Normale Supérieure de Lyon, France, in 2008. He worked as a Software Engineer in the Numerics Team at Intel Corporation. Since 2010, he is Maître de Conférences (Assistant Professor) at Université Pierre et Marie Curie Paris 6, Sorbonne Universités, in the PEQUAN Team. His research interests are Floating-Point Arithmetic and Validated Numerical Computing. Marc Mezzarobba received the Ph.D. degree in Computer Science from École polytechnique in 2011. Since 2013 he has been with CNRS and Université Paris 6. His main research interests are in computer algebra and computer arithmetic. Jean-Michel Muller was born in Grenoble, France, in 1961. He received his Ph.D. degree in 1985 from the Institut National Polytechnique de Grenoble. He is Directeur de Recherches (senior researcher) at CNRS, France, and he is the cohead of GDR-IM. His research interests are in Computer Arithmetic. Dr. Muller was co-program chair of the 13th IEEE Symposium on Computer Arithmetic (Asilomar, USA, June 1997), general chair of SCAN’97 (Lyon, France, sept. 1997), general chair of the 14th IEEE Symposium on Computer Arithmetic (Adelaide, Australia, April 1999), general chair of the 22nd IEEE Symposium on Computer Arithmetic (Lyon, France, June 2015). He is the author of several books, including ”Elementary Functions, Algorithms and Implementation” (2nd edition, Birkhauser, 2006), and he coordinated the writing of the ”Handbook of Floating-Point Arithmetic (Birkhäuser, 2010). He is an associate editor of the IEEE Transactions on Computers, and a senior member of the IEEE. 13 b32/d64 b32/d128 b64/d64 b64/d128 b128/d64 b128/d128 32 64 32 128 64 64 64 128 128 64 128 128 −1 8 −4, 10 145 17 159, 160 32, 32 0 3 332 9 +1 8 −42, 39 117 17 125, 128 32, 32 0 3 1344 10 −1 8 −38, 45 178 17 191, 192 32, 32 0 3 2048 16 +1 8 −622, 617 190 17 190, 192 32, 32 −1 3 29792 16 +1 8 −624, 617 242 17 253, 256 32, 32 0 3 39776 36 −1 8 −4, 8 86 17 95, 96 32, 32 0 3 188 5 −1 16 −2, 5 145 35 159, 160 64, 64 0 7 288 13 +1 32 −10, 10 117 72 125, 128 96, 96 0 7 720 14 −1 32 −9, 12 178 72 191, 192 96, 96 0 7 912 24 −1 64 −77, 78 190 147 191, 192 160, 160 0 7 5024 34 +1 64 −78, 78 242 147 253, 256 160, 160 0 7 6304 52 any πΎ, optimizing multiplication count π −1 πΎ 13 range of π −2, 5 min π1 satisfying (12) 86 min π2 satisfying (13) 28 chosen π1 , π (π1 ) 95, 96 chosen π2 , π (π2 ) 32, 32 π 0 Step 6 of Algo. 2 can be omitted 3 total table size in bytes 148 32-bit muls 5 −1 13 −2, 6 145 28 159, 160 32, 32 0 3 232 9 +1 14 −24, 22 117 31 125, 128 32, 32 0 3 808 10 −1 14 −22, 26 178 31 191, 192 32, 32 0 3 1232 16 +1 14 −355, 353 190 31 190, 192 32, 32 −1 3 17072 16 +1 14 −357, 353 242 31 253, 256 32, 32 0 3 22808 36 −1 13 −2, 6 145 28 159, 160 32, 32 0 3 232 9 +1 28 −12, 11 117 63 125, 128 64, 64 0 3 608 12 +1 28 −12, 11 178 63 189, 192 64, 64 0 3 800 28 −1 69 −71, 73 190 158 191, 192 160, 160 0 7 4860 34 +1 83 −60, 60 242 191 253, 256 192, 192 0 7 5864 56 π (π2 ) π (π′10 ) πΎ = 2π , optimizing multiplication count π −1 πΎ 8 range of π −4, 8 min π1 satisfying (12) 86 min π2 satisfying (13) 17 chosen π1 , π (π1 ) 95, 96 chosen π2 , π (π2 ) 32, 32 π 0 Step 6 of Algo. 2 can be omitted 3 total table size in bytes 188 32-bit muls 5 πΎ = 2π , optimizing table size π πΎ range of π min π1 satisfying (12) min π2 satisfying (13) chosen π1 , π (π1 ) chosen π2 , π (π2 ) π Step 6 of Algo. 2 can be omitted total table size in bytes 32-bit muls any πΎ, optimizing table size π πΎ range of π min π1 satisfying (12) min π2 satisfying (13) chosen π1 , π (π1 ) chosen π2 , π (π2 ) π Step 6 of Algo. 2 can be omitted total table size in bytes 32-bit muls −1 13 −2, 5 86 28 95, 96 32, 32 0 3 148 5 TABLE 7 Suggested parameters for Algorithm 2 on a 32-bit machine. 14 b32/d64 b32/d128 b64/d64 b64/d128 b128/d64 b128/d128 64 64 64 128 64 64 64 128 128 64 128 128 −1 16 −2, 5 145 35 191, 192 64, 64 0 3 320 5 +1 16 −21, 20 117 35 125, 128 64, 64 0 3 800 3 −1 16 −19, 23 178 35 191, 192 64, 64 0 3 1160 5 +1 16 −311, 309 190 35 190, 192 64, 64 −1 3 15032 5 +1 16 −312, 309 242 35 253, 256 64, 64 0 3 20032 10 +1 16 −3, 3 86 35 125, 128 64, 64 0 3 240 3 −1 16 −2, 5 145 35 191, 192 64, 64 0 3 320 5 +1 16 −21, 20 117 35 125, 128 64, 64 0 3 800 3 −1 32 −9, 12 178 72 191, 192 128, 128 0 7 1040 7 −1 64 −77, 78 190 147 191, 192 192, 192 0 7 5280 9 +1 64 −78, 78 242 147 253, 256 192, 192 0 7 6560 14 any πΎ, optimizing multiplication count π +1 πΎ 13 range of π −4, 3 min π1 satisfying (12) 86 min π2 satisfying (13) 28 chosen π1 , π (π1 ) 125, 128 chosen π2 , π (π2 ) 64, 64 π 0 Step 6 of Algo. 2 can be omitted 3 total table size in bytes 232 64-bit muls 3 −1 20 −1, 4 145 45 191, 192 64, 64 0 3 304 5 +1 28 −12, 11 117 63 125, 128 64, 64 0 3 608 3 −1 28 −11, 13 178 63 191, 192 64, 64 0 3 824 5 +1 28 −177, 177 190 63 190, 192 64, 64 −1 3 8744 5 +1 28 −178, 177 242 63 253, 256 64, 64 0 3 11616 10 −1 20 −1, 4 145 45 191, 192 64, 64 0 3 304 5 +1 28 −12, 11 117 63 125, 128 64, 64 0 3 608 3 +1 28 −12, 11 178 63 189, 192 64, 64 0 3 800 7 −1 82 −60, 61 190 189 191, 192 192, 192 0 7 4896 9 +1 83 −60, 60 242 191 253, 256 192, 192 0 7 5864 14 π (π2 ) π (π′10 ) πΎ = 2π , optimizing multiplication count π +1 πΎ 16 range of π −3, 3 min π1 satisfying (12) 86 min π2 satisfying (13) 35 chosen π1 , π (π1 ) 125, 128 chosen π2 , π (π2 ) 64, 64 π 0 Step 6 of Algo. 2 can be omitted 3 total table size in bytes 240 64-bit muls 3 πΎ = 2π , optimizing table size π πΎ range of π min π1 satisfying (12) min π2 satisfying (13) chosen π1 , π (π1 ) chosen π2 , π (π2 ) π Step 6 of Algo. 2 can be omitted total table size in bytes 64-bit muls any πΎ, optimizing table size π πΎ range of π min π1 satisfying (12) min π2 satisfying (13) chosen π1 , π (π1 ) chosen π2 , π (π2 ) π Step 6 of Algo. 2 can be omitted total table size in bytes 64-bit muls +1 13 −4, 3 86 28 125, 128 64, 64 0 3 232 3 TABLE 8 Suggested parameters for Algorithm 2 on a 64-bit machine.