[AD] MID revisited |
[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]
See bug #1640516 for reference and earlier threads for past discussions. In short, the Allegro MID is a misnomer, as it really clamps between x and z with the assumption that x is not greater than z. So, for example, MID(5,y,0) will give unpredictable results. The examples make use of MID, so it really is a public macro, even if it isn't publicly documented (yet). There are two choices: 1) Ignore it, but document it. 2) Rename MID to CLAMP and provide a proper MID. I prefer #2, as it actually addresses the problem. The two drawbacks of #2 are: 1) MID() is bound to be slightly slower than CLAMP(). So people currently using MID() in add-ons, etc should change their code accordingly. The macro I'm proposing only uses two comparisons for the "in order" where x < y < z, which is the same as MID(). It uses three comparisons for all other cases (except x > y > z, which takes two). 2) If such 3rd party code is changed to CLAMP(), the code would need to maintain a version check and #define CLAMP MID if the code is meant to work with 4.2 and previous. (Mainly affects libraries.) Attached is a patch against 4.3.10plus and a test program. You can see the output of the test program here: <http://pastebin.com/f5a19542> So it would be nice to make a definite decision one way or the other regarding this. -- Matthew Leverton
#include <allegro.h> #include <stdio.h> int main() { for (int x=1; x<=4; ++x) { for (int z=1; z<=4; z++) { for (int y=1; y<=4; y++) { printf("MID(%2d,%2d,%2d) = %2d\tCLAMP(%2d,%2d,%2d) = ", x,y,z,MID(x,y,z), x,y,z); if (x <= z) { printf("%2d", CLAMP(x,y,z)); if (MID(x,y,z) != CLAMP(x,y,z)) printf(" - ERROR"); } else printf(" undefined"); printf("\n"); } } } return 0; } END_OF_MAIN()
Attachment:
clamp.diff.gz
Description: GNU Zip compressed data
Mail converted by MHonArc 2.6.19+ | http://listengine.tuxfamily.org/ |