07 July 2021, 11:59 | #1 |
It's coming back!
Join Date: Jul 2018
Location: comp.sys.amiga
Posts: 762
|
Generating FFP values at compile time
I'm working with FFP format floating point numbers, using C++ Bartman's GCC environment, cross compiling from Windows.
I have a type, real, that's really just a LONGwith a bunch of overloaded operators to make it appear almost like an inbuilt type. Everything works, except initialising these objects is awkward. I can't just go: Code:
real pi = 3.14159; Code:
real pi = a2r("3.14159"); At the moment I have a bunch of #defined constants: Code:
#define r_pi 0xC90FDB42UL Does anyone have any smarter ideas? |
07 July 2021, 18:21 | #2 |
Registered User
Join Date: Jan 2019
Location: Germany
Posts: 3,214
|
Well, for Os 3.1.4, we had kinna the reverse problem, namely to let SAS/C accept IEEE single precision constants. The solution there, albeit hacky, was to have a (self-written) preprocessor (not the standard C preprocessor) which took these constants, and converted them to references (i.e. __3_14159), and then later in the build stage, C was compiled to assembler (not object code), and a second script then converted those constants by suitable immediate hex values.
It was really a sort of abuse of the SAS/C compiler chain, and an external "hack" that abused that chain. |
07 July 2021, 19:38 | #3 |
Registered User
Join Date: Jun 2020
Location: Druidia
Posts: 386
|
I've done something similar.
I wrote a wrapper class around the FFP library. Then pulled some helper functions from elsewhere for conversion from and to ieee floats. I also added some trig/math code as needed. For my implementation you can declare an FFP with the following helper function: ffloat value = ffloat_from_ieee(1.2345f); It's relatively fast but it's still better to do this all upfront so it's only slow for the initial setup. You can also create ffloat's from ints: ffloat value = ffloat(1) / ffloat(100); // 0.01 I've shared my class over at: https://github.com/rjobling/ffloat |
07 July 2021, 20:09 | #4 | |
It's coming back!
Join Date: Jul 2018
Location: comp.sys.amiga
Posts: 762
|
Quote:
Trouble mostly is that I'd have to write a preprocessor to run under Windows, and I've never done any Windows development before. |
|
07 July 2021, 20:19 | #5 | |
It's coming back!
Join Date: Jul 2018
Location: comp.sys.amiga
Posts: 762
|
Quote:
One of the problems I have is the static initialisation of flexible array members with that kind of approach ( ffloat_from_ieee or a2f = ffloat from char * ). I'm not sure if it's my code to blame, or a gcc bug. Maybe it would be worthwhile me testing with your approach as well as mine. |
|
07 July 2021, 20:35 | #6 |
Registered User
Join Date: Jun 2020
Location: Druidia
Posts: 386
|
I don't know what you mean by "static initialization of flexible array members"?
Can you show me an example of what you're doing? |
07 July 2021, 22:28 | #7 |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
|
|
07 July 2021, 22:35 | #8 |
Registered User
Join Date: Jun 2020
Location: Druidia
Posts: 386
|
Cool! I hadn't considered trying to get it all to happen at compile time. But that's pretty awesome that it can be made to work.
|
07 July 2021, 22:54 | #9 | |
Registered User
Join Date: Jan 2019
Location: Germany
Posts: 3,214
|
Quote:
First, if it's command line, it's not any different. Just use gcc, and your standard main() to collect command line arguments. Second, you can always use vamos and write an amiga binary. This is what I did as "pre-processor", but it was supposed to operate in an all-amiga environment anyhow. |
|
08 July 2021, 10:41 | #10 | |
It's coming back!
Join Date: Jul 2018
Location: comp.sys.amiga
Posts: 762
|
Quote:
I think I'll look for something else for the text replacement part, maybe I can make do with a sed script. |
|
08 July 2021, 10:48 | #11 | |
It's coming back!
Join Date: Jul 2018
Location: comp.sys.amiga
Posts: 762
|
Quote:
Code:
#include "../real_constants.h" static VertexComponentData yVertexComponentData = { .numTouchValues = 2, .numTotalValues = 4, .values = { r_0_000, r_2_600, r_5_500, r_1_200 } }; Code:
static VertexComponentData yVertexComponentData = { .numTouchValues = 2, .numTotalValues = 4, .values = { a2r("0.000"), a2r("2.600"), a2r("5.500"), a2r("1.200") } }; |
|
08 July 2021, 11:30 | #12 | |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
|
Quote:
|
|
08 July 2021, 11:45 | #13 |
It's coming back!
Join Date: Jul 2018
Location: comp.sys.amiga
Posts: 762
|
|
08 July 2021, 11:51 | #14 | |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
|
Quote:
You have to call the ffpfieee() function and instead of strings supply c++ floats. |
|
08 July 2021, 12:12 | #15 | |
It's coming back!
Join Date: Jul 2018
Location: comp.sys.amiga
Posts: 762
|
Quote:
But, my question was, how will that work with the cross compilation environment I outlined in post #1. The constexpr code needs to be executed, right? But my compiler is running on windows but generating 68000 Amiga code. How can the code be executed at compile time? Edit: I'm learning more about this new constexpr thing, and I'm now seeing that it's not code that is executed, it just allows for really complicated expressions to be evaluated, with a whole bunch of restrictions that change every few years. It will be interesting to see if I can parse a string with it. Last edited by deimos; 08 July 2021 at 12:29. |
|
08 July 2021, 12:50 | #16 |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
|
The site where you see the code is using a cross-compiler toolchain. So it works in cross-compiler for sure.
But why would you want strings? Code:
int a[] = { foo("3.14159") }; Code:
int a[] = { foo(3.14159) }; |
08 July 2021, 13:13 | #17 | |
It's coming back!
Join Date: Jul 2018
Location: comp.sys.amiga
Posts: 762
|
Quote:
|
|
08 July 2021, 13:26 | #18 |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
|
the function ffpfieee is "ffp from ieee"
Check the hex representation here https://godbolt.org/z/fWbzjTbff |
08 July 2021, 13:46 | #19 | |
It's coming back!
Join Date: Jul 2018
Location: comp.sys.amiga
Posts: 762
|
Quote:
|
|
08 July 2021, 14:03 | #20 |
Registered User
Join Date: Dec 2010
Location: Athens/Greece
Age: 53
Posts: 719
|
There shouldn't be different if your values are within FFP range. If they are not then you have other problems. If they are, then the conversion should work.
Last edited by alkis; 08 July 2021 at 14:10. Reason: typos |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Generating an accurate Paula period table | 8bitbubsy | Coders. General | 55 | 07 September 2020 21:04 |
Wanted: C source for converting gcc (linux) float to FFP | alkis | Coders. C/C++ | 5 | 16 August 2020 02:39 |
Integers vs floats (FFP/Sing/Doub) + printf() | guy lateur | Coders. Asm / Hardware | 63 | 18 July 2017 17:57 |
WinUAELoader: generating single game .uae? | Telegattone | support.WinUAE | 1 | 27 December 2016 12:28 |
Software for generating screenshots and videos | Edi (FZ2D) | Retrogaming General Discussion | 5 | 08 April 2010 23:34 |
|
|