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

normalize and vlen are slightly inconsistent. #244

Open
divVerent opened this issue Jan 19, 2025 · 4 comments
Open

normalize and vlen are slightly inconsistent. #244

divVerent opened this issue Jan 19, 2025 · 4 comments

Comments

@divVerent
Copy link
Contributor

divVerent commented Jan 19, 2025

In FTEQW, looking at the implementations of PF_normalize and PF_vlen, it seems like the following property is true:

normalize(v) == v * (1.0 / vlen(v))

using the MUL_VF and DIV_F instructions.

Non compiler specific test case:

normalize(v) * '1 0 0' == v_x * (1.0 / vlen(v))

In DarkPlaces, this is not entirely true as the intermediate value f, storing vlen(v), is taken the reciprocal of while a double, not while a float (the square root is computed as a double in both engines for both builtins).

I suggest changing https://github.com/DarkPlacesEngine/DarkPlaces/blob/master/prvm_cmds.c#L528 from double f to prvm_vec_t f, so the two functions become consistent with each other, and to document this relationship in a comment. Also, possibly the 1.0 used for computing the reciprocal also needs to be cast to (prvm_vec_t).

@uis246
Copy link
Collaborator

uis246 commented Jan 26, 2025

So normalize has higher precision than vlen. Why is it bad?

@divVerent
Copy link
Contributor Author

divVerent commented Jan 27, 2025 via email

@divVerent
Copy link
Contributor Author

Actually a third reason: the existing code hardcodes the assumption that double >= prvm_vec_t. That's true on all current platforms, but not guaranteed to stay that way.

@uis246
Copy link
Collaborator

uis246 commented Feb 8, 2025

typedef prvm_vec_t double double?

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

2 participants