Skip to content

Commit

Permalink
Ported V8's fast-dtoa. Most conversions should now run at comparable …
Browse files Browse the repository at this point in the history
…speed to strconv.FormatFloat().
  • Loading branch information
dop251 committed Apr 27, 2020
1 parent 1edec86 commit 351feaf
Show file tree
Hide file tree
Showing 12 changed files with 1,791 additions and 833 deletions.
19 changes: 0 additions & 19 deletions date.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,6 @@ func (d *dateObject) export() interface{} {
return nil
}

func (d *dateObject) setTime(year, m, day, hour, min, sec, nsec int64) Value {
t, ok := mkTime(year, m, day, hour, min, sec, nsec, time.Local)
if ok {
return d.setTimeMs(timeToMsec(t))
}
d.unset()
return _NaN
}

func (d *dateObject) setTimeUTC(year, m, day, hour, min, sec, nsec int64) Value {
t, ok := mkTime(year, m, day, hour, min, sec, nsec, time.UTC)
if ok {
t = t.In(time.Local)
return d.setTimeMs(timeToMsec(t))
}
d.unset()
return _NaN
}

func (d *dateObject) setTimeMs(ms int64) Value {
if ms >= 0 && ms <= maxTime || ms < 0 && ms >= -maxTime {
d.msec = ms
Expand Down
21 changes: 21 additions & 0 deletions ftoa/LICENSE_LUCENE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Copyright (C) 1998, 1999 by Lucent Technologies
All Rights Reserved

Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of Lucent or any of its entities
not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.

LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
22 changes: 12 additions & 10 deletions ftoa/common.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
/*
Package ftoa provides ECMAScript-compliant floating point number conversion to string.
It contains code ported from Rhino (https://github.com/mozilla/rhino/blob/master/src/org/mozilla/javascript/DToA.java)
as well as from the original code by David M. Gay.
See LICENSE_LUCENE for the original copyright message and disclaimer.
*/
package ftoa

import (
"math"
"math/big"
)

const (
Expand Down Expand Up @@ -97,7 +101,7 @@ func stuffBits(bits []byte, offset int, val uint32) {
bits[offset+3] = byte(val)
}

func d2b(d float64, bi *big.Int) (e, bits int) {
func d2b(d float64, b []byte) (e, bits int, dblBits []byte) {
dBits := math.Float64bits(d)
d0 := uint32(dBits >> 32)
d1 := uint32(dBits)
Expand All @@ -106,33 +110,32 @@ func d2b(d float64, bi *big.Int) (e, bits int) {
d0 &= 0x7fffffff /* clear sign bit, which we ignore */

var de, k, i int
var dbl_bits []byte
if de = int(d0 >> exp_shift); de != 0 {
z |= exp_msk1
}

y := d1
if y != 0 {
dbl_bits = make([]byte, 8)
dblBits = b[:8]
k = lo0bits(y)
y >>= k
if k != 0 {
stuffBits(dbl_bits, 4, y|z<<(32-k))
stuffBits(dblBits, 4, y|z<<(32-k))
z >>= k
} else {
stuffBits(dbl_bits, 4, y)
stuffBits(dblBits, 4, y)
}
stuffBits(dbl_bits, 0, z)
stuffBits(dblBits, 0, z)
if z != 0 {
i = 2
} else {
i = 1
}
} else {
dbl_bits = make([]byte, 4)
dblBits = b[:4]
k = lo0bits(z)
z >>= k
stuffBits(dbl_bits, 0, z)
stuffBits(dblBits, 0, z)
k += 32
i = 1
}
Expand All @@ -144,6 +147,5 @@ func d2b(d float64, bi *big.Int) (e, bits int) {
e = de - bias - (p - 1) + 1 + k
bits = 32*i - hi0bits(z)
}
bi.SetBytes(dbl_bits)
return
}
Loading

0 comments on commit 351feaf

Please sign in to comment.