gitee.com/quant1x/num@v0.3.2/internal/functions/special_test.go (about)

     1  package functions
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/num/internal/rand"
     6  	"gitee.com/quant1x/pkg/testify/require"
     7  	"slices"
     8  	"testing"
     9  )
    10  
    11  func TestSqrt(t *testing.T) {
    12  	rand.Seed(2)
    13  	for i := 0; i < 1000; i++ {
    14  		size := 1 + (i / 5)
    15  		{
    16  			r1 := Random[float64](size)
    17  			r2 := slices.Clone(r1)
    18  			Sqrt_Go_F64(r1)
    19  			Sqrt_AVX2_F64(r2)
    20  			require.InDeltaSlice(t, r1, r2, 0.001)
    21  		}
    22  		{
    23  			r1 := Random[float32](size)
    24  			r2 := slices.Clone(r1)
    25  			Sqrt_Go_F32(r1)
    26  			Sqrt_AVX2_F32(r2)
    27  			require.InDeltaSlice(t, r1, r2, 0.001)
    28  		}
    29  	}
    30  }
    31  
    32  func TestRound(t *testing.T) {
    33  	rand.Seed(2)
    34  	for i := 0; i < 1000; i++ {
    35  		size := 1 + (i / 5)
    36  		{
    37  			r1 := Random[float64](size)
    38  			r2 := slices.Clone(r1)
    39  			Round_Go_F64(r1)
    40  			Round_AVX2_F64(r2)
    41  			require.InDeltaSlice(t, r1, r2, 0.001)
    42  		}
    43  		{
    44  			r1 := Random[float32](size)
    45  			r2 := slices.Clone(r1)
    46  			Round_Go_F32(r1)
    47  			Round_AVX2_F32(r2)
    48  			require.InDeltaSlice(t, r1, r2, 0.001)
    49  		}
    50  		{
    51  			r1 := Random[float64](size)
    52  			r2 := slices.Clone(r1)
    53  			Floor_Go_F64(r1)
    54  			Floor_AVX2_F64(r2)
    55  			require.InDeltaSlice(t, r1, r2, 0.001)
    56  		}
    57  		{
    58  			r1 := Random[float32](size)
    59  			r2 := slices.Clone(r1)
    60  			Floor_Go_F32(r1)
    61  			Floor_AVX2_F32(r2)
    62  			require.InDeltaSlice(t, r1, r2, 0.001)
    63  		}
    64  		{
    65  			r1 := Random[float64](size)
    66  			r2 := slices.Clone(r1)
    67  			Ceil_Go_F64(r1)
    68  			Ceil_AVX2_F64(r2)
    69  			require.InDeltaSlice(t, r1, r2, 0.001)
    70  		}
    71  		{
    72  			r1 := Random[float32](size)
    73  			r2 := slices.Clone(r1)
    74  			Ceil_Go_F32(r1)
    75  			Ceil_AVX2_F32(r2)
    76  			require.InDeltaSlice(t, r1, r2, 0.001)
    77  		}
    78  	}
    79  }
    80  
    81  func TestPow(t *testing.T) {
    82  	rand.Seed(2)
    83  	for i := 0; i < 1000; i++ {
    84  		size := 1 + (i / 5)
    85  		{
    86  			x := Random[float64](size)
    87  			y := RandomRange[float64](-10, 10, size)
    88  			x1 := slices.Clone(x)
    89  			Pow_Go_F64(x, y)
    90  			Pow_AVX2_F64(x1, y)
    91  			require.InEpsilonSlice(t, x, x1, 0.001)
    92  		}
    93  		{
    94  			x := Random[float32](size)
    95  			y := RandomRange[float32](-10, 10, size)
    96  			x1 := slices.Clone(x)
    97  			Pow_Go_F32(x, y)
    98  			Pow_AVX2_F32(x1, y)
    99  			require.InEpsilonSlice(t, x, x1, 0.001)
   100  		}
   101  	}
   102  }
   103  
   104  func TestSinCos(t *testing.T) {
   105  	rand.Seed(2)
   106  	for i := 0; i < 1000; i++ {
   107  		size := 1 + (i / 5)
   108  		{
   109  			r1 := Random[float32](size)
   110  			r2 := slices.Clone(r1)
   111  			Sin_Go_F32(r1)
   112  			Sin_AVX2_F32(r2)
   113  			require.InDeltaSlice(t, r1, r2, 0.001)
   114  		}
   115  		{
   116  			r1 := Random[float32](size)
   117  			r2 := slices.Clone(r1)
   118  			Cos_Go_F32(r1)
   119  			Cos_AVX2_F32(r2)
   120  			require.InDeltaSlice(t, r1, r2, 0.001)
   121  		}
   122  		{
   123  			x := Random[float32](size)
   124  			r1Sin := make([]float32, size)
   125  			r1Cos := make([]float32, size)
   126  			r2Sin := make([]float32, size)
   127  			r2Cos := make([]float32, size)
   128  			SinCos_Go_F32(r1Sin, r1Cos, x)
   129  			SinCos_AVX2_F32(r2Sin, r2Cos, x)
   130  			require.InDeltaSlice(t, r1Sin, r2Sin, 0.001)
   131  			require.InDeltaSlice(t, r1Cos, r2Cos, 0.001)
   132  		}
   133  	}
   134  }
   135  
   136  func TestExpLog(t *testing.T) {
   137  	rand.Seed(2)
   138  	for i := 0; i < 1000; i++ {
   139  		size := 1 + (i / 5)
   140  		{
   141  			r1 := make([]float32, size)
   142  			for j := 0; j < size; j++ {
   143  				r1[j] = float32(rand.Float64()*20 - 10)
   144  			}
   145  			r2 := slices.Clone(r1)
   146  			Exp_Go_F32(r1)
   147  			Exp_AVX2_F32(r2)
   148  			require.InEpsilonSlice(t, r1, r2, 0.001)
   149  		}
   150  		{
   151  			r1 := Random[float32](size)
   152  			r2 := slices.Clone(r1)
   153  			Log_Go_F32(r1)
   154  			Log_AVX2_F32(r2)
   155  			require.InDeltaSlice(t, r1, r2, 0.001)
   156  		}
   157  		{
   158  			r1 := Random[float32](size)
   159  			r2 := slices.Clone(r1)
   160  			Log2_Go_F32(r1)
   161  			Log2_AVX2_F32(r2)
   162  			require.InDeltaSlice(t, r1, r2, 0.001)
   163  		}
   164  		{
   165  			r1 := Random[float32](size)
   166  			r2 := slices.Clone(r1)
   167  			Log10_Go_F32(r1)
   168  			Log10_AVX2_F32(r2)
   169  			require.InDeltaSlice(t, r1, r2, 0.001)
   170  		}
   171  	}
   172  }
   173  
   174  func BenchmarkPow(b *testing.B) {
   175  	for _, size := range sizes[3:] {
   176  		x := Random[float64](size)
   177  		y := RandomRange[float64](-10, 10, size)
   178  		x32 := Random[float32](size)
   179  		y32 := RandomRange[float32](-10, 10, size)
   180  
   181  		b.Run(fmt.Sprintf("go_f64_%d", size), func(b *testing.B) {
   182  			for i := 0; i < b.N; i++ {
   183  				b.StopTimer()
   184  				x := slices.Clone(x)
   185  				b.StartTimer()
   186  				Pow_Go_F64(x, y)
   187  			}
   188  		})
   189  		b.Run(fmt.Sprintf("go_f32_%d", size), func(b *testing.B) {
   190  			for i := 0; i < b.N; i++ {
   191  				b.StopTimer()
   192  				x32 := slices.Clone(x32)
   193  				b.StartTimer()
   194  				Pow_Go_F32(x32, y32)
   195  			}
   196  		})
   197  		b.Run(fmt.Sprintf("avx2_f64_%d", size), func(b *testing.B) {
   198  			for i := 0; i < b.N; i++ {
   199  				b.StopTimer()
   200  				x := slices.Clone(x)
   201  				b.StartTimer()
   202  				Pow_AVX2_F64(x, y)
   203  			}
   204  		})
   205  		b.Run(fmt.Sprintf("avx2_f32_%d", size), func(b *testing.B) {
   206  			for i := 0; i < b.N; i++ {
   207  				b.StopTimer()
   208  				x32 := slices.Clone(x32)
   209  				b.StartTimer()
   210  				Pow_AVX2_F32(x32, y32)
   211  			}
   212  		})
   213  	}
   214  }
   215  
   216  func BenchmarkSqrt(b *testing.B) {
   217  	for _, size := range sizes[3:] {
   218  		x := Random[float64](size)
   219  		x32 := Random[float32](size)
   220  
   221  		b.Run(fmt.Sprintf("go_f64_%d", size), func(b *testing.B) {
   222  			for i := 0; i < b.N; i++ {
   223  				b.StopTimer()
   224  				x := slices.Clone(x)
   225  				b.StartTimer()
   226  				Sqrt_Go_F64(x)
   227  			}
   228  		})
   229  		b.Run(fmt.Sprintf("go_f32_%d", size), func(b *testing.B) {
   230  			for i := 0; i < b.N; i++ {
   231  				b.StopTimer()
   232  				x32 := slices.Clone(x32)
   233  				b.StartTimer()
   234  				Sqrt_Go_F32(x32)
   235  			}
   236  		})
   237  		b.Run(fmt.Sprintf("avx2_f64_%d", size), func(b *testing.B) {
   238  			for i := 0; i < b.N; i++ {
   239  				b.StopTimer()
   240  				x := slices.Clone(x)
   241  				b.StartTimer()
   242  				Sqrt_AVX2_F64(x)
   243  			}
   244  		})
   245  		b.Run(fmt.Sprintf("avx2_f32_%d", size), func(b *testing.B) {
   246  			for i := 0; i < b.N; i++ {
   247  				b.StopTimer()
   248  				x32 := slices.Clone(x32)
   249  				b.StartTimer()
   250  				Sqrt_AVX2_F32(x32)
   251  			}
   252  		})
   253  	}
   254  }
   255  
   256  func BenchmarkRound(b *testing.B) {
   257  	for _, size := range sizes[3:] {
   258  		x := Random[float64](size)
   259  		x32 := Random[float32](size)
   260  
   261  		b.Run(fmt.Sprintf("go_f64_%d", size), func(b *testing.B) {
   262  			for i := 0; i < b.N; i++ {
   263  				b.StopTimer()
   264  				x := slices.Clone(x)
   265  				b.StartTimer()
   266  				Round_Go_F64(x)
   267  			}
   268  		})
   269  		b.Run(fmt.Sprintf("go_f32_%d", size), func(b *testing.B) {
   270  			for i := 0; i < b.N; i++ {
   271  				b.StopTimer()
   272  				x32 := slices.Clone(x32)
   273  				b.StartTimer()
   274  				Round_Go_F32(x32)
   275  			}
   276  		})
   277  		b.Run(fmt.Sprintf("avx2_f64_%d", size), func(b *testing.B) {
   278  			for i := 0; i < b.N; i++ {
   279  				b.StopTimer()
   280  				x := slices.Clone(x)
   281  				b.StartTimer()
   282  				Round_AVX2_F64(x)
   283  			}
   284  		})
   285  		b.Run(fmt.Sprintf("avx2_f32_%d", size), func(b *testing.B) {
   286  			for i := 0; i < b.N; i++ {
   287  				b.StopTimer()
   288  				x32 := slices.Clone(x32)
   289  				b.StartTimer()
   290  				Round_AVX2_F32(x32)
   291  			}
   292  		})
   293  	}
   294  }
   295  
   296  func BenchmarkSinCos(b *testing.B) {
   297  	for _, size := range sizes[3:] {
   298  		x32 := Random[float32](size)
   299  
   300  		b.Run(fmt.Sprintf("go_f32_%d", size), func(b *testing.B) {
   301  			for i := 0; i < b.N; i++ {
   302  				b.StopTimer()
   303  				x32 := slices.Clone(x32)
   304  				b.StartTimer()
   305  				Sin_Go_F32(x32)
   306  			}
   307  		})
   308  		b.Run(fmt.Sprintf("avx2_f32_%d", size), func(b *testing.B) {
   309  			for i := 0; i < b.N; i++ {
   310  				b.StopTimer()
   311  				x32 := slices.Clone(x32)
   312  				b.StartTimer()
   313  				Sin_AVX2_F32(x32)
   314  			}
   315  		})
   316  	}
   317  }
   318  
   319  func BenchmarkExpLog(b *testing.B) {
   320  	for _, size := range sizes[3:] {
   321  		x32 := Random[float32](size)
   322  
   323  		b.Run(fmt.Sprintf("go_exp_f32_%d", size), func(b *testing.B) {
   324  			for i := 0; i < b.N; i++ {
   325  				b.StopTimer()
   326  				x32 := slices.Clone(x32)
   327  				b.StartTimer()
   328  				Exp_Go_F32(x32)
   329  			}
   330  		})
   331  		b.Run(fmt.Sprintf("avx2_exp_f32_%d", size), func(b *testing.B) {
   332  			for i := 0; i < b.N; i++ {
   333  				b.StopTimer()
   334  				x32 := slices.Clone(x32)
   335  				b.StartTimer()
   336  				Exp_AVX2_F32(x32)
   337  			}
   338  		})
   339  		b.Run(fmt.Sprintf("go_log_f32_%d", size), func(b *testing.B) {
   340  			for i := 0; i < b.N; i++ {
   341  				b.StopTimer()
   342  				x32 := slices.Clone(x32)
   343  				b.StartTimer()
   344  				Log_Go_F32(x32)
   345  			}
   346  		})
   347  		b.Run(fmt.Sprintf("avx2_log_f32_%d", size), func(b *testing.B) {
   348  			for i := 0; i < b.N; i++ {
   349  				b.StopTimer()
   350  				x32 := slices.Clone(x32)
   351  				b.StartTimer()
   352  				Log_AVX2_F32(x32)
   353  			}
   354  		})
   355  	}
   356  }