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

opj_uint_adds() is questionable #515

Closed
szukw000 opened this issue Jun 20, 2015 · 1 comment
Closed

opj_uint_adds() is questionable #515

szukw000 opened this issue Jun 20, 2015 · 1 comment
Labels

Comments

@szukw000
Copy link
Contributor

'opj_uintmath.h' contains the lines:

/**
Get the saturated sum of two unsigned integers
@return Returns saturated sum of a+b
*/
static INLINE OPJ_UINT32 opj_uint_adds(OPJ_UINT32 a, OPJ_UINT32 b) {
OPJ_UINT64 sum = (OPJ_UINT64)a + (OPJ_UINT64)b;
return -(OPJ_UINT32)(sum >> 32) | (OPJ_UINT32)sum;
}

'opj_uint_adds()' is always the first value of:

static INLINE OPJ_UINT32 opj_uint_min(OPJ_UINT32 a, OPJ_UINT32 b) {
return a < b ? a : b;
}

This means: the value returned MUST not be negative.

Should the '-' sign in 'opj_uint_adds()' be removed?

winfried

@mayeut
Copy link
Collaborator

mayeut commented Jun 20, 2015

Winfried,

Strictly C speaking, it's questionable. However, I don't know of any hardware that openjpeg will run on that does not use 2's complement arithmetic.

The high 32 bits part of sum is either 1 or 0 depending on overflow.
If an overflow occurs, -(OPJ_UINT32)(sum >> 32) == (OPJ_UINT32)-1 == UINT32_MAX, that's all ones so once ORed with the low part, it's still UINT32_MAX.
If no overflow occurs, -(OPJ_UINT32)(sum >> 32) == 0, overall result is low part of sum.

Matthieu

@mayeut mayeut closed this as completed Jun 20, 2015
@mayeut mayeut added the invalid label Sep 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants