...

Source file src/github.com/bytedance/sonic/internal/native/avx2/fastfloat_amd64_test.go

Documentation: github.com/bytedance/sonic/internal/native/avx2

     1  // Code generated by Makefile, DO NOT EDIT.
     2  
     3  // Code generated by Makefile, DO NOT EDIT.
     4  
     5  /*
     6   * Copyright 2021 ByteDance Inc.
     7   *
     8   * Licensed under the Apache License, Version 2.0 (the "License");
     9   * you may not use this file except in compliance with the License.
    10   * You may obtain a copy of the License at
    11   *
    12   *     http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing, software
    15   * distributed under the License is distributed on an "AS IS" BASIS,
    16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17   * See the License for the specific language governing permissions and
    18   * limitations under the License.
    19   */
    20  
    21  package avx2
    22  
    23  import (
    24      `encoding/json`
    25      `math`
    26      `math/rand`
    27      `strconv`
    28      `testing`
    29  
    30      `github.com/stretchr/testify/assert`
    31  )
    32  
    33  func TestFastFloat_Encode(t *testing.T) {
    34      var buf [64]byte
    35      assert.Equal(t, "0"                         , string(buf[:f64toa(&buf[0], 0)]))
    36      assert.Equal(t, "-0"                         , string(buf[:f64toa(&buf[0], math.Float64frombits(0x8000000000000000))]))
    37      assert.Equal(t, "12340000000"               , string(buf[:f64toa(&buf[0], 1234e7)]))
    38      assert.Equal(t, "12.34"                     , string(buf[:f64toa(&buf[0], 1234e-2)]))
    39      assert.Equal(t, "0.001234"                  , string(buf[:f64toa(&buf[0], 1234e-6)]))
    40      assert.Equal(t, "1e+30"                      , string(buf[:f64toa(&buf[0], 1e30)]))
    41      assert.Equal(t, "1.234e+33"                  , string(buf[:f64toa(&buf[0], 1234e30)]))
    42      assert.Equal(t, "1.234e+308"                 , string(buf[:f64toa(&buf[0], 1234e305)]))
    43      assert.Equal(t, "1.234e-317"                , string(buf[:f64toa(&buf[0], 1234e-320)]))
    44      assert.Equal(t, "1.7976931348623157e+308"    , string(buf[:f64toa(&buf[0], 1.7976931348623157e308)]))
    45      assert.Equal(t, "-12340000000"              , string(buf[:f64toa(&buf[0], -1234e7)]))
    46      assert.Equal(t, "-12.34"                    , string(buf[:f64toa(&buf[0], -1234e-2)]))
    47      assert.Equal(t, "-0.001234"                 , string(buf[:f64toa(&buf[0], -1234e-6)]))
    48      assert.Equal(t, "-1e+30"                     , string(buf[:f64toa(&buf[0], -1e30)]))
    49      assert.Equal(t, "-1.234e+33"                 , string(buf[:f64toa(&buf[0], -1234e30)]))
    50      assert.Equal(t, "-1.234e+308"                , string(buf[:f64toa(&buf[0], -1234e305)]))
    51      assert.Equal(t, "-1.234e-317"               , string(buf[:f64toa(&buf[0], -1234e-320)]))
    52      assert.Equal(t, "-2.2250738585072014e-308"  , string(buf[:f64toa(&buf[0], -2.2250738585072014e-308)]))
    53  }
    54  
    55  func TestFastFloat_Random(t *testing.T) {
    56      var buf [64]byte
    57      N := 10000
    58      for i := 0; i < N; i++ {
    59          b64 := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
    60          f64 := math.Float64frombits(b64)
    61  
    62          jout, jerr := json.Marshal(f64)
    63          n := f64toa(&buf[0], f64)
    64          if jerr == nil {
    65              assert.Equal(t, jout, buf[:n])
    66          } else {
    67              assert.True(t, n == 0)
    68          }
    69  
    70          f32 := math.Float32frombits(rand.Uint32())
    71          jout, jerr = json.Marshal(f32)
    72          n = f32toa(&buf[0], f32)
    73          if jerr == nil {
    74              assert.Equal(t, jout, buf[:n])
    75          } else {
    76              assert.True(t, n == 0)
    77          }
    78      }
    79  }
    80  
    81  func BenchmarkParseFloat64(b *testing.B) {
    82      var f64toaBenches = []struct {
    83          name    string
    84          float   float64
    85      }{
    86          {"Zero", 0},
    87          {"Decimal", 33909},
    88          {"Float", 339.7784},
    89          {"Exp", -5.09e75},
    90          {"NegExp", -5.11e-95},
    91          {"LongExp", 1.234567890123456e-78},
    92          {"Big", 123456789123456789123456789},
    93      
    94      }
    95      for _, c := range f64toaBenches {
    96          f64bench := []struct {
    97              name string
    98              test func(*testing.B)
    99          }{{
   100              name: "StdLib",
   101              test: func(b *testing.B) { var buf [64]byte; for i := 0; i < b.N; i++ { strconv.AppendFloat(buf[:0], c.float, 'g', -1, 64) }},
   102          }, {
   103              name: "FastFloat",
   104              test: func(b *testing.B) { var buf [64]byte; for i := 0; i < b.N; i++ { f64toa(&buf[0], c.float) }},
   105          }}
   106          for _, bm := range f64bench {
   107              name := bm.name + "_" + c.name
   108              b.Run(name, bm.test)
   109          }
   110      }
   111  }
   112  
   113  func BenchmarkParseFloat32(b *testing.B) {
   114      var f32toaBenches = []struct {
   115          name    string
   116          float   float32
   117      }{
   118          {"Zero", 0},
   119          {"Integer", 33909},
   120          {"ExactFraction", 3.375},
   121          {"Point", 339.7784},
   122          {"Exp", -5.09e25},
   123          {"NegExp", -5.11e-25},
   124          {"Shortest", 1.234567e-8},
   125      }
   126      for _, c := range f32toaBenches {
   127          bench := []struct {
   128              name string
   129              test func(*testing.B)
   130          }{{
   131              name: "StdLib32",
   132              test: func(b *testing.B) { var buf [64]byte; for i := 0; i < b.N; i++ { strconv.AppendFloat(buf[:0], float64(c.float), 'g', -1, 32) }},
   133          }, {
   134              name: "FastFloat32",
   135              test: func(b *testing.B) { var buf [64]byte; for i := 0; i < b.N; i++ { f32toa(&buf[0], c.float) }},
   136          }}
   137          for _, bm := range bench {
   138              name := bm.name + "_" + c.name
   139              b.Run(name, bm.test)
   140          }
   141      }
   142  }
   143  

View as plain text