Last active
March 14, 2022 10:41
-
-
Save V0XNIHILI/70a28a978a4ccaa364d1a1dbd72b370b to your computer and use it in GitHub Desktop.
PYNQ-Z2 floating point conversions
This file contains 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
void divide (float a, float b, float *c) | |
{ | |
#pragma HLS interface s_axilite port=a | |
#pragma HLS interface s_axilite port=b | |
#pragma HLS interface s_axilite port=c | |
#pragma HLS interface ap_ctrl_none port=return | |
*c = a / b; | |
} |
This file contains 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
#include <iostream> | |
using namespace std; | |
void divide(float a, float b, float *c); | |
int main() | |
{ | |
float a = 42.0f; | |
float b = 3.0f; | |
float c; | |
divide(a, b, &c); | |
std:cout << c << std::endl; | |
return 0; | |
} |
This file contains 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
import struct | |
""" | |
Please note that these functions only work | |
for single number input and output | |
""" | |
def float_to_bytes(a: float) -> bytes: | |
return struct.pack("f", a) | |
def bytes_to_float(a: int) -> float: | |
return struct.unpack("f", struct.pack("i", a))[0] | |
This file contains 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
from pynq import Overlay | |
from pynq import MMIO | |
import struct | |
def float_to_bytes(a: float) -> bytes: | |
return struct.pack("f", a) | |
def bytes_to_float(a: int) -> float: | |
return struct.unpack("f", struct.pack("i", a))[0] | |
divide_overlay = Overlay("../../overlays/divide_test/divide_test.bit") | |
divide_overlay.download() | |
# Divide 42.9 by 3.0, resulting in 14.3 | |
divide_mmio = MMIO(0x43C0_0000, 0x10000) | |
divide_mmio.write(0x10, float_to_bytes(42.9)); | |
divide_mmio.write(0x18, float_to_bytes(3.0)); | |
print(bytes_to_float(divide_mmio.read(0x20))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment