From 7a583674339ab469805be4027bd5982a14816a76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camille=20Moni=C3=A8re?= Date: Mon, 14 Feb 2022 16:10:10 +0100 Subject: [PATCH] New self-explanatory examples --- examples/ap_int/Makefile | 20 ++++++++++++++++++-- examples/ap_int/ap_int_manip_ex.cpp | 20 ++++++++++++++++++++ examples/ap_int/ap_int_manip_ex2.cpp | 17 +++++++++++++++++ examples/ap_int/ap_int_manip_ex3.cpp | 18 ++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 examples/ap_int/ap_int_manip_ex.cpp create mode 100644 examples/ap_int/ap_int_manip_ex2.cpp create mode 100644 examples/ap_int/ap_int_manip_ex3.cpp diff --git a/examples/ap_int/Makefile b/examples/ap_int/Makefile index 7b343a2..4ef871f 100644 --- a/examples/ap_int/Makefile +++ b/examples/ap_int/Makefile @@ -1,5 +1,6 @@ # # Copyright 2019 Xilinx, Inc. +# Copyright 2022 DrasLorus # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,10 +14,25 @@ # See the License for the specific language governing permissions and # limitations under the License. # + +all: a.out ex1.out ex2.out ex3.out + a.out: test.cpp - g++ -I../../include test.cpp -std=c++11 + g++ -I../../include test.cpp -std=c++14 -o a.out ./a.out +ex1.out: ap_int_manip_ex.cpp + g++ -I../../include ap_int_manip_ex.cpp -std=c++14 -o ex1.out + ./ex1.out + +ex2.out: ap_int_manip_ex2.cpp + g++ -I../../include ap_int_manip_ex2.cpp -std=c++14 -o ex2.out + ./ex2.out + +ex3.out: ap_int_manip_ex3.cpp + g++ -I../../include ap_int_manip_ex3.cpp -std=c++14 -o ex3.out + ./ex3.out + .PHONY: clean clean: - rm -rf a.out + rm -rf *.out diff --git a/examples/ap_int/ap_int_manip_ex.cpp b/examples/ap_int/ap_int_manip_ex.cpp new file mode 100644 index 0000000..3dc5231 --- /dev/null +++ b/examples/ap_int/ap_int_manip_ex.cpp @@ -0,0 +1,20 @@ +#include +#include +using namespace std; + +int main(int, char **) { + const ap_int<16> A = -3987; + const ap_int<16> s_A = ~(A >> 2) + 1; + + cout << A << " " << s_A << " " << round(3987. / 4) << endl; + + const bool Ri[2] = {false, true}; + const ap_int<16> R_val[2] = {-int8_t(Ri[0]), -int8_t(Ri[1])}; + + const ap_int<16> ps_A = (R_val[0] ^ (A >> 2)) + Ri[0]; + const ap_int<16> ms_A = (R_val[1] ^ (A >> 2)) + Ri[1]; + + cout << ps_A << " " << ms_A << endl; + + return 0; +} diff --git a/examples/ap_int/ap_int_manip_ex2.cpp b/examples/ap_int/ap_int_manip_ex2.cpp new file mode 100644 index 0000000..34182b8 --- /dev/null +++ b/examples/ap_int/ap_int_manip_ex2.cpp @@ -0,0 +1,17 @@ +#include +#include +#include +using namespace std; + +int main(int, char **) { + const ap_int<32> A = 0xDEADBEEF; + const ap_int<32> s_A = ~(A >> 2) + 1; + + cout << A << " " << s_A << " " << (int64_t) round(-1. * A.to_float() / 4.) << endl; + + const ap_int<16> dead = A(31, 16); + const ap_int<16> beaf = A(15, 0); + printf("%08x || %04x || %04x\n", A.to_uint(), dead.to_int(), beaf.to_int()); + + return 0; +} diff --git a/examples/ap_int/ap_int_manip_ex3.cpp b/examples/ap_int/ap_int_manip_ex3.cpp new file mode 100644 index 0000000..ff553e1 --- /dev/null +++ b/examples/ap_int/ap_int_manip_ex3.cpp @@ -0,0 +1,18 @@ +#include +#include +#include +using namespace std; + +int main(int, char **) { + const ap_int<8> A = 79; // 0x4F = 79 + const ap_int<4> msb_A_4bits = A(7, 4); // 0x4 = 4 + const ap_int<4> lsb_A_4bits = A(3, 0); // 0xF = -1 + + const ap_int<8> msb_A_8bits = A(7, 4); // 0x04 = 4 + const ap_int<8> lsb_A_8bits = A(3, 0); // 0x0F = 15 + + printf("%d || %d || %d\n", A.to_int(), msb_A_4bits.to_int(), lsb_A_4bits.to_int()); + printf("%d || %d || %d\n", A.to_int(), msb_A_8bits.to_int(), lsb_A_8bits.to_int()); + + return 0; +}