github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/audio/benchmark/mix.go (about)

     1  package benchmark_audio
     2  
     3  import "unsafe"
     4  
     5  func Mix_Dynamic(dst, a, b []float32) {
     6  	n := len(a)
     7  	if n > len(b) {
     8  		n = len(b)
     9  	}
    10  	if n > len(dst) {
    11  		n = len(dst)
    12  	}
    13  	for i := 0; i < n; i++ {
    14  		dst[i] = a[i] + b[i]
    15  	}
    16  }
    17  
    18  func Mix_Baseline(dst, a, b []float32) {
    19  	n := len(a)
    20  	if n > len(b) {
    21  		n = len(b)
    22  	}
    23  	if n > len(dst) {
    24  		n = len(dst)
    25  	}
    26  
    27  	pdst := (uintptr)(unsafe.Pointer(&dst[0]))
    28  	pend := (uintptr)(unsafe.Pointer(&dst[len(dst)-1])) + 4
    29  	pa := (uintptr)(unsafe.Pointer(&a[0]))
    30  	pb := (uintptr)(unsafe.Pointer(&b[0]))
    31  	for pdst < pend {
    32  		*(*float32)(unsafe.Pointer(pdst)) =
    33  			*(*float32)(unsafe.Pointer(pa)) +
    34  				*(*float32)(unsafe.Pointer(pb))
    35  		pdst += 4
    36  		pa += 4
    37  		pb += 4
    38  	}
    39  }
    40  
    41  func MixInto_Dynamic(a, b []float32) {
    42  	n := len(a)
    43  	if n > len(b) {
    44  		n = len(b)
    45  	}
    46  	for i := 0; i < n; i++ {
    47  		a[i] += b[i]
    48  	}
    49  }
    50  
    51  func MixInto_Baseline(a, b []float32) {
    52  	n := len(a)
    53  	if n > len(b) {
    54  		n = len(b)
    55  	}
    56  	pa := (uintptr)(unsafe.Pointer(&a[0]))
    57  	pend := (uintptr)(unsafe.Pointer(&a[len(a)-1])) + 4
    58  	pb := (uintptr)(unsafe.Pointer(&b[0]))
    59  	for pa < pend {
    60  		*(*float32)(unsafe.Pointer(pa)) += *(*float32)(unsafe.Pointer(pb))
    61  		pa += 4
    62  		pb += 4
    63  	}
    64  }