/////////////////////////
// freqAnalyzer_old.cu // 
// Andrew Krepps       //
// Module 9 Assignment //
// 4/9/2018            //
/////////////////////////

#include <chrono>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include <cufft.h>

///////////////////////////////////////////////////////////////////////////////
/// \brief find the magnitude of each frequency bin from FFT results
/// 
/// \param [in] in the complex frequency-domain signal
/// \param [out] out the magnitude of each frequency bin
/// \param [in] n the number of samples
///////////////////////////////////////////////////////////////////////////////
__global__ void calcMagnitudes(const cufftComplex* in, float* out, const unsigned int n)
{
	const unsigned int idx = blockIdx.x*blockDim.x + threadIdx.x;
	
	if (idx < n) {
		out[idx] = cuCabsf(in[idx]);
	}
}

///////////////////////////////////////////////////////////////////////////////
/// \brief run an FFT on a real signal and find the magnitude of each frequency
/// bin
/// 
/// \param [in] timeSignal the real time-domain signal
/// \param [out] freqSignalMagnitudes the magnitude of each frequency bin
/// \param [in] n the number of samples
/// \param [in] blockSize the number of threads per block
///////////////////////////////////////////////////////////////////////////////
void runFFT(const float* timeSignal, float* freqSignalMagnitudes, const unsigned int n, const unsigned int blockSize)
{
	// create FFT plan
	cufftHandle plan;
	cufftPlan1d(&plan, n, CUFFT_R2C, 1);
	
	// allocate device memory
	cufftReal* d_timeSignal;
	cufftComplex* d_freqSignal;
	float* d_freqSignalMagnitudes;
	
	const unsigned int realBytes = n*sizeof(cufftReal);
	const unsigned int complexBytes = n*sizeof(cufftComplex);
	cudaMalloc((void**)&d_timeSignal, realBytes);
	cudaMalloc((void**)&d_freqSignal, complexBytes);
	cudaMalloc((void**)&d_freqSignalMagnitudes, realBytes);
	
	// copy input data to device
	cudaMemcpy(d_timeSignal, timeSignal, realBytes, cudaMemcpyHostToDevice);
	
	// perform FFT
	cufftExecR2C(plan, d_timeSignal, d_freqSignal);
	cudaDeviceSynchronize();

	// find magnitudes of frequency signal
	const unsigned int numBlocks = n/blockSize;
	calcMagnitudes<<<numBlocks, blockSize>>>(d_freqSignal, d_freqSignalMagnitudes, n);
	
	// copy output data to host
	cudaMemcpy(freqSignalMagnitudes, d_freqSignalMagnitudes, realBytes, cudaMemcpyDeviceToHost);
	
	// free device memory
	cudaFree(d_timeSignal);
	cudaFree(d_freqSignal);
	cudaFree(d_freqSignalMagnitudes);
}

///////////////////////////////////////////////////////////////////////////////
/// \brief initialize input data on the host
/// 
/// \param [out] timeSignal the time-domain input signal
/// \param [in] frequency the signal frequency (in Hz)
/// \param [in] samplingRate the signal sampling rate (in Hz)
/// \param [in] n the number of samples
///////////////////////////////////////////////////////////////////////////////
void initializeInputData(float* timeSignal, float frequency, float samplingRate, const unsigned int n)
{
	printf("Generating %f Hz signal\n", frequency);
	for (unsigned int i = 0; i < n; ++i) {
		timeSignal[i] = sin(2.0f*M_PI*frequency*i/samplingRate);
	}
}

///////////////////////////////////////////////////////////////////////////////
/// \brief extract strongest frequency bin from FFT results
/// 
/// \param [in] freqSignalMagnitudes the magnitude of each frequency bin
/// \param [in] samplingRate the signal sampling rate (in Hz)
/// \param [in] n the number of samples
/// 
/// \returns the frequency (in Hz) of the bin with the largest magnitude
///////////////////////////////////////////////////////////////////////////////
float extractFrequency(const float* freqSignalMagnitudes, float samplingRate, const unsigned int n)
{
	// find frequency bin with highest magnitude
	unsigned int maxIdx = 0;
	float maxVal = 0.0f;
	
	// since input was a real signal, we only need to consider the first half of the output
	for (unsigned int i = 0; i < n/2; ++i) {
		if (freqSignalMagnitudes[i] > maxVal) {
			maxVal = freqSignalMagnitudes[i];
			maxIdx = i;
		}
	}
	
	// calculate frequency of selected bin
	return maxIdx*samplingRate/n;
}

int main(int argc, char** argv)
{
	// configure run
	unsigned int dataSize = 512;
	unsigned int blockSize = 256;
	float frequency = 128.0f;
	float samplingRate = 512.0f;
	if (argc > 1) {
		dataSize = atoi(argv[1]);
	}
	if (argc > 2) {
		blockSize = atoi(argv[2]);
	}
	if (argc > 3) {
		frequency = atof(argv[3]);
	}
	if (argc > 4) {
		samplingRate = atof(argv[4]);
	}
	
	// allocate and initialize host memory
	const unsigned int numBytes = dataSize*sizeof(float);
	float* timeSignal = (float*)malloc(numBytes);
	float* freqSignalMagnitudes = (float*)malloc(numBytes);
	initializeInputData(timeSignal, frequency, samplingRate, dataSize);
	
	// dummy execution to avoid startup cost	
	runFFT(timeSignal, freqSignalMagnitudes, dataSize, blockSize);
	
	// measure total exectution time (including serial frequency extraction from FFT results on host)
	auto start = std::chrono::high_resolution_clock::now();
	
	// run FFT and extract dominant frequency
	runFFT(timeSignal, freqSignalMagnitudes, dataSize, blockSize);
	float foundFrequency = extractFrequency(freqSignalMagnitudes, samplingRate, dataSize);
	
	// stop clock and calculate time in ms
	auto stop = std::chrono::high_resolution_clock::now();	
	std::chrono::duration<float> duration(stop - start);
	float ms = duration.count()*1000.0f;
	
	// show results
	printf("Strongest frequency bin was %f Hz (execution time = %.3f ms)\n", foundFrequency, ms);
	
	// free host memory
	free(timeSignal);
	free(freqSignalMagnitudes);
}

	.file	"tmpxft_00208f9a_00000000-6_freqAnalyzer_old.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2159:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2159:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"Generating %f Hz signal\n"
	.text
	.globl	_Z19initializeInputDataPfffj
	.type	_Z19initializeInputDataPfffj, @function
_Z19initializeInputDataPfffj:
.LFB2150:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	cvtss2sd	%xmm0, %xmm2
	movb	$1, %al
	movaps	%xmm2, %xmm0
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	movd	%xmm1, %r14d
	xorl	%r12d, %r12d
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	movq	%rdi, %rbp
	movl	$2, %edi
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	movl	%esi, %ebx
	leaq	.LC0(%rip), %rsi
	subq	$24, %rsp
	.cfi_def_cfa_offset 64
	movsd	%xmm2, 8(%rsp)
	call	__printf_chk@PLT
.L3:
	cmpq	%rbx, %r12
	je	.L9
	movl	%r12d, %eax
	movsd	.LC1(%rip), %xmm0
	movd	%r14d, %xmm4
	mulsd	8(%rsp), %xmm0
	cvtsi2sdq	%rax, %xmm1
	mulsd	%xmm1, %xmm0
	cvtss2sd	%xmm4, %xmm1
	divsd	%xmm1, %xmm0
	call	sin@PLT
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, 0(%rbp,%r12,4)
	incq	%r12
	jmp	.L3
.L9:
	addq	$24, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2150:
	.size	_Z19initializeInputDataPfffj, .-_Z19initializeInputDataPfffj
	.globl	_Z16extractFrequencyPKffj
	.type	_Z16extractFrequencyPKffj, @function
_Z16extractFrequencyPKffj:
.LFB2151:
	.cfi_startproc
	endbr64
	movl	%esi, %r8d
	xorl	%edx, %edx
	xorps	%xmm1, %xmm1
	xorl	%eax, %eax
	shrl	%r8d
.L11:
	movl	%edx, %ecx
	cmpl	%r8d, %edx
	jnb	.L18
	movss	(%rdi,%rdx,4), %xmm2
	comiss	%xmm1, %xmm2
	maxss	%xmm1, %xmm2
	cmovbe	%eax, %ecx
	movaps	%xmm2, %xmm1
	incq	%rdx
	movl	%ecx, %eax
	jmp	.L11
.L18:
	cvtsi2ssq	%rax, %xmm1
	movl	%esi, %esi
	mulss	%xmm1, %xmm0
	cvtsi2ssq	%rsi, %xmm1
	divss	%xmm1, %xmm0
	ret
	.cfi_endproc
.LFE2151:
	.size	_Z16extractFrequencyPKffj, .-_Z16extractFrequencyPKffj
	.globl	_Z44__device_stub__Z14calcMagnitudesPK6float2PfjPK6float2Pfj
	.type	_Z44__device_stub__Z14calcMagnitudesPK6float2PfjPK6float2Pfj, @function
_Z44__device_stub__Z14calcMagnitudesPK6float2PfjPK6float2Pfj:
.LFB2181:
	.cfi_startproc
	endbr64
	subq	$136, %rsp
	.cfi_def_cfa_offset 144
	movq	%rdi, 24(%rsp)
	leaq	40(%rsp), %rcx
	leaq	48(%rsp), %rdi
	movq	%rsi, 16(%rsp)
	leaq	60(%rsp), %rsi
	movl	%edx, 12(%rsp)
	leaq	32(%rsp), %rdx
	movq	%fs:40, %rax
	movq	%rax, 120(%rsp)
	xorl	%eax, %eax
	leaq	24(%rsp), %rax
	movl	$1, 56(%rsp)
	movq	%rax, 96(%rsp)
	leaq	16(%rsp), %rax
	movq	%rax, 104(%rsp)
	leaq	12(%rsp), %rax
	movq	%rax, 112(%rsp)
	movabsq	$4294967297, %rax
	movq	%rax, 48(%rsp)
	movq	%rax, 60(%rsp)
	movl	$1, 68(%rsp)
	call	__cudaPopCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L19
	pushq	40(%rsp)
	.cfi_def_cfa_offset 152
	leaq	_Z14calcMagnitudesPK6float2Pfj(%rip), %rdi
	pushq	40(%rsp)
	.cfi_def_cfa_offset 160
	movq	76(%rsp), %rcx
	movl	84(%rsp), %r8d
	movq	64(%rsp), %rsi
	movl	72(%rsp), %edx
	leaq	112(%rsp), %r9
	call	cudaLaunchKernel@PLT
	popq	%rax
	.cfi_def_cfa_offset 152
	popq	%rdx
	.cfi_def_cfa_offset 144
.L19:
	movq	120(%rsp), %rax
	subq	%fs:40, %rax
	je	.L21
	call	__stack_chk_fail@PLT
.L21:
	addq	$136, %rsp
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2181:
	.size	_Z44__device_stub__Z14calcMagnitudesPK6float2PfjPK6float2Pfj, .-_Z44__device_stub__Z14calcMagnitudesPK6float2PfjPK6float2Pfj
	.globl	_Z14calcMagnitudesPK6float2Pfj
	.type	_Z14calcMagnitudesPK6float2Pfj, @function
_Z14calcMagnitudesPK6float2Pfj:
.LFB2182:
	.cfi_startproc
	endbr64
	jmp	_Z44__device_stub__Z14calcMagnitudesPK6float2PfjPK6float2Pfj
	.cfi_endproc
.LFE2182:
	.size	_Z14calcMagnitudesPK6float2Pfj, .-_Z14calcMagnitudesPK6float2Pfj
	.globl	_Z6runFFTPKfPfjj
	.type	_Z6runFFTPKfPfjj, @function
_Z6runFFTPKfPfjj:
.LFB2149:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	movq	%rdi, %r14
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	movq	%rsi, %r13
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	movl	%ecx, %ebp
	movl	$1, %ecx
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	movl	%edx, %ebx
	movl	$42, %edx
	leal	0(,%rbx,4), %r12d
	movl	%ebx, %esi
	subq	$64, %rsp
	.cfi_def_cfa_offset 112
	movq	%fs:40, %rax
	movq	%rax, 56(%rsp)
	xorl	%eax, %eax
	leaq	4(%rsp), %rdi
	call	cufftPlan1d@PLT
	leaq	8(%rsp), %rdi
	movq	%r12, %rsi
	call	cudaMalloc@PLT
	leal	0(,%rbx,8), %esi
	leaq	16(%rsp), %rdi
	call	cudaMalloc@PLT
	leaq	24(%rsp), %rdi
	movq	%r12, %rsi
	call	cudaMalloc@PLT
	movq	8(%rsp), %rdi
	movq	%r12, %rdx
	movq	%r14, %rsi
	movl	$1, %ecx
	call	cudaMemcpy@PLT
	movq	16(%rsp), %rdx
	movq	8(%rsp), %rsi
	movl	4(%rsp), %edi
	call	cufftExecR2C@PLT
	call	cudaDeviceSynchronize@PLT
	xorl	%edx, %edx
	movl	%ebx, %eax
	movl	%ebp, 44(%rsp)
	movabsq	$4294967297, %rcx
	xorl	%r9d, %r9d
	xorl	%r8d, %r8d
	divl	%ebp
	movq	%rcx, 36(%rsp)
	movl	40(%rsp), %esi
	movq	%rcx, 48(%rsp)
	movl	52(%rsp), %ecx
	movq	44(%rsp), %rdx
	movl	%eax, 32(%rsp)
	movq	32(%rsp), %rdi
	call	__cudaPushCallConfiguration@PLT
	testl	%eax, %eax
	jne	.L25
	movq	24(%rsp), %rsi
	movq	16(%rsp), %rdi
	movl	%ebx, %edx
	call	_Z44__device_stub__Z14calcMagnitudesPK6float2PfjPK6float2Pfj
.L25:
	movq	24(%rsp), %rsi
	movl	$2, %ecx
	movq	%r12, %rdx
	movq	%r13, %rdi
	call	cudaMemcpy@PLT
	movq	8(%rsp), %rdi
	call	cudaFree@PLT
	movq	16(%rsp), %rdi
	call	cudaFree@PLT
	movq	24(%rsp), %rdi
	call	cudaFree@PLT
	movq	56(%rsp), %rax
	subq	%fs:40, %rax
	je	.L26
	call	__stack_chk_fail@PLT
.L26:
	addq	$64, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2149:
	.size	_Z6runFFTPKfPfjj, .-_Z6runFFTPKfPfjj
	.section	.rodata.str1.1
.LC7:
	.string	"Strongest frequency bin was %f Hz (execution time = %.3f ms)\n"
	.section	.text.startup,"ax",@progbits
	.globl	main
	.type	main, @function
main:
.LFB2152:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	subq	$16, %rsp
	.cfi_def_cfa_offset 64
	cmpl	$1, %edi
	jle	.L30
	movl	%edi, %ebp
	movq	8(%rsi), %rdi
	movq	%rsi, %r12
	call	atoi@PLT
	movl	%eax, %ebx
	cmpl	$2, %ebp
	je	.L31
	movq	16(%r12), %rdi
	call	atoi@PLT
	movl	%eax, %r13d
	cmpl	$3, %ebp
	je	.L32
	movq	24(%r12), %rdi
	call	atof@PLT
	movss	.LC3(%rip), %xmm1
	cvtsd2ss	%xmm0, %xmm2
	cmpl	$4, %ebp
	je	.L29
	movq	32(%r12), %rdi
	movss	%xmm2, 8(%rsp)
	call	atof@PLT
	movss	8(%rsp), %xmm2
	cvtsd2ss	%xmm0, %xmm1
	jmp	.L29
.L30:
	movss	.LC4(%rip), %xmm2
	movl	$256, %r13d
	movl	$512, %ebx
	jmp	.L35
.L31:
	movss	.LC4(%rip), %xmm2
	movl	$256, %r13d
	jmp	.L35
.L32:
	movss	.LC4(%rip), %xmm2
.L35:
	movss	.LC3(%rip), %xmm1
.L29:
	leal	0(,%rbx,4), %ebp
	movss	%xmm2, 12(%rsp)
	movq	%rbp, %rdi
	movss	%xmm1, 8(%rsp)
	call	malloc@PLT
	movq	%rbp, %rdi
	movq	%rax, %r12
	call	malloc@PLT
	movss	12(%rsp), %xmm2
	movl	%ebx, %esi
	movq	%r12, %rdi
	movss	8(%rsp), %xmm1
	movq	%rax, %rbp
	movaps	%xmm2, %xmm0
	call	_Z19initializeInputDataPfffj
	movl	%r13d, %ecx
	movl	%ebx, %edx
	movq	%rbp, %rsi
	movq	%r12, %rdi
	call	_Z6runFFTPKfPfjj
	call	_ZNSt6chrono3_V212system_clock3nowEv@PLT
	movl	%r13d, %ecx
	movl	%ebx, %edx
	movq	%rbp, %rsi
	movq	%r12, %rdi
	movq	%rax, %r14
	call	_Z6runFFTPKfPfjj
	movss	8(%rsp), %xmm1
	movl	%ebx, %esi
	movq	%rbp, %rdi
	movaps	%xmm1, %xmm0
	call	_Z16extractFrequencyPKffj
	movss	%xmm0, 8(%rsp)
	call	_ZNSt6chrono3_V212system_clock3nowEv@PLT
	movss	8(%rsp), %xmm0
	movl	$2, %edi
	leaq	.LC7(%rip), %rsi
	subq	%r14, %rax
	cvtsi2ssq	%rax, %xmm1
	divss	.LC5(%rip), %xmm1
	movb	$2, %al
	cvtss2sd	%xmm0, %xmm0
	mulss	.LC6(%rip), %xmm1
	cvtss2sd	%xmm1, %xmm1
	call	__printf_chk@PLT
	movq	%r12, %rdi
	call	free@PLT
	movq	%rbp, %rdi
	call	free@PLT
	addq	$16, %rsp
	.cfi_def_cfa_offset 48
	xorl	%eax, %eax
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2152:
	.size	main, .-main
	.section	.rodata.str1.1
.LC8:
	.string	"_Z14calcMagnitudesPK6float2Pfj"
	.section	.text.startup
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2184:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	pushq	$0
	.cfi_def_cfa_offset 24
	xorl	%r9d, %r9d
	orl	$-1, %r8d
	pushq	$0
	.cfi_def_cfa_offset 32
	leaq	.LC8(%rip), %rdx
	movq	%rax, %rdi
	leaq	_Z14calcMagnitudesPK6float2Pfj(%rip), %rsi
	pushq	$0
	.cfi_def_cfa_offset 40
	movq	%rdx, %rcx
	pushq	$0
	.cfi_def_cfa_offset 48
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFunction@PLT
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	addq	$32, %rsp
	.cfi_def_cfa_offset 16
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2184:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LC1:
	.long	1413754136
	.long	1075388923
	.section	.rodata.cst4,"aM",@progbits,4
	.align 4
.LC3:
	.long	1140850688
	.align 4
.LC4:
	.long	1124073472
	.align 4
.LC5:
	.long	1315859240
	.align 4
.LC6:
	.long	1148846080
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

/////////////////////////
// freqAnalyzer_old.cu // 
// Andrew Krepps       //
// Module 9 Assignment //
// 4/9/2018            //
/////////////////////////


#include <hip/hip_runtime.h>
#include <chrono>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include <hipfft/hipfft.h>

///////////////////////////////////////////////////////////////////////////////
/// \brief find the magnitude of each frequency bin from FFT results
/// 
/// \param [in] in the complex frequency-domain signal
/// \param [out] out the magnitude of each frequency bin
/// \param [in] n the number of samples
///////////////////////////////////////////////////////////////////////////////
__global__ void calcMagnitudes(const hipfftComplex* in, float* out, const unsigned int n)
{
	const unsigned int idx = blockIdx.x*blockDim.x + threadIdx.x;
	
	if (idx < n) {
		out[idx] = hipCabsf(in[idx]);
	}
}

///////////////////////////////////////////////////////////////////////////////
/// \brief run an FFT on a real signal and find the magnitude of each frequency
/// bin
/// 
/// \param [in] timeSignal the real time-domain signal
/// \param [out] freqSignalMagnitudes the magnitude of each frequency bin
/// \param [in] n the number of samples
/// \param [in] blockSize the number of threads per block
///////////////////////////////////////////////////////////////////////////////
void runFFT(const float* timeSignal, float* freqSignalMagnitudes, const unsigned int n, const unsigned int blockSize)
{
	// create FFT plan
	hipfftHandle plan;
	hipfftPlan1d(&plan, n, HIPFFT_R2C, 1);
	
	// allocate device memory
	hipfftReal* d_timeSignal;
	hipfftComplex* d_freqSignal;
	float* d_freqSignalMagnitudes;
	
	const unsigned int realBytes = n*sizeof(hipfftReal);
	const unsigned int complexBytes = n*sizeof(hipfftComplex);
	hipMalloc((void**)&d_timeSignal, realBytes);
	hipMalloc((void**)&d_freqSignal, complexBytes);
	hipMalloc((void**)&d_freqSignalMagnitudes, realBytes);
	
	// copy input data to device
	hipMemcpy(d_timeSignal, timeSignal, realBytes, hipMemcpyHostToDevice);
	
	// perform FFT
	hipfftExecR2C(plan, d_timeSignal, d_freqSignal);
	hipDeviceSynchronize();

	// find magnitudes of frequency signal
	const unsigned int numBlocks = n/blockSize;
	calcMagnitudes<<<numBlocks, blockSize>>>(d_freqSignal, d_freqSignalMagnitudes, n);
	
	// copy output data to host
	hipMemcpy(freqSignalMagnitudes, d_freqSignalMagnitudes, realBytes, hipMemcpyDeviceToHost);
	
	// free device memory
	hipFree(d_timeSignal);
	hipFree(d_freqSignal);
	hipFree(d_freqSignalMagnitudes);
}

///////////////////////////////////////////////////////////////////////////////
/// \brief initialize input data on the host
/// 
/// \param [out] timeSignal the time-domain input signal
/// \param [in] frequency the signal frequency (in Hz)
/// \param [in] samplingRate the signal sampling rate (in Hz)
/// \param [in] n the number of samples
///////////////////////////////////////////////////////////////////////////////
void initializeInputData(float* timeSignal, float frequency, float samplingRate, const unsigned int n)
{
	printf("Generating %f Hz signal\n", frequency);
	for (unsigned int i = 0; i < n; ++i) {
		timeSignal[i] = sin(2.0f*M_PI*frequency*i/samplingRate);
	}
}

///////////////////////////////////////////////////////////////////////////////
/// \brief extract strongest frequency bin from FFT results
/// 
/// \param [in] freqSignalMagnitudes the magnitude of each frequency bin
/// \param [in] samplingRate the signal sampling rate (in Hz)
/// \param [in] n the number of samples
/// 
/// \returns the frequency (in Hz) of the bin with the largest magnitude
///////////////////////////////////////////////////////////////////////////////
float extractFrequency(const float* freqSignalMagnitudes, float samplingRate, const unsigned int n)
{
	// find frequency bin with highest magnitude
	unsigned int maxIdx = 0;
	float maxVal = 0.0f;
	
	// since input was a real signal, we only need to consider the first half of the output
	for (unsigned int i = 0; i < n/2; ++i) {
		if (freqSignalMagnitudes[i] > maxVal) {
			maxVal = freqSignalMagnitudes[i];
			maxIdx = i;
		}
	}
	
	// calculate frequency of selected bin
	return maxIdx*samplingRate/n;
}

int main(int argc, char** argv)
{
	// configure run
	unsigned int dataSize = 512;
	unsigned int blockSize = 256;
	float frequency = 128.0f;
	float samplingRate = 512.0f;
	if (argc > 1) {
		dataSize = atoi(argv[1]);
	}
	if (argc > 2) {
		blockSize = atoi(argv[2]);
	}
	if (argc > 3) {
		frequency = atof(argv[3]);
	}
	if (argc > 4) {
		samplingRate = atof(argv[4]);
	}
	
	// allocate and initialize host memory
	const unsigned int numBytes = dataSize*sizeof(float);
	float* timeSignal = (float*)malloc(numBytes);
	float* freqSignalMagnitudes = (float*)malloc(numBytes);
	initializeInputData(timeSignal, frequency, samplingRate, dataSize);
	
	// dummy execution to avoid startup cost	
	runFFT(timeSignal, freqSignalMagnitudes, dataSize, blockSize);
	
	// measure total exectution time (including serial frequency extraction from FFT results on host)
	auto start = std::chrono::high_resolution_clock::now();
	
	// run FFT and extract dominant frequency
	runFFT(timeSignal, freqSignalMagnitudes, dataSize, blockSize);
	float foundFrequency = extractFrequency(freqSignalMagnitudes, samplingRate, dataSize);
	
	// stop clock and calculate time in ms
	auto stop = std::chrono::high_resolution_clock::now();	
	std::chrono::duration<float> duration(stop - start);
	float ms = duration.count()*1000.0f;
	
	// show results
	printf("Strongest frequency bin was %f Hz (execution time = %.3f ms)\n", foundFrequency, ms);
	
	// free host memory
	free(timeSignal);
	free(freqSignalMagnitudes);
}

	.text
	.file	"freqAnalyzer_old.hip"
	.globl	_Z29__device_stub__calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj # -- Begin function _Z29__device_stub__calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj
	.type	_Z29__device_stub__calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj,@function
_Z29__device_stub__calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj: # @_Z29__device_stub__calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj
	.cfi_startproc
# %bb.0:
	pushq	%r15
	.cfi_def_cfa_offset 16
	pushq	%r14
	.cfi_def_cfa_offset 24
	pushq	%r13
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$112, %rsp
	.cfi_def_cfa_offset 160
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r13, -32
	.cfi_offset %r14, -24
	.cfi_offset %r15, -16
	leaq	40(%rsp), %rax
	movq	%rdi, (%rax)
	leaq	32(%rsp), %rcx
	movq	%rsi, (%rcx)
	leaq	12(%rsp), %rsi
	movl	%edx, (%rsi)
	leaq	80(%rsp), %rbx
	movq	%rax, (%rbx)
	movq	%rcx, 8(%rbx)
	movq	%rsi, 16(%rbx)
	leaq	64(%rsp), %r14
	leaq	48(%rsp), %r15
	leaq	24(%rsp), %r12
	leaq	16(%rsp), %r13
	movq	%r14, %rdi
	movq	%r15, %rsi
	movq	%r12, %rdx
	movq	%r13, %rcx
	callq	__hipPopCallConfiguration
	movq	(%r14), %rsi
	movl	8(%r14), %edx
	movq	(%r15), %rcx
	movl	8(%r15), %r8d
	movl	$_Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj, %edi
	movq	%rbx, %r9
	pushq	(%r13)
	.cfi_adjust_cfa_offset 8
	pushq	(%r12)
	.cfi_adjust_cfa_offset 8
	callq	hipLaunchKernel
	addq	$128, %rsp
	.cfi_adjust_cfa_offset -128
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z29__device_stub__calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj, .Lfunc_end0-_Z29__device_stub__calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj
	.cfi_endproc
                                        # -- End function
	.globl	_Z6runFFTPKfPfjj                # -- Begin function _Z6runFFTPKfPfjj
	.type	_Z6runFFTPKfPfjj,@function
_Z6runFFTPKfPfjj:                       # @_Z6runFFTPKfPfjj
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$56, %rsp
	.cfi_def_cfa_offset 112
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%ecx, %ebp
	movl	%edx, %r14d
	movq	%rsi, 32(%rsp)                  # 8-byte Spill
	movq	%rdi, 24(%rsp)                  # 8-byte Spill
	leaq	48(%rsp), %rdi
	movl	%edx, %esi
	movl	$42, %edx
	movl	$1, %ecx
	callq	hipfftPlan1d
	leal	(,%r14,4), %r15d
	leal	(,%r14,8), %ebx
	leaq	40(%rsp), %r12
	movq	%r12, %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	leaq	16(%rsp), %r13
	movq	%r13, %rdi
	movq	%rbx, %rsi
	callq	hipMalloc
	leaq	8(%rsp), %rdi
	movq	%r15, %rsi
	callq	hipMalloc
	movq	(%r12), %rdi
	movq	24(%rsp), %rsi                  # 8-byte Reload
	movq	%r15, %rdx
	movl	$1, %ecx
	callq	hipMemcpy
	leaq	48(%rsp), %rax
	movq	(%rax), %rdi
	movq	(%r12), %rsi
	movq	(%r13), %rdx
	callq	hipfftExecR2C
	callq	hipDeviceSynchronize
	movl	%r14d, %eax
	xorl	%edx, %edx
	divl	%ebp
                                        # kill: def $eax killed $eax def $rax
	btsq	$32, %rax
	movl	%ebp, %edx
	btsq	$32, %rdx
	movq	%rax, %rdi
	movl	$1, %esi
	movl	$1, %ecx
	xorl	%r8d, %r8d
	xorl	%r9d, %r9d
	callq	__hipPushCallConfiguration
	testl	%eax, %eax
	jne	.LBB1_2
# %bb.1:
	movq	16(%rsp), %rdi
	movq	8(%rsp), %rsi
	movl	%r14d, %edx
	callq	_Z29__device_stub__calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj
.LBB1_2:
	movq	8(%rsp), %rsi
	movq	32(%rsp), %rdi                  # 8-byte Reload
	movq	%r15, %rdx
	movl	$2, %ecx
	callq	hipMemcpy
	movq	40(%rsp), %rdi
	callq	hipFree
	movq	16(%rsp), %rdi
	callq	hipFree
	movq	8(%rsp), %rdi
	callq	hipFree
	addq	$56, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_Z6runFFTPKfPfjj, .Lfunc_end1-_Z6runFFTPKfPfjj
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst8,"aM",@progbits,8
	.p2align	3, 0x0                          # -- Begin function _Z19initializeInputDataPfffj
.LCPI2_0:
	.quad	0x401921fb54442d18              # double 6.2831853071795862
	.text
	.globl	_Z19initializeInputDataPfffj
	.type	_Z19initializeInputDataPfffj,@function
_Z19initializeInputDataPfffj:           # @_Z19initializeInputDataPfffj
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	subq	$24, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	%esi, %ebp
	movss	%xmm1, 8(%rsp)                  # 4-byte Spill
	movq	%rdi, %rbx
	cvtss2sd	%xmm0, %xmm0
	movl	$.L.str, %edi
	movsd	%xmm0, 16(%rsp)                 # 8-byte Spill
	movb	$1, %al
	callq	printf
	testl	%ebp, %ebp
	je	.LBB2_3
# %bb.1:                                # %.lr.ph
	movsd	16(%rsp), %xmm0                 # 8-byte Reload
                                        # xmm0 = mem[0],zero
	mulsd	.LCPI2_0(%rip), %xmm0
	movsd	%xmm0, 16(%rsp)                 # 8-byte Spill
	xorps	%xmm0, %xmm0
	cvtss2sd	8(%rsp), %xmm0                  # 4-byte Folded Reload
	movsd	%xmm0, 8(%rsp)                  # 8-byte Spill
	movl	%ebp, %r14d
	xorl	%r15d, %r15d
.LBB2_2:                                # =>This Inner Loop Header: Depth=1
	movl	%r15d, %eax
	xorps	%xmm0, %xmm0
	cvtsi2sd	%rax, %xmm0
	mulsd	16(%rsp), %xmm0                 # 8-byte Folded Reload
	divsd	8(%rsp), %xmm0                  # 8-byte Folded Reload
	callq	sin
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, (%rbx,%r15,4)
	incq	%r15
	cmpq	%r15, %r14
	jne	.LBB2_2
.LBB2_3:                                # %._crit_edge
	addq	$24, %rsp
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end2:
	.size	_Z19initializeInputDataPfffj, .Lfunc_end2-_Z19initializeInputDataPfffj
	.cfi_endproc
                                        # -- End function
	.globl	_Z16extractFrequencyPKffj       # -- Begin function _Z16extractFrequencyPKffj
	.type	_Z16extractFrequencyPKffj,@function
_Z16extractFrequencyPKffj:              # @_Z16extractFrequencyPKffj
	.cfi_startproc
# %bb.0:
	xorps	%xmm1, %xmm1
	cmpl	$2, %esi
	jb	.LBB3_4
# %bb.1:                                # %.lr.ph.preheader
	movl	%esi, %eax
	shrl	%eax
	xorl	%ecx, %ecx
	xorl	%edx, %edx
.LBB3_2:                                # %.lr.ph
                                        # =>This Inner Loop Header: Depth=1
	movss	(%rdi,%rcx,4), %xmm2            # xmm2 = mem[0],zero,zero,zero
	ucomiss	%xmm1, %xmm2
	cmoval	%ecx, %edx
	maxss	%xmm1, %xmm2
	incq	%rcx
	movaps	%xmm2, %xmm1
	cmpq	%rcx, %rax
	jne	.LBB3_2
# %bb.3:                                # %._crit_edge.loopexit
	movl	%edx, %eax
	xorps	%xmm1, %xmm1
	cvtsi2ss	%rax, %xmm1
.LBB3_4:                                # %._crit_edge
	movl	%esi, %eax
	xorps	%xmm2, %xmm2
	cvtsi2ss	%rax, %xmm2
	mulss	%xmm0, %xmm1
	divss	%xmm2, %xmm1
	movaps	%xmm1, %xmm0
	retq
.Lfunc_end3:
	.size	_Z16extractFrequencyPKffj, .Lfunc_end3-_Z16extractFrequencyPKffj
	.cfi_endproc
                                        # -- End function
	.section	.rodata.cst4,"aM",@progbits,4
	.p2align	2, 0x0                          # -- Begin function main
.LCPI4_0:
	.long	0x44000000                      # float 512
.LCPI4_1:
	.long	0x43000000                      # float 128
.LCPI4_2:
	.long	0x4e6e6b28                      # float 1.0E+9
.LCPI4_3:
	.long	0x447a0000                      # float 1000
	.text
	.globl	main
	.type	main,@function
main:                                   # @main
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r12
	.cfi_def_cfa_offset 40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	subq	$16, %rsp
	.cfi_def_cfa_offset 64
	.cfi_offset %rbx, -48
	.cfi_offset %r12, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movss	.LCPI4_0(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, 8(%rsp)                  # 4-byte Spill
	movl	$256, %ebp                      # imm = 0x100
	movl	$512, %ebx                      # imm = 0x200
	movss	.LCPI4_1(%rip), %xmm0           # xmm0 = mem[0],zero,zero,zero
	movss	%xmm0, 12(%rsp)                 # 4-byte Spill
	cmpl	$2, %edi
	jl	.LBB4_5
# %bb.1:
	movq	%rsi, %r14
	movl	%edi, %r15d
	movq	8(%rsi), %rdi
	callq	atoi
	movl	%eax, %ebx
	cmpl	$2, %r15d
	je	.LBB4_5
# %bb.2:
	movq	16(%r14), %rdi
	callq	atoi
	movl	%eax, %ebp
	cmpl	$4, %r15d
	jb	.LBB4_5
# %bb.3:
	movq	24(%r14), %rdi
	callq	atof
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, 12(%rsp)                 # 4-byte Spill
	cmpl	$4, %r15d
	je	.LBB4_5
# %bb.4:
	movq	32(%r14), %rdi
	callq	atof
	cvtsd2ss	%xmm0, %xmm0
	movss	%xmm0, 8(%rsp)                  # 4-byte Spill
.LBB4_5:                                # %.thread44
	leal	(,%rbx,4), %r15d
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %r14
	movq	%r15, %rdi
	callq	malloc
	movq	%rax, %r15
	movq	%r14, %rdi
	movss	12(%rsp), %xmm0                 # 4-byte Reload
                                        # xmm0 = mem[0],zero,zero,zero
	movss	8(%rsp), %xmm1                  # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	movl	%ebx, %esi
	callq	_Z19initializeInputDataPfffj
	movq	%r14, %rdi
	movq	%r15, %rsi
	movl	%ebx, %edx
	movl	%ebp, %ecx
	callq	_Z6runFFTPKfPfjj
	callq	_ZNSt6chrono3_V212system_clock3nowEv
	movq	%rax, %r12
	movq	%r14, %rdi
	movq	%r15, %rsi
	movl	%ebx, %edx
	movl	%ebp, %ecx
	callq	_Z6runFFTPKfPfjj
	xorps	%xmm0, %xmm0
	cmpl	$2, %ebx
	jb	.LBB4_9
# %bb.6:                                # %.lr.ph.preheader.i
	movl	%ebx, %eax
	shrl	%eax
	xorl	%ecx, %ecx
	xorl	%edx, %edx
.LBB4_7:                                # %.lr.ph.i
                                        # =>This Inner Loop Header: Depth=1
	movss	(%r15,%rcx,4), %xmm1            # xmm1 = mem[0],zero,zero,zero
	ucomiss	%xmm0, %xmm1
	cmoval	%ecx, %edx
	maxss	%xmm0, %xmm1
	incq	%rcx
	movaps	%xmm1, %xmm0
	cmpq	%rcx, %rax
	jne	.LBB4_7
# %bb.8:                                # %._crit_edge.loopexit.i
	movl	%edx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%rax, %xmm0
.LBB4_9:                                # %_Z16extractFrequencyPKffj.exit
	movss	8(%rsp), %xmm1                  # 4-byte Reload
                                        # xmm1 = mem[0],zero,zero,zero
	mulss	%xmm0, %xmm1
	movl	%ebx, %eax
	xorps	%xmm0, %xmm0
	cvtsi2ss	%rax, %xmm0
	divss	%xmm0, %xmm1
	movss	%xmm1, 8(%rsp)                  # 4-byte Spill
	callq	_ZNSt6chrono3_V212system_clock3nowEv
	subq	%r12, %rax
	xorps	%xmm1, %xmm1
	cvtsi2ss	%rax, %xmm1
	divss	.LCPI4_2(%rip), %xmm1
	mulss	.LCPI4_3(%rip), %xmm1
	xorps	%xmm0, %xmm0
	cvtss2sd	8(%rsp), %xmm0                  # 4-byte Folded Reload
	cvtss2sd	%xmm1, %xmm1
	movl	$.L.str.1, %edi
	movb	$2, %al
	callq	printf
	movq	%r14, %rdi
	callq	free
	movq	%r15, %rdi
	callq	free
	xorl	%eax, %eax
	addq	$16, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end4:
	.size	main, .Lfunc_end4-main
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_ctor
	.type	__hip_module_ctor,@function
__hip_module_ctor:                      # @__hip_module_ctor
	.cfi_startproc
# %bb.0:
	subq	$40, %rsp
	.cfi_def_cfa_offset 48
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	jne	.LBB5_2
# %bb.1:
	movl	$__hip_fatbin_wrapper, %edi
	callq	__hipRegisterFatBinary
	movq	%rax, %rdi
	movq	%rax, __hip_gpubin_handle(%rip)
.LBB5_2:
	xorps	%xmm0, %xmm0
	movups	%xmm0, 16(%rsp)
	movups	%xmm0, (%rsp)
	movl	$_Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj, %esi
	movl	$.L__unnamed_1, %edx
	movl	$.L__unnamed_1, %ecx
	movl	$-1, %r8d
	xorl	%r9d, %r9d
	callq	__hipRegisterFunction
	movl	$__hip_module_dtor, %edi
	addq	$40, %rsp
	.cfi_def_cfa_offset 8
	jmp	atexit                          # TAILCALL
.Lfunc_end5:
	.size	__hip_module_ctor, .Lfunc_end5-__hip_module_ctor
	.cfi_endproc
                                        # -- End function
	.p2align	4, 0x90                         # -- Begin function __hip_module_dtor
	.type	__hip_module_dtor,@function
__hip_module_dtor:                      # @__hip_module_dtor
	.cfi_startproc
# %bb.0:
	movq	__hip_gpubin_handle(%rip), %rdi
	testq	%rdi, %rdi
	je	.LBB6_2
# %bb.1:
	pushq	%rax
	.cfi_def_cfa_offset 16
	callq	__hipUnregisterFatBinary
	movq	$0, __hip_gpubin_handle(%rip)
	addq	$8, %rsp
	.cfi_def_cfa_offset 8
.LBB6_2:
	retq
.Lfunc_end6:
	.size	__hip_module_dtor, .Lfunc_end6-__hip_module_dtor
	.cfi_endproc
                                        # -- End function
	.type	_Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj,@object # @_Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj
	.section	.rodata,"a",@progbits
	.globl	_Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj
	.p2align	3, 0x0
_Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj:
	.quad	_Z29__device_stub__calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj
	.size	_Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj, 8

	.type	.L.str,@object                  # @.str
	.section	.rodata.str1.1,"aMS",@progbits,1
.L.str:
	.asciz	"Generating %f Hz signal\n"
	.size	.L.str, 25

	.type	.L.str.1,@object                # @.str.1
.L.str.1:
	.asciz	"Strongest frequency bin was %f Hz (execution time = %.3f ms)\n"
	.size	.L.str.1, 62

	.type	.L__unnamed_1,@object           # @0
.L__unnamed_1:
	.asciz	"_Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj"
	.size	.L__unnamed_1, 48

	.type	__hip_fatbin_wrapper,@object    # @__hip_fatbin_wrapper
	.section	.hipFatBinSegment,"a",@progbits
	.p2align	3, 0x0
__hip_fatbin_wrapper:
	.long	1212764230                      # 0x48495046
	.long	1                               # 0x1
	.quad	__hip_fatbin
	.quad	0
	.size	__hip_fatbin_wrapper, 24

	.type	__hip_gpubin_handle,@object     # @__hip_gpubin_handle
	.local	__hip_gpubin_handle
	.comm	__hip_gpubin_handle,8,8
	.section	.init_array,"aw",@init_array
	.p2align	3, 0x0
	.quad	__hip_module_ctor
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym _Z29__device_stub__calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj
	.addrsig_sym __hip_module_ctor
	.addrsig_sym __hip_module_dtor
	.addrsig_sym _Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj
	.addrsig_sym __hip_fatbin
	.addrsig_sym __hip_fatbin_wrapper
	.addrsig_sym __hip_cuid_

	.text
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.protected	_Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj ; -- Begin function _Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj
	.globl	_Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj
	.p2align	8
	.type	_Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj,@function
_Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj: ; @_Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj
; %bb.0:
	s_clause 0x1
	s_load_b32 s2, s[0:1], 0x24
	s_load_b32 s3, s[0:1], 0x10
	s_waitcnt lgkmcnt(0)
	s_and_b32 s2, s2, 0xffff
	s_delay_alu instid0(SALU_CYCLE_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_mad_u64_u32 v[1:2], null, s15, s2, v[0:1]
	s_mov_b32 s2, exec_lo
	v_cmpx_gt_u32_e64 s3, v1
	s_cbranch_execz .LBB0_2
; %bb.1:
	s_load_b128 s[0:3], s[0:1], 0x0
	v_mov_b32_e32 v2, 0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_1)
	v_lshlrev_b64 v[3:4], 3, v[1:2]
	s_waitcnt lgkmcnt(0)
	v_add_co_u32 v3, vcc_lo, s0, v3
	s_delay_alu instid0(VALU_DEP_2) | instskip(SKIP_3) | instid1(VALU_DEP_1)
	v_add_co_ci_u32_e32 v4, vcc_lo, s1, v4, vcc_lo
	global_load_b64 v[3:4], v[3:4], off
	s_waitcnt vmcnt(0)
	v_mul_f32_e32 v0, v4, v4
	v_fmac_f32_e32 v0, v3, v3
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_1) | instid1(VALU_DEP_2)
	v_mul_f32_e32 v3, 0x4f800000, v0
	v_cmp_gt_f32_e32 vcc_lo, 0xf800000, v0
	v_cndmask_b32_e32 v3, v0, v3, vcc_lo
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_3) | instid1(VALU_DEP_2)
	v_sqrt_f32_e32 v0, v3
	s_waitcnt_depctr 0xfff
	v_add_nc_u32_e32 v4, -1, v0
	v_add_nc_u32_e32 v5, 1, v0
	v_fma_f32 v6, -v4, v0, v3
	s_delay_alu instid0(VALU_DEP_2) | instskip(NEXT) | instid1(VALU_DEP_2)
	v_fma_f32 v7, -v5, v0, v3
	v_cmp_ge_f32_e64 s0, 0, v6
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_3)
	v_cndmask_b32_e64 v0, v0, v4, s0
	v_cmp_lt_f32_e64 s0, 0, v7
	s_delay_alu instid0(VALU_DEP_1) | instskip(NEXT) | instid1(VALU_DEP_1)
	v_cndmask_b32_e64 v0, v0, v5, s0
	v_mul_f32_e32 v4, 0x37800000, v0
	s_delay_alu instid0(VALU_DEP_1) | instskip(SKIP_2) | instid1(VALU_DEP_3)
	v_cndmask_b32_e32 v4, v0, v4, vcc_lo
	v_lshlrev_b64 v[0:1], 2, v[1:2]
	v_cmp_class_f32_e64 vcc_lo, v3, 0x260
	v_cndmask_b32_e32 v2, v4, v3, vcc_lo
	s_delay_alu instid0(VALU_DEP_3) | instskip(NEXT) | instid1(VALU_DEP_4)
	v_add_co_u32 v0, vcc_lo, s2, v0
	v_add_co_ci_u32_e32 v1, vcc_lo, s3, v1, vcc_lo
	global_store_b32 v[0:1], v2, off
.LBB0_2:
	s_nop 0
	s_sendmsg sendmsg(MSG_DEALLOC_VGPRS)
	s_endpgm
	.section	.rodata,"a",@progbits
	.p2align	6, 0x0
	.amdhsa_kernel _Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj
		.amdhsa_group_segment_fixed_size 0
		.amdhsa_private_segment_fixed_size 0
		.amdhsa_kernarg_size 280
		.amdhsa_user_sgpr_count 15
		.amdhsa_user_sgpr_dispatch_ptr 0
		.amdhsa_user_sgpr_queue_ptr 0
		.amdhsa_user_sgpr_kernarg_segment_ptr 1
		.amdhsa_user_sgpr_dispatch_id 0
		.amdhsa_user_sgpr_private_segment_size 0
		.amdhsa_wavefront_size32 1
		.amdhsa_uses_dynamic_stack 0
		.amdhsa_enable_private_segment 0
		.amdhsa_system_sgpr_workgroup_id_x 1
		.amdhsa_system_sgpr_workgroup_id_y 0
		.amdhsa_system_sgpr_workgroup_id_z 0
		.amdhsa_system_sgpr_workgroup_info 0
		.amdhsa_system_vgpr_workitem_id 0
		.amdhsa_next_free_vgpr 8
		.amdhsa_next_free_sgpr 16
		.amdhsa_float_round_mode_32 0
		.amdhsa_float_round_mode_16_64 0
		.amdhsa_float_denorm_mode_32 3
		.amdhsa_float_denorm_mode_16_64 3
		.amdhsa_dx10_clamp 1
		.amdhsa_ieee_mode 1
		.amdhsa_fp16_overflow 0
		.amdhsa_workgroup_processor_mode 1
		.amdhsa_memory_ordered 1
		.amdhsa_forward_progress 0
		.amdhsa_shared_vgpr_count 0
		.amdhsa_exception_fp_ieee_invalid_op 0
		.amdhsa_exception_fp_denorm_src 0
		.amdhsa_exception_fp_ieee_div_zero 0
		.amdhsa_exception_fp_ieee_overflow 0
		.amdhsa_exception_fp_ieee_underflow 0
		.amdhsa_exception_fp_ieee_inexact 0
		.amdhsa_exception_int_div_zero 0
	.end_amdhsa_kernel
	.text
.Lfunc_end0:
	.size	_Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj, .Lfunc_end0-_Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj
                                        ; -- End function
	.section	.AMDGPU.csdata,"",@progbits
; Kernel info:
; codeLenInByte = 304
; NumSgprs: 18
; NumVgprs: 8
; ScratchSize: 0
; MemoryBound: 0
; FloatMode: 240
; IeeeMode: 1
; LDSByteSize: 0 bytes/workgroup (compile time only)
; SGPRBlocks: 2
; VGPRBlocks: 0
; NumSGPRsForWavesPerEU: 18
; NumVGPRsForWavesPerEU: 8
; Occupancy: 16
; WaveLimiterHint : 0
; COMPUTE_PGM_RSRC2:SCRATCH_EN: 0
; COMPUTE_PGM_RSRC2:USER_SGPR: 15
; COMPUTE_PGM_RSRC2:TRAP_HANDLER: 0
; COMPUTE_PGM_RSRC2:TGID_X_EN: 1
; COMPUTE_PGM_RSRC2:TGID_Y_EN: 0
; COMPUTE_PGM_RSRC2:TGID_Z_EN: 0
; COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: 0
	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgpu_metadata
---
amdhsa.kernels:
  - .args:
      - .address_space:  global
        .offset:         0
        .size:           8
        .value_kind:     global_buffer
      - .address_space:  global
        .offset:         8
        .size:           8
        .value_kind:     global_buffer
      - .offset:         16
        .size:           4
        .value_kind:     by_value
      - .offset:         24
        .size:           4
        .value_kind:     hidden_block_count_x
      - .offset:         28
        .size:           4
        .value_kind:     hidden_block_count_y
      - .offset:         32
        .size:           4
        .value_kind:     hidden_block_count_z
      - .offset:         36
        .size:           2
        .value_kind:     hidden_group_size_x
      - .offset:         38
        .size:           2
        .value_kind:     hidden_group_size_y
      - .offset:         40
        .size:           2
        .value_kind:     hidden_group_size_z
      - .offset:         42
        .size:           2
        .value_kind:     hidden_remainder_x
      - .offset:         44
        .size:           2
        .value_kind:     hidden_remainder_y
      - .offset:         46
        .size:           2
        .value_kind:     hidden_remainder_z
      - .offset:         64
        .size:           8
        .value_kind:     hidden_global_offset_x
      - .offset:         72
        .size:           8
        .value_kind:     hidden_global_offset_y
      - .offset:         80
        .size:           8
        .value_kind:     hidden_global_offset_z
      - .offset:         88
        .size:           2
        .value_kind:     hidden_grid_dims
    .group_segment_fixed_size: 0
    .kernarg_segment_align: 8
    .kernarg_segment_size: 280
    .language:       OpenCL C
    .language_version:
      - 2
      - 0
    .max_flat_workgroup_size: 1024
    .name:           _Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj
    .private_segment_fixed_size: 0
    .sgpr_count:     18
    .sgpr_spill_count: 0
    .symbol:         _Z14calcMagnitudesPK15HIP_vector_typeIfLj2EEPfj.kd
    .uniform_work_group_size: 1
    .uses_dynamic_stack: false
    .vgpr_count:     8
    .vgpr_spill_count: 0
    .wavefront_size: 32
    .workgroup_processor_mode: 1
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
