-
Notifications
You must be signed in to change notification settings - Fork 33
Implement floating-point Fast Fourier Transform #210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ethanuppal
wants to merge
20
commits into
intel:main
Choose a base branch
from
ethanuppal:fft2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
87ca92b
Add bit reversal component for 1D arrays
ethanuppal 492150c
Add complex floating point module
ethanuppal b5e4895
Add butterfly module
ethanuppal cc2059c
WIP butterfly stage
ethanuppal e2463a1
backup
ethanuppal 468abe4
Fix butterfly test
ethanuppal 51e4e7c
Fix typo in butterfly and add tags to connectIO
ethanuppal f2ee651
Confused?
ethanuppal 7712bf8
Remove extraneous file
ethanuppal 0b8c3bf
For good measure
ethanuppal 7481def
backup
ethanuppal 823c57b
Fix weird error
ethanuppal f88400b
Fix memory harness
ethanuppal f634b18
Rework interface
ethanuppal 972fcb1
switch to connectIntf
ethanuppal 2537fcb
Finish fft test
ethanuppal 7c1ad04
print stuff
ethanuppal dffb187
stuff
ethanuppal ca5422f
Merge remote-tracking branch 'upstream/main' into fft2
ethanuppal bc4f828
ok
ethanuppal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright (C) 2025 Intel Corporation | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| import 'package:rohd/rohd.dart'; | ||
|
|
||
| // class FFT extends Module { | ||
| // LogicArray get out => output('out') as LogicArray; | ||
| // | ||
| // FFT(Logic en, Logic clk, Logic reset, LogicArray input, {super.name = 'fft'}) | ||
| // : assert(input.dimensions.length == 1) { | ||
| // final int length = input.dimensions[0]; | ||
| // if ((length & (~(length - 1))) != length) { | ||
| // assert(false); | ||
| // } | ||
| // final int log2Length = log2Ceil(length); | ||
| // | ||
| // input = addInputArray( | ||
| // 'input_array', | ||
| // input, | ||
| // dimensions: input.dimensions, // it seems like these are needed | ||
| // elementWidth: input.elementWidth, | ||
| // numUnpackedDimensions: input.numUnpackedDimensions, | ||
| // ); | ||
| // | ||
| // List<LogicArray> stageArrays = List.generate( | ||
| // log2Length + 1, | ||
| // (stage) => LogicArray( | ||
| // input.dimensions, | ||
| // input.elementWidth, | ||
| // name: 'stage${stage}Array', | ||
| // numUnpackedDimensions: input.numUnpackedDimensions, | ||
| // ), | ||
| // ); | ||
| // | ||
| // LogicArray out = addOutputArray( | ||
| // 'out', | ||
| // dimensions: input.dimensions, | ||
| // elementWidth: input.elementWidth, | ||
| // numUnpackedDimensions: input.numUnpackedDimensions, | ||
| // ); | ||
| // out <= stageArrays[log2Length]; | ||
| // | ||
| // List<List<Conditional> Function(PipelineStageInfo)> fftStages = []; | ||
| // | ||
| // fftStages.add((p) => [stageArrays[0] < BitReverse(input).out]); | ||
| // | ||
| // for (var s = 1; s <= log2Length; s++) { | ||
| // final m = 1 << s; | ||
| // final mShift = log2Ceil(m); | ||
| // | ||
| // Counter i = Counter(en, reset, clk, width: log2Length - 1); | ||
| // | ||
| // Logic k = (i.val >> (mShift - 1)) << mShift; | ||
| // Logic j = (i.val & Const((m >> 1) - 1, width: i.width)); | ||
| // } | ||
| // | ||
| // // ReadyValidPipeline() | ||
| // | ||
| // // for s = 1 to log(n) do | ||
| // // m ← 2s | ||
| // // ωm ← exp(−2πi/m) | ||
| // // for k = 0 to n-1 by m do | ||
| // // ω ← 1 | ||
| // // for j = 0 to m/2 – 1 do | ||
| // // t ← ω A[k + j + m/2] | ||
| // // u ← A[k + j] | ||
| // // A[k + j] ← u + t | ||
| // // A[k + j + m/2] ← u – t | ||
| // // ω ← ω ωm | ||
| // } | ||
| // } |
83 changes: 83 additions & 0 deletions
83
lib/src/arithmetic/signals/floating_point_logics/complex_floating_point_logic.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // Copyright (C) 2024-2025 Intel Corporation | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| import 'package:meta/meta.dart'; | ||
| import 'package:rohd/rohd.dart'; | ||
| import 'package:rohd_hcl/rohd_hcl.dart'; | ||
|
|
||
| class ComplexFloatingPoint extends LogicStructure { | ||
| final FloatingPoint realPart; | ||
|
|
||
| final FloatingPoint imaginaryPart; | ||
|
|
||
| static String _nameJoin(String? structName, String signalName) { | ||
| if (structName == null) { | ||
| return signalName; | ||
| } | ||
| return '${structName}_$signalName'; | ||
| } | ||
|
|
||
| ComplexFloatingPoint({ | ||
| required int exponentWidth, | ||
| required int mantissaWidth, | ||
| String? name, | ||
| }) : this._internal( | ||
| realPart: FloatingPoint( | ||
| exponentWidth: exponentWidth, | ||
| mantissaWidth: mantissaWidth, | ||
| name: _nameJoin(name, 're'), | ||
| ), | ||
| imaginaryPart: FloatingPoint( | ||
| exponentWidth: exponentWidth, | ||
| mantissaWidth: mantissaWidth, | ||
| name: _nameJoin(name, 'im'), | ||
| ), | ||
| name: name, | ||
| ); | ||
|
|
||
| ComplexFloatingPoint._internal( | ||
| {required this.realPart, required this.imaginaryPart, super.name}) | ||
| : assert(realPart.exponent.width == imaginaryPart.exponent.width), | ||
| assert(realPart.mantissa.width == imaginaryPart.mantissa.width), | ||
| super([realPart, imaginaryPart]); | ||
|
|
||
| @mustBeOverridden | ||
| @override | ||
| ComplexFloatingPoint clone({String? name}) => ComplexFloatingPoint( | ||
| exponentWidth: realPart.exponent.width, | ||
| mantissaWidth: realPart.mantissa.width, | ||
| name: name, | ||
| ); | ||
|
|
||
| ComplexFloatingPoint adder(ComplexFloatingPoint other) => | ||
| ComplexFloatingPoint._internal( | ||
| realPart: FloatingPointAdderSinglePath(realPart, other.realPart).sum, | ||
| imaginaryPart: | ||
| FloatingPointAdderSinglePath(imaginaryPart, other.imaginaryPart) | ||
| .sum, | ||
| name: _nameJoin(name, "adder")); | ||
|
|
||
| ComplexFloatingPoint multiplier(ComplexFloatingPoint other) { | ||
| // use only 3 multipliers: https://mathworld.wolfram.com/ComplexMultiplication.html | ||
| final ac = FloatingPointMultiplierSimple(realPart, other.realPart).product; | ||
| final bd = FloatingPointMultiplierSimple(imaginaryPart, other.imaginaryPart) | ||
| .product; | ||
| final abcd = FloatingPointMultiplierSimple( | ||
| FloatingPointAdderSinglePath(realPart, imaginaryPart).sum, | ||
| FloatingPointAdderSinglePath(other.realPart, other.imaginaryPart) | ||
| .sum) | ||
| .product; | ||
|
|
||
| return ComplexFloatingPoint._internal( | ||
| realPart: FloatingPointAdderSinglePath(ac, bd.negated()).sum, | ||
| imaginaryPart: FloatingPointAdderSinglePath(abcd, | ||
| FloatingPointAdderSinglePath(ac.negated(), bd.negated()).sum) | ||
| .sum, | ||
| name: _nameJoin(name, "multiplier")); | ||
| } | ||
|
|
||
| ComplexFloatingPoint negated() => ComplexFloatingPoint._internal( | ||
| realPart: realPart.negated(), | ||
| imaginaryPart: imaginaryPart.negated(), | ||
| name: _nameJoin(name, "negated")); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Copyright (C) 2021-2024 Intel Corporation | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| import 'package:rohd/rohd.dart'; | ||
| import 'package:rohd_hcl/rohd_hcl.dart'; | ||
|
|
||
| int bitReverse(int value, int bits) { | ||
| var reversed = 0; | ||
| for (var i = 0; i < bits; i++) { | ||
| reversed <<= 1; | ||
| reversed |= value & 1; | ||
| value >>= 1; | ||
| } | ||
| return reversed; | ||
| } | ||
|
|
||
| class BitReversal extends Module { | ||
| LogicArray get out => output('out') as LogicArray; | ||
|
|
||
| BitReversal(LogicArray input, {super.name = 'bit_reversal'}) | ||
| : assert(input.dimensions.length == 1, 'Can only bit reverse 1D arrays') { | ||
| input = addInputArray( | ||
| 'input_array', | ||
| input, | ||
| dimensions: input.dimensions, // it seems like these are needed | ||
| elementWidth: input.elementWidth, | ||
| numUnpackedDimensions: input.numUnpackedDimensions, | ||
| ); | ||
|
|
||
| final out = addOutputArray( | ||
| 'out', | ||
| dimensions: input.dimensions, | ||
| elementWidth: input.elementWidth, | ||
| numUnpackedDimensions: input.numUnpackedDimensions, | ||
| ); | ||
|
|
||
| final length = input.dimensions[0]; | ||
| final bits = log2Ceil(length); | ||
|
|
||
| for (var i = 0; i < length; i++) { | ||
| out.elements[bitReverse(i, bits)] <= input.elements[i]; | ||
| } | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
test/arithmetic/floating_point/copmlex_floating_point_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // Copyright (C) 2025 Intel Corporation | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
| // | ||
| // floating_point_test.dart | ||
| // Tests of Floating Point basic types | ||
| // | ||
| // 2024 April 1 | ||
| // Authors: | ||
| // Max Korbel <[email protected]> | ||
| // Desmond A Kirkpatrick <[email protected] | ||
|
|
||
| import 'package:rohd/rohd.dart'; | ||
| import 'package:rohd_hcl/rohd_hcl.dart'; | ||
| import 'package:rohd_hcl/src/arithmetic/signals/floating_point_logics/complex_floating_point_logic.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| ComplexFloatingPoint newComplex(double real, double imaginary) { | ||
| final realFP = FloatingPoint64(); | ||
| final imaginaryFP = FloatingPoint64(); | ||
|
|
||
| final realFPValue = FloatingPoint64Value.populator().ofDouble(real); | ||
| final imaginaryFPValue = FloatingPoint64Value.populator().ofDouble(imaginary); | ||
|
|
||
| realFP.put(realFPValue); | ||
| imaginaryFP.put(imaginaryFPValue); | ||
|
|
||
| final complex = ComplexFloatingPoint( | ||
| exponentWidth: realFP.exponent.width, | ||
| mantissaWidth: realFP.mantissa.width); | ||
| complex.realPart <= realFP; | ||
| complex.imaginaryPart <= imaginaryFP; | ||
|
|
||
| return complex; | ||
| } | ||
|
|
||
| void main() { | ||
| tearDown(() async { | ||
| await Simulator.reset(); | ||
| }); | ||
|
|
||
| test('complex constructor', () { | ||
| final complex = newComplex(1.23, 3.45); | ||
|
|
||
| expect(complex.realPart.floatingPointValue.toDouble(), 1.23); | ||
| expect(complex.imaginaryPart.floatingPointValue.toDouble(), 3.45); | ||
| }); | ||
|
|
||
| test('complex addition', () { | ||
| final a = newComplex(1.0, 0.0); | ||
| final b = newComplex(0.0, -1.0); | ||
| final c = a.adder(b); | ||
|
|
||
| expect(c.realPart.floatingPointValue.toDouble(), 1.0); | ||
| expect(b.imaginaryPart.floatingPointValue.toDouble(), -1.0); | ||
| }); | ||
|
|
||
| test('complex multiplication', () { | ||
| final a = newComplex(1.0, 2.0); | ||
| final b = newComplex(-3.0, -4.0); | ||
| final c = a.multiplier(b); | ||
|
|
||
| expect(c.realPart.floatingPointValue.toDouble(), 5.0); | ||
| expect(c.imaginaryPart.floatingPointValue.toDouble(), -10.0); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not override operator
+and*?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't want to hide the fact that it's expensive
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the names of the functions seem unintuitive to me, maybe
plusandtimesoraddedToandmultipliedByor something?if we wanted to stay more consistent with other parts of the library, these could be their own components (classes, modules) e.g.
ComplexFloatingPointMultiplier, perhaps with an argument that allows you to choose your ownFloatingPointMultiplierimplementation for the internals.