|
From: | Alice Osako |
Subject: | Portable bitwise operations library (was Re: Portability Considerations) |
Date: | Mon, 18 Mar 2024 23:40:20 -0400 |
User-agent: | Mozilla Thunderbird |
https://github.com/Schol-R-LEA/Modula-2-Portable-Bit-Manipulation/tree/mainThere has to be a better solution, IMO, but this is what I was able to come up with without resorting to either the GCC-specific libraries, or importing assembly code. Even then, the claim to being 'portable' is probably questionable at best. The fact that there is a constant which defines the word width, which would have to be manually modified for the correct value, makes the question of 'portability' pointless.
And yes, I did have to resort to using variant record aliasing to write the two specialized cast utility functions, and worse, to looping on the bits for the shifts. I am truly uncomfortable with this approach, but couldn't think of a better one within the constraints of generic ISO code.
I will get to ASHR, ROTL, and ROTR eventually, but this is about the most I can manage tonight. Similarly, a GCC specific version, hopefully less grotesque, will have to wait, as will a more comprehensive suite of tests.
Perhaps someone need to write a unit test framework for Modula-2, but that won't be me right now.
By the way, does the GCC implementation of Modula-2 support inline assembly, or would I have to wrap the assembly/C code into functions and import them? Neither solution really appeals to me, but they would be better than the unholy mockery I've already written. I know that the ByteBitOps and WordBitOps modules exist, but those are undocumented and that's the last thing I need to be messing with right now.
I am not certain how I would proceed with a version that is compatible with PIM (any edition); it is probably something that would have to be done on a per-implementation basis.
[Prev in Thread] | Current Thread | [Next in Thread] |