Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add standard macros to handle common operations #760

Open
pierrejoye opened this issue Sep 11, 2021 · 3 comments
Open

Add standard macros to handle common operations #760

pierrejoye opened this issue Sep 11, 2021 · 3 comments
Assignees

Comments

@pierrejoye
Copy link
Contributor

pierrejoye commented Sep 11, 2021

We have the same operations across the sources, sometimes using the same method, sometimes not. As it may not be an issue for some, it creates disparities and inconsistencies, especially if it comes to channels calculations. Many times we don't follow standards (or lack of), like differences between integer casting on intel or arm, or various other architectures.

We have a few macros or functions already, however I would like to use gd_intern.h to implement the most common ones and unify their usages everywhere as well as enforce their usage for all new codes.

It has three main advantages:

  1. Centralized implementation and add them to the CS
  2. testable, we can add tests with an include "../src/gd_intern.h" and run tests across various compilers and architectures
  3. Various implementations like filters, 2D transformations, etc will be predictable and have uniform channel rounding

The 1st ones I had in mind are:

gd intern

clamp255
clamp127
clamp(input, min, max)

double

roundd
roundd_cast_uint8
roundd_cast_uint16
roundd_cast_uint32
roundd_cast_uint64

roundd
roundupd_cast_uint8
roundupd_cast_uint16
roundupd_cast_uint32
roundupd_cast_uint64

floord
floord_cast_uint8
floord_cast_uint16
floord_cast_uint32
floord_cast_uint64

float

roundf
roundf_cast_uint8
roundf_cast_uint16
roundf_cast_uint32
roundf_cast_uint64

roundupf
roundupf_cast_uint8
roundupf_cast_uint16
roundupf_cast_uint32
roundupf_cast_uint64

floorf
floorf_cast_uint8
floorf_cast_uint16
floorf_cast_uint32
floorf_cast_uint64

We could add as well the same but taking an uint32_t argb, the uint32_t 31 bit color, or both the quadlet and triplet (r,g,b)/(a,r,g,b).

And indeed, the same for the fixed point (bith fp32 and fp64). However we need to unify the FP APIs as well. And use the same implementations everywhere.

What do you think?

@vapier
Copy link
Member

vapier commented Sep 11, 2021

centralizing helpers in gd_intern.h sgtm

i think changing "cast_uint8" to "_u8" doesn't really lose clarity ?

@pierrejoye
Copy link
Contributor Author

That should be ok. I tend to prefer self documentation about what it accepts and returns. I don't mind either one. :)

I did not create a task for it but I would like to enforce usage of stdint types as well.

@cmb69
Copy link
Contributor

cmb69 commented Dec 14, 2021

Yes, that sounds good to me either. We should not forget about

/* only used here, let do a generic fixed point integers later if required by other
part of GD */
typedef long gdFixed;
/* Integer to fixed point */
#define gd_itofx(x) ((x) << 8)
/* Float to fixed point */
#define gd_ftofx(x) (long)((x) * 256)
/* Double to fixed point */
#define gd_dtofx(x) (long)((x) * 256)
/* Fixed point to integer */
#define gd_fxtoi(x) ((x) >> 8)
/* Fixed point to float */
# define gd_fxtof(x) ((float)(x) / 256)
/* Fixed point to double */
#define gd_fxtod(x) ((double)(x) / 256)
/* Multiply a fixed by a fixed */
#define gd_mulfx(x,y) (((x) * (y)) >> 8)
/* Divide a fixed by a fixed */
#define gd_divfx(x,y) (((x) << 8) / (y))

Maybe we should drop that altogether, or at least use int64_t instead of long on 64bit (for LLP64).

@cmb69 cmb69 removed their assignment Dec 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants