From a21b0ac7254fd215156c5efcf0a6d1d0baa1ae25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Szostak?= Date: Sat, 8 Jun 2024 16:15:21 +0200 Subject: [PATCH] Implemented dumb float to bytes converter --- Makefile | 20 +++++++++++++++----- src/floattobitshelper.c | 8 ++++++++ src/floattobitshelper.h | 1 + src/shardware.adb | 5 ++++- src/shardware.ads | 7 +++++++ 5 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 src/floattobitshelper.c create mode 100644 src/floattobitshelper.h diff --git a/Makefile b/Makefile index 9ceda43..316b90d 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,27 @@ default: sillymachine -sillymachine: ./src/sillymachine.adb ./src/*.adb ./src/*.ads - gnatmake ./src/sillymachine.adb +floattobitshelper: ./src/floattobitshelper.c ./src/floattobitshelper.h + gcc -c src/floattobitshelper.c -o floattobitshelper.o + ar -crs floattobitshelper.a floattobitshelper.o + +floatwin: ./src/floattobitshelper.c ./src/floattobitshelper.h + x86_64-w64-mingw32-gcc -c src/floattobitshelper.c -o floatwin.o -lwinmm -lgdi32 + ar -crs floattobitshelper.a floatwin.o + + +sillymachine: ./src/sillymachine.adb ./src/*.adb ./src/*.ads floattobitshelper + gnatmake ./src/sillymachine.adb -largs -L./ -l:floattobitshelper.a debug: ./src/sillymachine.adb ./src/*.adb ./src/*.ads gnatmake ./src/sillymachine.adb -o sillymachine-debug -g -win: ./src/sillymachine.adb ./src/*.adb ./src/*.ads +win: ./src/sillymachine.adb ./src/*.adb ./src/*.ads floatwin - x86_64-w64-mingw32-gnatmake src/sillymachine.adb -lwinmm -lgdi32 + x86_64-w64-mingw32-gnatmake src/sillymachine.adb -lwinmm -lgdi32 -largs -L./ -l:floattobitshelper.a + all: sillymachine win clean: - rm *.o *.ali sillymachine.exe sillymachine sillymachine-debug + rm *.o *.ali *.a sillymachine.exe sillymachine sillymachine-debug diff --git a/src/floattobitshelper.c b/src/floattobitshelper.c new file mode 100644 index 0000000..d75e1e1 --- /dev/null +++ b/src/floattobitshelper.c @@ -0,0 +1,8 @@ +#include "floattobitshelper.h" +#include + +int floattobitshelper(float x){ + int result = 0; + memcpy(&result, &x, sizeof result); + return result; +} diff --git a/src/floattobitshelper.h b/src/floattobitshelper.h new file mode 100644 index 0000000..ecfc23b --- /dev/null +++ b/src/floattobitshelper.h @@ -0,0 +1 @@ +int floattobitshelper(float x); diff --git a/src/shardware.adb b/src/shardware.adb index 415774c..6d82cae 100644 --- a/src/shardware.adb +++ b/src/shardware.adb @@ -180,8 +180,11 @@ package body Shardware is function FToBytes (Number : Float) return ByteArr is ByteBuff : ByteArr (1 .. 4); + Buffer : Integer := 0; begin - raise Constraint_Error with "Not Implemented!"; + -- raise Constraint_Error with "Not Implemented!"; + Buffer := Integer(FloatToBitsHelper(C_float(Number))); + ByteBuff := IToBytes(Buffer); return ByteBuff; end FToBytes; diff --git a/src/shardware.ads b/src/shardware.ads index 6c23b65..43924b8 100644 --- a/src/shardware.ads +++ b/src/shardware.ads @@ -1,5 +1,6 @@ with Ada.Sequential_IO; with Interfaces; use Interfaces; +with Interfaces.C; use Interfaces.C; package Shardware is @@ -29,6 +30,12 @@ package Shardware is function BytesToI (TheBytes : ByteArr) return Integer; function BytesToF (TheBytes : ByteArr) return Float; + function FloatToBitsHelper (X: C_float) return Int + with + Import => True, + Convention => C, + External_Name => "floattobitshelper"; + function UToBytes (Number : Unsigned_32) return ByteArr; function IToBytes (Number : Integer) return ByteArr; function FToBytes (Number : Float) return ByteArr;