yum/vfft
A (very) fast fourier transform.
git clone https://git.yummers.dev/yum/vfft
f99f221
master
1use crate :: util :: * ; 2use num :: complex :: Complex ; 3use num :: traits :: { Float , FloatConst } ; 4 5fn fft_butterfly_radix_2_unsafe < T : Float + FloatConst > ( 6input : & mut [ Complex < T > ] , 7output : & mut [ Complex < T > ] , 8stride : usize , 9big_n : usize , 10twiddles : & [ Complex < T > ] , 11_ : & ExecutionStage < T > , 12) { 13let input_ptr = input . as_ptr () ; 14let output_ptr = output . as_mut_ptr () ; 15for start_idx in 0 .. stride { 16for k in 0 .. big_n / 2 { 17unsafe { 18// Collect inputs. 19let p = * input_ptr . add ( start_idx + 2 * k * stride ) ; 20let q = * input_ptr . add ( start_idx + ( 2 * k + 1 ) * stride ) ; 21// Collect relevant twiddles. 22let ot = twiddles . get_unchecked ( k * stride ) ; 23// Combine. 24let r = q * ot ; 25* output_ptr . add ( start_idx + k * stride ) = p + r ; 26* output_ptr . add ( start_idx + ( k + big_n / 2 ) * stride ) = p - r ; 27} 28} 29} 30} 31 32fn fft_butterfly_radix_2_s0_unsafe < T : Float + FloatConst > ( 33input : & mut [ Complex < T > ] , 34output : & mut [ Complex < T > ] , 35_ : & Matrix < Complex < T >> , 36) { 37let input_ptr = input . as_ptr () ; 38let output_ptr = output . as_mut_ptr () ; 39let stride = input . len () / 2 ; 40let big_n = 2 ; 41for start_idx in 0 .. stride { 42for k in 0 .. big_n / 2 { 43unsafe { 44// Collect inputs. 45let p = * input_ptr . add ( start_idx + 2 * k * stride ) ; 46let q = * input_ptr . add ( start_idx + ( 2 * k + 1 ) * stride ) ; 47* output_ptr . add ( start_idx + k * stride ) = p + q ; 48* output_ptr . add ( start_idx + ( k + big_n / 2 ) * stride ) = p - q ; 49} 50} 51} 52} 53 54fn fft_butterfly_radix_2_reindexed_unsafe < T : Float + FloatConst > ( 55input : & mut [ Complex < T > ] , 56output : & mut [ Complex < T > ] , 57stride : usize , 58_ : usize , 59twiddles : & [ Complex < T > ] , 60_ : & ExecutionStage < T > , 61) { 62let input_ptr = input . as_ptr () ; 63let output_ptr = output . as_mut_ptr () ; 64let half_len = input . len () / 2 ; 65let stride_bit_mask = ! ( stride - 1 ) ; 66for idx in 0 .. half_len { 67let twiddle_idx = idx & stride_bit_mask ; 68let p_idx = twiddle_idx + idx ; 69let q_idx = p_idx + stride ; 70 71unsafe { 72let p = * input_ptr . add ( p_idx ) ; 73let q = * input_ptr . add ( q_idx ) * twiddles . get_unchecked ( twiddle_idx ) ; 74* output_ptr . add ( idx ) = p + q ; 75* output_ptr . add ( idx + half_len ) = p - q ; 76} 77} 78} 79 80fn fft_butterfly_radix_2_reindexed_s0_unsafe < T : Float + FloatConst > ( 81input : & mut [ Complex < T > ] , 82output : & mut [ Complex < T > ] , 83_ : & Matrix < Complex < T >> , 84) { 85let input_ptr = input . as_ptr () ; 86let output_ptr = output . as_mut_ptr () ; 87let half_len = input . len () / 2 ; 88for idx in 0 .. half_len { 89unsafe { 90let p = * input_ptr . add ( idx ) ; 91let q = * input_ptr . add ( idx + half_len ) ; 92* output_ptr . add ( idx ) = p + q ; 93* output_ptr . add ( idx + half_len ) = p - q ; 94} 95} 96} 97 98fn fft_butterfly_radix_2_unrolled_unsafe < T : Float + FloatConst , const WIDTH : usize > ( 99input : & mut [ Complex < T > ] , 100output : & mut [ Complex < T > ] , 101stride : usize , 102_ : usize , 103twiddles : & [ Complex < T > ] , 104_ : & ExecutionStage < T > , 105) { 106let input_ptr = input . as_ptr () ; 107let output_ptr = output . as_mut_ptr () ; 108let half_len = input . len () / 2 ; 109let stride_bit_mask = ! ( stride - 1 ) ; 110for idx0 in ( 0 .. half_len ) . step_by ( WIDTH ) { 111unsafe { 112let p_q : [ _ ; WIDTH ] = std :: array :: from_fn ( | lane | { 113let idx = idx0 + lane ; 114let twiddle_idx = idx & stride_bit_mask ; 115let p_idx = twiddle_idx + idx ; 116let q_idx = p_idx + stride ; 117let p = * input_ptr . add ( p_idx ) ; 118let q = * input_ptr . add ( q_idx ) * twiddles . get_unchecked ( twiddle_idx ) ; 119( p , q ) 120}) ; 121// Make it obvious to the compiler that it can vectorize this store. 122for ( lane , ( p , q )) in p_q . into_iter () . enumerate () { 123* output_ptr . add ( idx0 + lane ) = p + q ; 124} 125for ( lane , ( p , q )) in p_q . into_iter () . enumerate () { 126* output_ptr . add ( idx0 + half_len + lane ) = p - q ; 127} 128} 129} 130} 131 132// For whatever reason, the compiler shits itself if we manually unroll this. 133// So use the 134fn fft_butterfly_radix_2_unrolled_s0_unsafe < T : Float + FloatConst , const WIDTH : usize > ( 135input : & mut [ Complex < T > ] , 136output : & mut [ Complex < T > ] , 137m : & Matrix < Complex < T >> , 138) { 139fft_butterfly_radix_2_reindexed_s0_unsafe ( input , output , m ) ; 140} 141 142fn radix_3_dft < T : Float + FloatConst > ( 143input : & [ Complex < T > ; 3 ] , 144_ : & Matrix < Complex < T >> , 145) -> [ Complex < T > ; 3 ] { 146const N : usize = 3 ; 147let zero = Complex :: new ( T :: zero () , T :: zero ()) ; 148let mut result = [ zero ; N ] ; 149let half = T :: from ( 0.5 ) . unwrap () ; 150let sqrt_3_over_2 = 151T :: from ( 0.8660254037844386467637231707529361834714026269051903140279034897 ) . unwrap () ; 152let sum = input [ 1 ] + input [ 2 ] ; 153let diff = input [ 1 ] - input [ 2 ] ; 154result [ 0 ] = input [ 0 ] + input [ 1 ] + input [ 2 ] ; 155result [ 1 ] = input [ 0 ] - sum . scale ( half ) - mul_i ( diff . scale ( sqrt_3_over_2 )) ; 156result [ 2 ] = input [ 0 ] - sum . scale ( half ) + mul_i ( diff . scale ( sqrt_3_over_2 )) ; 157result 158} 159 160fn fft_butterfly_radix_3_unsafe < T : Float + FloatConst > ( 161input : & mut [ Complex < T > ] , 162output : & mut [ Complex < T > ] , 163stride : usize , 164big_n : usize , 165twiddles : & [ Complex < T > ] , 166stage : & ExecutionStage < T > , 167) { 168let input_ptr = input . as_ptr () ; 169let output_ptr = output . as_mut_ptr () ; 170const N : usize = 3 ; 171for start_idx in 0 .. stride { 172for k in 0 .. big_n / N { 173unsafe { 174// Collect inputs. 175let inputs : [ Complex < T > ; N ] = 176std :: array :: from_fn ( | idx | * input_ptr . add ( start_idx + ( N * k + idx ) * stride )) ; 177// Collect relevant twiddles. 178let twiddles : [ & Complex < T > ; N ] = 179std :: array :: from_fn ( | idx | twiddles . get_unchecked ( idx * k * stride )) ; 180// Precompute a term used in the inner loop. 181let it : [ Complex < T > ; N ] = std :: array :: from_fn ( | idx | { 182if idx == 0 { 183inputs [ idx ] 184} else { 185inputs [ idx ] * twiddles [ idx ] 186} 187}) ; 188let result = radix_3_dft ( & it , & stage . outer_terms ) ; 189for row in 0 .. N { 190* output_ptr . add ( start_idx + ( k + row * big_n / N ) * stride ) = result [ row ] ; 191} 192} 193} 194} 195} 196 197fn fft_butterfly_radix_3_s0_unsafe < T : Float + FloatConst > ( 198input : & mut [ Complex < T > ] , 199output : & mut [ Complex < T > ] , 200outer_terms : & Matrix < Complex < T >> , 201) { 202const N : usize = 3 ; 203let input_ptr = input . as_ptr () ; 204let output_ptr = output . as_mut_ptr () ; 205let stride = input . len () / N ; 206let big_n = N ; 207for start_idx in 0 .. stride { 208for k in 0 .. big_n / N { 209unsafe { 210// Collect inputs. 211let inputs : [ Complex < T > ; N ] = 212std :: array :: from_fn ( | idx | * input_ptr . add ( start_idx + ( N * k + idx ) * stride )) ; 213let result = radix_3_dft ( & inputs , outer_terms ) ; 214for row in 0 .. N { 215* output_ptr . add ( start_idx + ( k + row * big_n / N ) * stride ) = result [ row ] ; 216} 217} 218} 219} 220} 221 222fn fft_butterfly_radix_9_unsafe < T : Float + FloatConst > ( 223input : & mut [ Complex < T > ] , 224output : & mut [ Complex < T > ] , 225stride : usize , 226big_n : usize , 227twiddles : & [ Complex < T > ] , 228stage : & ExecutionStage < T > , 229) { 230let input_ptr = input . as_ptr () ; 231let output_ptr = output . as_mut_ptr () ; 232const N : usize = 9 ; 233for start_idx in 0 .. stride { 234for k in 0 .. big_n / N { 235unsafe { 236let inputs : [ Complex < T > ; N ] = 237std :: array :: from_fn ( | idx | * input_ptr . add ( start_idx + ( N * k + idx ) * stride )) ; 238let twiddles : [ & Complex < T > ; N ] = 239std :: array :: from_fn ( | idx | twiddles . get_unchecked ( idx * k * stride )) ; 240let it : [ Complex < T > ; N ] = std :: array :: from_fn ( | idx | { 241if idx == 0 { 242inputs [ idx ] 243} else { 244inputs [ idx ] * twiddles [ idx ] 245} 246}) ; 247 248let b00 = [ it [ 0 ] , it [ 3 ] , it [ 6 ]] ; 249let b01 = [ it [ 1 ] , it [ 4 ] , it [ 7 ]] ; 250let b02 = [ it [ 2 ] , it [ 5 ] , it [ 8 ]] ; 251 252let b00_out = radix_3_dft ( & b00 , & stage . outer_terms ) ; 253let b01_out = radix_3_dft ( & b01 , & stage . outer_terms ) ; 254let b02_out = radix_3_dft ( & b02 , & stage . outer_terms ) ; 255 256// Recall the general way that outer terms work: 257// First row is all 1s. 258// Second row rotates 1/r clockwise. 259// Third row rotates 2/r clockwise. 260// outer_terms already takes into account the way rotations 261// accelerate each row, so we just have to index into the right 262// row/col. 263let b10 = [ b00_out [ 0 ] , b01_out [ 0 ] , b02_out [ 0 ]] ; 264let b11 = [ 265b00_out [ 1 ] , 266b01_out [ 1 ] * stage . outer_terms . data . get_unchecked ( 9 + 1 ) , 267b02_out [ 1 ] * stage . outer_terms . data . get_unchecked ( 9 + 2 ) , 268] ; 269let b12 = [ 270b00_out [ 2 ] , 271b01_out [ 2 ] * stage . outer_terms . data . get_unchecked ( 18 + 1 ) , 272b02_out [ 2 ] * stage . outer_terms . data . get_unchecked ( 18 + 2 ) , 273] ; 274 275let r00 = radix_3_dft ( & b10 , & stage . outer_terms ) ; 276let r01 = radix_3_dft ( & b11 , & stage . outer_terms ) ; 277let r02 = radix_3_dft ( & b12 , & stage . outer_terms ) ; 278 279let results = [ 280r00 [ 0 ] , r01 [ 0 ] , r02 [ 0 ] , r00 [ 1 ] , r01 [ 1 ] , r02 [ 1 ] , r00 [ 2 ] , r01 [ 2 ] , r02 [ 2 ] , 281] ; 282for row in 0 .. N { 283* output_ptr . add ( start_idx + ( k + row * big_n / N ) * stride ) = results [ row ] ; 284} 285} 286} 287} 288} 289 290fn fft_butterfly_radix_9_s0_unsafe < T : Float + FloatConst > ( 291input : & mut [ Complex < T > ] , 292output : & mut [ Complex < T > ] , 293outer_terms : & Matrix < Complex < T >> , 294) { 295let input_ptr = input . as_ptr () ; 296let output_ptr = output . as_mut_ptr () ; 297const N : usize = 9 ; 298let stride = input . len () / N ; 299let big_n = N ; 300for start_idx in 0 .. stride { 301for k in 0 .. big_n / N { 302unsafe { 303let inputs : [ Complex < T > ; N ] = 304std :: array :: from_fn ( | idx | * input_ptr . add ( start_idx + ( N * k + idx ) * stride )) ; 305 306let b00 = [ inputs [ 0 ] , inputs [ 3 ] , inputs [ 6 ]] ; 307let b01 = [ inputs [ 1 ] , inputs [ 4 ] , inputs [ 7 ]] ; 308let b02 = [ inputs [ 2 ] , inputs [ 5 ] , inputs [ 8 ]] ; 309 310let b00_out = radix_3_dft ( & b00 , & outer_terms ) ; 311let b01_out = radix_3_dft ( & b01 , & outer_terms ) ; 312let b02_out = radix_3_dft ( & b02 , & outer_terms ) ; 313 314// Recall the general way that outer terms work: 315// First row is all 1s. 316// Second row rotates 1/r clockwise. 317// Third row rotates 2/r clockwise. 318let b10 = [ b00_out [ 0 ] , b01_out [ 0 ] , b02_out [ 0 ]] ; 319let b11 = [ 320b00_out [ 1 ] , 321b01_out [ 1 ] * outer_terms . data . get_unchecked ( 9 + 1 ) , 322b02_out [ 1 ] * outer_terms . data . get_unchecked ( 9 + 2 ) , 323] ; 324let b12 = [ 325b00_out [ 2 ] , 326b01_out [ 2 ] * outer_terms . data . get_unchecked ( 18 + 1 ) , 327b02_out [ 2 ] * outer_terms . data . get_unchecked ( 18 + 2 ) , 328] ; 329 330let r00 = radix_3_dft ( & b10 , & outer_terms ) ; 331let r01 = radix_3_dft ( & b11 , & outer_terms ) ; 332let r02 = radix_3_dft ( & b12 , & outer_terms ) ; 333 334let results = [ 335r00 [ 0 ] , r01 [ 0 ] , r02 [ 0 ] , r00 [ 1 ] , r01 [ 1 ] , r02 [ 1 ] , r00 [ 2 ] , r01 [ 2 ] , r02 [ 2 ] , 336] ; 337for row in 0 .. N { 338* output_ptr . add ( start_idx + ( k + row * big_n / N ) * stride ) = results [ row ] ; 339} 340} 341} 342} 343} 344 345fn odd_radix_dft < const N : usize , T : Float + FloatConst > ( 346input : & [ Complex < T > ; N ] , 347outer_terms : & Matrix < Complex < T >> , 348) -> [ Complex < T > ; N ] { 349let zero = Complex :: new ( T :: zero () , T :: zero ()) ; 350let mut result = [ zero ; N ] ; 351// Precompute sums and differences. 352let mut sums = [ zero ; N ] ; 353let mut diffs = [ zero ; N ] ; 354for col in 1 .. N / 2 + 1 { 355sums [ col ] = input [ col ] + input [ N - col ] ; 356diffs [ col ] = input [ col ] - input [ N - col ] ; 357} 358 359// 0,0 term is never indexed, so compute it out of the loop. 360result [ 0 ] = input [ 0 ] ; 361 362// 0th row is never indexed, so compute it out of the loop. 363for col in 1 .. N / 2 + 1 { 364// outer_terms is always 1 along the first row and column, so we can skip the multiplication. 365result [ 0 ] = result [ 0 ] + sums [ col ] ; 366} 367 368for row in 1 .. N / 2 + 1 { 369// Column zero is not indexed below, so precompute here. Recall the 370// outer_terms is 1 along the first column. 371// 372// Per our identity, we split this sum into a "real" coefficient and an 373// "imaginary" coefficient. Note that they are in fact both complex-valued. 374let mut re_part = input [ 0 ] ; 375let mut im_part = zero ; 376 377for col in 1 .. N / 2 + 1 { 378unsafe { 379let w = outer_terms . data . get_unchecked ( row * N + col ) ; 380re_part = re_part + sums [ col ] . scale ( w . re ) ; 381im_part = im_part + diffs [ col ] . scale ( w . im ) ; 382} 383} 384 385// Then apply the identity to get both rows. 386let i_im_part = mul_i ( im_part ) ; 387result [ row ] = re_part + i_im_part ; 388result [ N - row ] = re_part - i_im_part ; 389} 390result 391} 392 393fn fft_butterfly_radix_p_unsafe < const N : usize , T : Float + FloatConst > ( 394input : & mut [ Complex < T > ] , 395output : & mut [ Complex < T > ] , 396stride : usize , 397big_n : usize , 398twiddles : & [ Complex < T > ] , 399stage : & ExecutionStage < T > , 400) { 401let input_ptr = input . as_ptr () ; 402let output_ptr = output . as_mut_ptr () ; 403for start_idx in 0 .. stride { 404for k in 0 .. big_n / N { 405unsafe { 406// Collect inputs. 407let inputs : [ Complex < T > ; N ] = 408std :: array :: from_fn ( | idx | * input_ptr . add ( start_idx + ( N * k + idx ) * stride )) ; 409// Collect relevant twiddles. 410let twiddles : [ & Complex < T > ; N ] = 411std :: array :: from_fn ( | idx | twiddles . get_unchecked ( idx * k * stride )) ; 412// Precompute a term used in the inner loop. 413let it : [ Complex < T > ; N ] = std :: array :: from_fn ( | idx | { 414if idx == 0 { 415inputs [ idx ] 416} else { 417inputs [ idx ] * twiddles [ idx ] 418} 419}) ; 420let result = odd_radix_dft ( & it , & stage . outer_terms ) ; 421for row in 0 .. N { 422* output_ptr . add ( start_idx + ( k + row * big_n / N ) * stride ) = result [ row ] ; 423} 424} 425} 426} 427} 428 429fn fft_butterfly_radix_p_s0_unsafe < const N : usize , T : Float + FloatConst > ( 430input : & mut [ Complex < T > ] , 431output : & mut [ Complex < T > ] , 432outer_terms : & Matrix < Complex < T >> , 433) { 434let input_ptr = input . as_ptr () ; 435let output_ptr = output . as_mut_ptr () ; 436let stride = input . len () / N ; 437let big_n = N ; 438for start_idx in 0 .. stride { 439for k in 0 .. big_n / N { 440unsafe { 441// Collect inputs. 442let inputs : [ Complex < T > ; N ] = 443std :: array :: from_fn ( | idx | * input_ptr . add ( start_idx + ( N * k + idx ) * stride )) ; 444// Precompute a term used in the inner loop. 445let result = odd_radix_dft ( & inputs , outer_terms ) ; 446for row in 0 .. N { 447* output_ptr . add ( start_idx + ( k + row * big_n / N ) * stride ) = result [ row ] ; 448} 449} 450} 451} 452} 453 454#[ inline ( always ) ] 455fn mul_i < T : Float + FloatConst > ( x : Complex < T > ) -> Complex < T > { 456Complex :: new ( - x . im , x . re ) 457} 458 459#[ inline ( always ) ] 460fn mul_ni < T : Float + FloatConst > ( x : Complex < T > ) -> Complex < T > { 461Complex :: new ( x . im , - x . re ) 462} 463 464fn fft_butterfly_radix_4_unsafe < T : Float + FloatConst > ( 465input : & mut [ Complex < T > ] , 466output : & mut [ Complex < T > ] , 467stride : usize , 468big_n : usize , 469twiddles : & [ Complex < T > ] , 470_ : & ExecutionStage < T > , 471) { 472let input_ptr = input . as_ptr () ; 473let output_ptr = output . as_mut_ptr () ; 474for start_idx in 0 .. stride { 475for k in 0 .. big_n / 4 { 476unsafe { 477// Collect inputs. 478let i0 = * input_ptr . add ( start_idx + 4 * k * stride ) ; 479let i1 = * input_ptr . add ( start_idx + ( 4 * k + 1 ) * stride ) ; 480let i2 = * input_ptr . add ( start_idx + ( 4 * k + 2 ) * stride ) ; 481let i3 = * input_ptr . add ( start_idx + ( 4 * k + 3 ) * stride ) ; 482// Collect relevant twiddles. 483let ot1 = twiddles . get_unchecked ( 1 * k * stride ) ; 484let ot2 = twiddles . get_unchecked ( 2 * k * stride ) ; 485let ot3 = twiddles . get_unchecked ( 3 * k * stride ) ; 486 487let a = i0 ; 488let b = ot1 * i1 ; 489let c = ot2 * i2 ; 490let d = ot3 * i3 ; 491 492// To derive this, write the output assignments in terms of 493// a/b/c/d, then factor out! 494let ac_sum = a + c ; 495let ac_diff = a - c ; 496let bd_sum = b + d ; 497let bd_diff_ni = mul_ni ( b - d ) ; 498 499* output_ptr . add ( start_idx + k * stride ) = ac_sum + bd_sum ; 500* output_ptr . add ( start_idx + ( k + big_n / 4 ) * stride ) = ac_diff + bd_diff_ni ; 501* output_ptr . add ( start_idx + ( k + big_n / 2 ) * stride ) = ac_sum - bd_sum ; 502* output_ptr . add ( start_idx + ( k + 3 * big_n / 4 ) * stride ) = ac_diff - bd_diff_ni ; 503} 504} 505} 506} 507 508fn fft_butterfly_radix_4_s0_unsafe < T : Float + FloatConst > ( 509input : & mut [ Complex < T > ] , 510output : & mut [ Complex < T > ] , 511_ : & Matrix < Complex < T >> , 512) { 513let stride = input . len () / 4 ; 514let big_n = 4 ; 515let input_ptr = input . as_ptr () ; 516let output_ptr = output . as_mut_ptr () ; 517for start_idx in 0 .. stride { 518for k in 0 .. big_n / 4 { 519unsafe { 520// Collect inputs. 521let i0 = * input_ptr . add ( start_idx + 4 * k * stride ) ; 522let i1 = * input_ptr . add ( start_idx + ( 4 * k + 1 ) * stride ) ; 523let i2 = * input_ptr . add ( start_idx + ( 4 * k + 2 ) * stride ) ; 524let i3 = * input_ptr . add ( start_idx + ( 4 * k + 3 ) * stride ) ; 525 526let a = i0 ; 527let b = i1 ; 528let c = i2 ; 529let d = i3 ; 530 531// To derive this, write the output assignments in terms of 532// a/b/c/d, then factor out! 533let ac_sum = a + c ; 534let ac_diff = a - c ; 535let bd_sum = b + d ; 536let bd_diff_ni = mul_ni ( b - d ) ; 537 538* output_ptr . add ( start_idx + k * stride ) = ac_sum + bd_sum ; 539* output_ptr . add ( start_idx + ( k + big_n / 4 ) * stride ) = ac_diff + bd_diff_ni ; 540* output_ptr . add ( start_idx + ( k + big_n / 2 ) * stride ) = ac_sum - bd_sum ; 541* output_ptr . add ( start_idx + ( k + 3 * big_n / 4 ) * stride ) = ac_diff - bd_diff_ni ; 542} 543} 544} 545} 546 547#[ inline ( always ) ] 548fn rot_45 < T : Float + FloatConst > ( c : Complex < T > ) -> Complex < T > { 549let s = T :: FRAC_1_SQRT_2 () ; 550// The standard 2D rotation matrix gives: 551// [ cos(pi/4) -sin(pi/4)] [ s -s ] 552// [ sin(pi/4) cos(pi/4)] = [ s s ] 553Complex :: < T > :: new ( c . re - c . im , c . re + c . im ) * s 554} 555 556#[ inline ( always ) ] 557fn rot_90 < T : Float + FloatConst > ( c : Complex < T > ) -> Complex < T > { 558// The standard 2D rotation matrix gives: 559// [ cos(pi/2) -sin(pi/2)] [ 0 -1 ] 560// [ sin(pi/2) cos(pi/2)] = [ 1 0 ] 561Complex :: < T > :: new ( - c . im , c . re ) 562} 563 564#[ inline ( always ) ] 565fn rot_180 < T : Float + FloatConst > ( c : Complex < T > ) -> Complex < T > { 566// The standard 2D rotation matrix gives: 567// [ cos(pi) -sin(pi)] [ -1 0 ] 568// [ sin(pi) cos(pi)] = [ 0 -1 ] 569Complex :: < T > :: new ( - c . re , - c . im ) 570} 571 572#[ inline ( always ) ] 573fn rot_270 < T : Float + FloatConst > ( c : Complex < T > ) -> Complex < T > { 574// The standard 2D rotation matrix gives: 575// [ cos(3pi/2) -sin(3pi/2)] [ 0 1 ] 576// [ sin(3pi/2) cos(3pi/2)] = [ -1 0 ] 577Complex :: < T > :: new ( c . im , - c . re ) 578} 579 580fn fft_butterfly_radix_8_unsafe < T : Float + FloatConst > ( 581input : & mut [ Complex < T > ] , 582output : & mut [ Complex < T > ] , 583stride : usize , 584big_n : usize , 585twiddles : & [ Complex < T > ] , 586_ : & ExecutionStage < T > , 587) { 588let input_ptr = input . as_ptr () ; 589let output_ptr = output . as_mut_ptr () ; 590for start_idx in 0 .. stride { 591for k in 0 .. big_n / 8 { 592unsafe { 593// Collect inputs. 594let i0 = * input_ptr . add ( start_idx + 8 * k * stride ) ; 595let i1 = * input_ptr . add ( start_idx + ( 8 * k + 1 ) * stride ) ; 596let i2 = * input_ptr . add ( start_idx + ( 8 * k + 2 ) * stride ) ; 597let i3 = * input_ptr . add ( start_idx + ( 8 * k + 3 ) * stride ) ; 598let i4 = * input_ptr . add ( start_idx + ( 8 * k + 4 ) * stride ) ; 599let i5 = * input_ptr . add ( start_idx + ( 8 * k + 5 ) * stride ) ; 600let i6 = * input_ptr . add ( start_idx + ( 8 * k + 6 ) * stride ) ; 601let i7 = * input_ptr . add ( start_idx + ( 8 * k + 7 ) * stride ) ; 602 603// Collect relevant twiddles. 604let ot1 = twiddles . get_unchecked ( 1 * k * stride ) ; 605let ot2 = twiddles . get_unchecked ( 2 * k * stride ) ; 606let ot3 = twiddles . get_unchecked ( 3 * k * stride ) ; 607let ot4 = twiddles . get_unchecked ( 4 * k * stride ) ; 608let ot5 = twiddles . get_unchecked ( 5 * k * stride ) ; 609let ot6 = twiddles . get_unchecked ( 6 * k * stride ) ; 610let ot7 = twiddles . get_unchecked ( 7 * k * stride ) ; 611 612let a = i0 ; 613let b = ot1 * i1 ; 614let c = ot2 * i2 ; 615let d = ot3 * i3 ; 616let e = ot4 * i4 ; 617let f = ot5 * i5 ; 618let g = ot6 * i6 ; 619let h = ot7 * i7 ; 620 621let ae_sum = a + e ; 622let ae_diff = a - e ; 623let bf_sum = b + f ; 624let bf_diff = b - f ; 625let cg_sum = c + g ; 626let cg_diff = c - g ; 627let dh_sum = d + h ; 628let dh_diff = d - h ; 629 630let w00 = ae_sum + cg_sum ; 631let w01 = ae_sum - cg_sum ; 632let w10 = ae_diff + rot_270 ( cg_diff ) ; 633let w11 = ae_diff - rot_270 ( cg_diff ) ; 634let x00 = bf_sum + dh_sum ; 635let x01 = rot_270 ( bf_sum ) + rot_90 ( dh_sum ) ; 636let x10 = rot_45 ( rot_270 ( bf_diff ) + rot_180 ( dh_diff )) ; 637let x11 = rot_45 ( rot_180 ( bf_diff ) + rot_270 ( dh_diff )) ; 638 639* output_ptr . add ( start_idx + k * stride ) = w00 + x00 ; 640* output_ptr . add ( start_idx + ( k + big_n / 8 ) * stride ) = w10 + x10 ; 641* output_ptr . add ( start_idx + ( k + big_n / 4 ) * stride ) = w01 + x01 ; 642* output_ptr . add ( start_idx + ( k + 3 * big_n / 8 ) * stride ) = w11 + x11 ; 643* output_ptr . add ( start_idx + ( k + big_n / 2 ) * stride ) = w00 - x00 ; 644* output_ptr . add ( start_idx + ( k + 5 * big_n / 8 ) * stride ) = w10 - x10 ; 645* output_ptr . add ( start_idx + ( k + 3 * big_n / 4 ) * stride ) = w01 - x01 ; 646* output_ptr . add ( start_idx + ( k + 7 * big_n / 8 ) * stride ) = w11 - x11 ; 647} 648} 649} 650} 651 652fn fft_butterfly_radix_8_s0_unsafe < T : Float + FloatConst > ( 653input : & mut [ Complex < T > ] , 654output : & mut [ Complex < T > ] , 655_ : & Matrix < Complex < T >> , 656) { 657let stride = input . len () / 8 ; 658let big_n = 8 ; 659let input_ptr = input . as_ptr () ; 660let output_ptr = output . as_mut_ptr () ; 661for start_idx in 0 .. stride { 662for k in 0 .. big_n / 8 { 663unsafe { 664// Collect inputs. 665let i0 = * input_ptr . add ( start_idx + 8 * k * stride ) ; 666let i1 = * input_ptr . add ( start_idx + ( 8 * k + 1 ) * stride ) ; 667let i2 = * input_ptr . add ( start_idx + ( 8 * k + 2 ) * stride ) ; 668let i3 = * input_ptr . add ( start_idx + ( 8 * k + 3 ) * stride ) ; 669let i4 = * input_ptr . add ( start_idx + ( 8 * k + 4 ) * stride ) ; 670let i5 = * input_ptr . add ( start_idx + ( 8 * k + 5 ) * stride ) ; 671let i6 = * input_ptr . add ( start_idx + ( 8 * k + 6 ) * stride ) ; 672let i7 = * input_ptr . add ( start_idx + ( 8 * k + 7 ) * stride ) ; 673 674let a = i0 ; 675let b = i1 ; 676let c = i2 ; 677let d = i3 ; 678let e = i4 ; 679let f = i5 ; 680let g = i6 ; 681let h = i7 ; 682 683let ae_sum = a + e ; 684let ae_diff = a - e ; 685let bf_sum = b + f ; 686let bf_diff = b - f ; 687let cg_sum = c + g ; 688let cg_diff = c - g ; 689let dh_sum = d + h ; 690let dh_diff = d - h ; 691 692let w00 = ae_sum + cg_sum ; 693let w01 = ae_sum - cg_sum ; 694let w10 = ae_diff + rot_270 ( cg_diff ) ; 695let w11 = ae_diff - rot_270 ( cg_diff ) ; 696let x00 = bf_sum + dh_sum ; 697let x01 = rot_270 ( bf_sum ) + rot_90 ( dh_sum ) ; 698let x10 = rot_45 ( rot_270 ( bf_diff ) + rot_180 ( dh_diff )) ; 699let x11 = rot_45 ( rot_180 ( bf_diff ) + rot_270 ( dh_diff )) ; 700 701* output_ptr . add ( start_idx + k * stride ) = w00 + x00 ; 702* output_ptr . add ( start_idx + ( k + big_n / 8 ) * stride ) = w10 + x10 ; 703* output_ptr . add ( start_idx + ( k + big_n / 4 ) * stride ) = w01 + x01 ; 704* output_ptr . add ( start_idx + ( k + 3 * big_n / 8 ) * stride ) = w11 + x11 ; 705* output_ptr . add ( start_idx + ( k + big_n / 2 ) * stride ) = w00 - x00 ; 706* output_ptr . add ( start_idx + ( k + 5 * big_n / 8 ) * stride ) = w10 - x10 ; 707* output_ptr . add ( start_idx + ( k + 3 * big_n / 4 ) * stride ) = w01 - x01 ; 708* output_ptr . add ( start_idx + ( k + 7 * big_n / 8 ) * stride ) = w11 - x11 ; 709} 710} 711} 712} 713 714struct Matrix < T > { 715data : Vec < T > , 716rows : usize , 717cols : usize , 718} 719 720impl < T : Clone + Default > Matrix < T > { 721pub fn new ( rows : usize , cols : usize ) -> Self { 722let data = vec ! [ T :: default () ; rows * cols ] ; 723Self { data , rows , cols } 724} 725} 726 727// Reference: https://doc.rust-lang.org/std/ops/trait.Index.html 728impl < T > std :: ops :: Index < ( usize , usize ) > for Matrix < T > { 729type Output = T ; 730fn index ( & self , ( r , c ) : ( usize , usize )) -> & T { 731& self . data [ r * self . cols + c ] 732} 733} 734 735impl < T > std :: ops :: IndexMut < ( usize , usize ) > for Matrix < T > { 736fn index_mut ( & mut self , ( r , c ) : ( usize , usize )) -> & mut T { 737& mut self . data [ r * self . cols + c ] 738} 739} 740 741pub struct ExecutionPlan < T : Float + FloatConst > { 742pub twiddles : Vec < Complex < T >> , 743pub stages : Vec < ExecutionStage < T >> , 744pub scratch : Vec < Complex < T >> , 745} 746 747pub struct ExecutionStage < T : Float + FloatConst > { 748radix : usize , 749butterfly : 750fn ( & mut [ Complex < T > ] , & mut [ Complex < T > ] , usize , usize , & [ Complex < T > ] , & ExecutionStage < T > ) , 751butterfly_s0 : fn ( & mut [ Complex < T > ] , & mut [ Complex < T > ] , & Matrix < Complex < T >> ) , 752// TODO explain wtf this is. Link to article? 753outer_terms : Matrix < Complex < T >> , 754} 755 756impl < T : Clone + Default + Float + FloatConst > ExecutionPlan < T > { 757pub fn new ( data : & [ Complex < T > ]) -> anyhow :: Result < Self > { 758let data_len = data . len () ; 759 760//let available_radices = [9, 7, 8, 5, 4, 3, 2]; 761let available_radices = [ 2 ] ; 762let mut stages = Vec :: new () ; 763let mut len = data_len ; 764while len > 1 { 765let mut got_match = false ; 766for radix in & available_radices { 767if len % radix == 0 { 768let butterflies = Self :: radix_to_butterfly ( * radix ) ? ; 769stages . push ( ExecutionStage { 770radix : * radix , 771butterfly : butterflies . 0 , 772butterfly_s0 : butterflies . 1 , 773outer_terms : Self :: generate_radix_outer_terms ( * radix ) , 774}) ; 775len /= radix ; 776got_match = true ; 777break ; 778} 779} 780if ! got_match { 781anyhow :: bail ! ( "Could not factor input length {}" , len ) ; 782} 783} 784 785let twiddles = precompute_twiddles :: < T > ( data_len ) ; 786let scratch : Vec < Complex < T >> = vec ! [ Complex :: new ( T :: zero () , T :: zero ()) ; data . len ()] ; 787Ok ( Self { 788twiddles , 789stages , 790scratch , 791}) 792} 793 794fn generate_radix_outer_terms ( radix : usize ) -> Matrix < Complex < T >> { 795assert ! ( radix > 1 , "radix must be greater than 1" ) ; 796let mut data = Matrix :: < Complex < T >> :: new ( radix , radix ) ; 797for row in 0 .. radix { 798for col in 0 .. radix { 799let k = ( row * col ) % radix ; 800let entry = Complex :: < f64 > :: cis ( - f64 :: TAU () * ( k as f64 ) / ( radix as f64 )) ; 801data . data [ row * radix + col ] = 802Complex :: < T > :: new ( T :: from ( entry . re ) . unwrap () , T :: from ( entry . im ) . unwrap ()) ; 803} 804} 805data 806} 807 808fn radix_to_butterfly ( 809radix : usize , 810) -> anyhow :: Result < ( 811fn ( & mut [ Complex < T > ] , & mut [ Complex < T > ] , usize , usize , & [ Complex < T > ] , & ExecutionStage < T > ) , 812fn ( & mut [ Complex < T > ] , & mut [ Complex < T > ] , & Matrix < Complex < T >> ) , 813) > { 814match radix { 8158 => Ok (( 816fft_butterfly_radix_8_unsafe , 817fft_butterfly_radix_8_s0_unsafe , 818)) , 8194 => Ok (( 820fft_butterfly_radix_4_unsafe , 821fft_butterfly_radix_4_s0_unsafe , 822)) , 8232 => Ok (( 824fft_butterfly_radix_2_unrolled_unsafe :: < T , 4 > , 825fft_butterfly_radix_2_unrolled_s0_unsafe :: < T , 4 > , 826)) , 8279 => Ok (( 828fft_butterfly_radix_9_unsafe , 829fft_butterfly_radix_9_s0_unsafe , 830)) , 8313 => Ok (( 832fft_butterfly_radix_3_unsafe , 833fft_butterfly_radix_3_s0_unsafe , 834)) , 8355 => Ok (( 836fft_butterfly_radix_p_unsafe :: < 5 , T > , 837fft_butterfly_radix_p_s0_unsafe :: < 5 , T > , 838)) , 8397 => Ok (( 840fft_butterfly_radix_p_unsafe :: < 7 , T > , 841fft_butterfly_radix_p_s0_unsafe :: < 7 , T > , 842)) , 843_ => anyhow :: bail ! ( "Unsupported radix {}" , radix ) , 844} 845} 846} 847 848pub fn fft < T : Float + FloatConst > ( src : & mut [ Complex < T > ] , plan : & mut ExecutionPlan < T > ) { 849let scratch = plan . scratch . as_mut_slice () ; 850assert_eq ! ( src . len () , scratch . len ()) ; 851assert_eq ! ( plan . twiddles . len () , src . len ()) ; 852 853let n_iter = plan . stages . len () ; 854if n_iter % 2 == 1 { 855scratch . copy_from_slice ( src ) ; 856} 857 858let ( mut input , mut output ) = if n_iter % 2 == 0 { 859( scratch , src ) 860} else { 861( src , scratch ) 862} ; 863 864let big_n = input . len () ; 865let mut stride = big_n ; 866let mut big_n = 1 ; 867for ( stage_idx , stage ) in plan . stages . iter () . enumerate () { 868stride /= stage . radix ; 869big_n *= stage . radix ; 870std :: mem :: swap ( & mut input , & mut output ) ; 871 872if stage_idx == 0 { 873( stage . butterfly_s0 )( input , output , & stage . outer_terms ) ; 874} else { 875( stage . butterfly )( input , output , stride , big_n , & plan . twiddles , & stage ) ; 876} 877} 878} 879 880#[ cfg ( test ) ] 881mod tests { 882use rand :: RngExt ; 883use rand :: SeedableRng ; 884use rand :: rngs :: StdRng ; 885 886use super :: * ; 887use num :: complex :: Complex ; 888use std :: time :: Instant ; 889 890// Cast one float type to another, truncating if needed. 891fn t0_to_t1 < T0 : Float , T1 : Float > ( val : T0 ) -> T1 { 892return num :: cast ( val ) . unwrap () ; 893} 894 895// Casts one Complex<T> to another. 896fn complex_to_t < T0 : Float , T1 : Float > ( data : & [ Complex < T0 > ]) -> Vec < Complex < T1 >> { 897data . iter () 898. map ( | c | Complex :: new ( t0_to_t1 ( c . re ) , t0_to_t1 ( c . im ))) 899. collect () 900} 901 902fn evaluate_results ( 903result_ref_64 : & Vec < Complex < f64 >> , 904result_cur_64 : & Vec < Complex < f64 >> , 905duration_dft : std :: time :: Duration , 906duration_cur : std :: time :: Duration , 907algo_name : & str , 908) { 909assert_eq ! ( result_ref_64 . len () , result_cur_64 . len ()) ; 910 911println ! ( " Algorithm: {}" , algo_name ) ; 912println ! ( " Duration: {:?}" , duration_cur ) ; 913println ! ( 914" Speedup: {:?}" , 915duration_dft . as_nanos () as f32 / duration_cur . as_nanos () as f32 916) ; 917 918let mut max_err : f64 = 0.0 ; 919let mut sum_err : f64 = 0.0 ; 920// TODO: median, 90p, 99p 921 922let result_64 : Vec < Complex < f64 >> = complex_to_t ( & result_cur_64 ) ; 923 924for i in 0 .. result_ref_64 . len () { 925max_err = max_err . max (( result_ref_64 [ i ] - result_64 [ i ]) . norm ()) ; 926sum_err += ( result_ref_64 [ i ] - result_64 [ i ]) . norm () ; 927} 928println ! ( " Max err: {}" , max_err ) ; 929println ! ( " Avg err: {}" , sum_err / ( result_ref_64 . len () as f64 )) ; 930} 931 932fn evaluate_size ( size : u32 ) { 933println ! ( "size={}" , size ) ; 934 935let mut data : Vec < Complex < f64 >> = Vec :: new () ; 936data . resize ( size as usize , Complex :: ZERO ) ; 937let mut rng = StdRng :: seed_from_u64 ( 42 ) ; 938for i in 0 .. data . len () { 939data [ i ] = Complex :: new ( rng . random () , rng . random ()) ; 940} 941 942let mut result_ref : Vec < rustfft :: num_complex :: Complex < f64 >> = complex_to_t ( & data ) ; 943let mut ref_planner = rustfft :: FftPlanner :: < f64 > :: new () ; 944let ref_fft = ref_planner . plan_fft_forward ( result_ref . len ()) ; 945let begin_instant = Instant :: now () ; 946ref_fft . process ( & mut result_ref ) ; 947let duration_ref = begin_instant . elapsed () ; 948let result_ref_64 : Vec < Complex < f64 >> = complex_to_t ( & result_ref ) ; 949evaluate_results ( 950& result_ref_64 , 951& result_ref_64 , 952std :: time :: Duration :: from_secs ( 0 ) , 953duration_ref , 954"REF" , 955) ; 956 957let mut result_rfft : Vec < rustfft :: num_complex :: Complex < f32 >> = complex_to_t ( & data ) ; 958let mut rfftner = rustfft :: FftPlanner :: < f32 > :: new () ; 959let rfft_fft = rfftner . plan_fft_forward ( result_rfft . len ()) ; 960let mut begin_instant = Instant :: now () ; 961rfft_fft . process ( & mut result_rfft ) ; 962let duration_rfft = begin_instant . elapsed () ; 963let result_rfft_64 : Vec < Complex < f64 >> = complex_to_t ( & result_rfft ) ; 964evaluate_results ( 965& result_ref_64 , 966& result_rfft_64 , 967std :: time :: Duration :: from_secs ( 0 ) , 968duration_rfft , 969"RFFT" , 970) ; 971 972let mut result_fft = complex_to_t ( & data ) ; 973let mut plan = ExecutionPlan :: < f32 > :: new ( & result_fft ) . unwrap () ; 974begin_instant = Instant :: now () ; 975fft ( & mut result_fft , & mut plan ) ; 976let duration_fft = begin_instant . elapsed () ; 977let result_fft_64 : Vec < Complex < f64 >> = complex_to_t ( & result_fft ) ; 978evaluate_results ( 979& result_ref_64 , 980& result_fft_64 , 981std :: time :: Duration :: from_secs ( 0 ) , 982duration_fft , 983"FFT v8 (plan)" , 984) ; 985} 986 987#[ test ] 988fn naive_fft_matches_ref () { 989for exponent in 4 .. 14 { 990evaluate_size ( 2u32 . pow ( exponent )) ; 991} 992/* 993evaluate_size(3u32.pow(3)); // 27 994evaluate_size(3u32.pow(4)); // 81 995evaluate_size(3u32.pow(5)); // 243 996evaluate_size(3u32.pow(6)); // 729 997evaluate_size(3u32.pow(7)); // 2187 998evaluate_size(3u32.pow(8)); // 6561 999 1000evaluate_size(5u32.pow(2)); // 25 1001evaluate_size(5u32.pow(3)); // 125 1002evaluate_size(5u32.pow(4)); // 625 1003evaluate_size(5u32.pow(5)); // 3125 1004 1005evaluate_size(7u32.pow(2)); // 49 1006evaluate_size(7u32.pow(3)); // 343 1007evaluate_size(7u32.pow(4)); // 2401 1008*/ 1009} 1010}