Skip to content

Commit 6d0ef7a

Browse files
author
Christoph Oelckers
committed
- added conversion macros to convert floating point angles to angle_t, using xs_Float.h, and replaced all occurences in the code with them (let's hope I found everything.)
Converting a floating point value that is out of range for a signed integer will result in 0x80000000 with SSE math, which is used exclusively for this purpose on modern Visual C++ compilers, so this cannot be used anywhere. On ARM there's problems with float to unsigned int conversions. xs_Float does not depend on these
1 parent 7b42093 commit 6d0ef7a

18 files changed

+58
-46
lines changed

src/c_cmds.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -1236,3 +1236,16 @@ CCMD(secret)
12361236
}
12371237
}
12381238
}
1239+
1240+
CCMD(angleconvtest)
1241+
{
1242+
Printf("Testing degrees to angle conversion:\n");
1243+
for (double ang = -5 * 180.; ang < 5 * 180.; ang += 45.)
1244+
{
1245+
angle_t ang1 = FLOAT2ANGLE(ang);
1246+
angle_t ang2 = (angle_t)(ang * (ANGLE_90 / 90.));
1247+
angle_t ang3 = (angle_t)(int)(ang * (ANGLE_90 / 90.));
1248+
Printf("Angle = %.5f: xs_RoundToInt = %08x, unsigned cast = %08x, signed cast = %08x\n",
1249+
ang, ang1, ang2, ang3);
1250+
}
1251+
}

src/d_netinfo.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ void ReadCompatibleUserInfo(FArchive &arc, userinfo_t &info)
894894

895895
*static_cast<FStringCVar *>(info[NAME_Name]) = netname;
896896
*static_cast<FIntCVar *>(info[NAME_Team]) = team;
897-
*static_cast<FFloatCVar *>(info[NAME_Autoaim]) = (float)aimdist / ANGLE_1;
897+
*static_cast<FFloatCVar *>(info[NAME_Autoaim]) = ANGLE2FLOAT(aimdist);
898898
*static_cast<FIntCVar *>(info[NAME_Skin]) = skin;
899899
*static_cast<FIntCVar *>(info[NAME_Gender]) = gender;
900900
*static_cast<FBoolCVar *>(info[NAME_NeverSwitchOnPickup]) = neverswitch;

src/edata.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ static void parseSector(FScanner &sc)
392392
{
393393
sc.CheckString("=");
394394
sc.MustGetFloat();
395-
sec.planexform[sector_t::floor].angle = angle_t(sc.Float * ANGLE_90 / 90.);
395+
sec.planexform[sector_t::floor].angle = FLOAT2ANGLE(sc.Float);
396396
}
397397
else if (sc.Compare("flooroffsetx"))
398398
{
@@ -416,7 +416,7 @@ static void parseSector(FScanner &sc)
416416
{
417417
sc.CheckString("=");
418418
sc.MustGetFloat();
419-
sec.planexform[sector_t::ceiling].angle = angle_t(sc.Float * ANGLE_90 / 90.);
419+
sec.planexform[sector_t::ceiling].angle = FLOAT2ANGLE(sc.Float);
420420
}
421421
else if (sc.Compare("ceilingoffsetx"))
422422
{

src/fragglescript/t_func.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1473,9 +1473,9 @@ void FParser::SF_SetCamera(void)
14731473
else
14741474
{
14751475
fixed_t pitch = fixedvalue(t_argv[3]);
1476-
if(pitch < -50*FRACUNIT) pitch = -50*FRACUNIT;
1477-
if(pitch > 50*FRACUNIT) pitch = 50*FRACUNIT;
1478-
newcamera->pitch=(angle_t)((pitch/65536.0f)*(ANGLE_45/45.0f)*(20.0f/32.0f));
1476+
if (pitch < -50 * FRACUNIT) pitch = -50 * FRACUNIT;
1477+
if (pitch > 50 * FRACUNIT) pitch = 50 * FRACUNIT;
1478+
newcamera->pitch = xs_CRoundToUInt((pitch / 65536.0f)*(ANGLE_45 / 45.0f)*(20.0f / 32.0f));
14791479
}
14801480
player->camera=newcamera;
14811481
}

src/g_doom/a_doomweaps.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ DEFINE_ACTION_FUNCTION_PARAMS(AActor, A_Saw)
124124
PARAM_CLASS_OPT (pufftype, AActor) { pufftype = NULL; }
125125
PARAM_INT_OPT (flags) { flags = 0; }
126126
PARAM_FIXED_OPT (range) { range = 0; }
127-
PARAM_ANGLE_OPT (spread_xy) { spread_xy = angle_t(2.8125 * (ANGLE_90 / 90.0)); }
127+
PARAM_ANGLE_OPT(spread_xy) { spread_xy = 33554432; /*angle_t(2.8125 * (ANGLE_90 / 90.0));*/ } // The floating point expression does not get optimized away.
128128
PARAM_ANGLE_OPT (spread_z) { spread_z = 0; }
129129
PARAM_FIXED_OPT (lifesteal) { lifesteal = 0; }
130130
PARAM_INT_OPT (lifestealmax) { lifestealmax = 0; }

src/g_shared/a_camera.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void ASecurityCamera::PostBeginPlay ()
8686
pitch = -ANGLE_90 + ANGLE_1;
8787
else if (pitch >= ANGLE_90)
8888
pitch = ANGLE_90 - ANGLE_1;
89-
Range = (angle_t)((float)args[1] * 536870912.f / 45.f);
89+
Range = FLOAT2ANGLE(args[1]);
9090
}
9191

9292
void ASecurityCamera::Tick ()
@@ -136,7 +136,7 @@ void AAimingCamera::PostBeginPlay ()
136136

137137
args[2] = 0;
138138
Super::PostBeginPlay ();
139-
MaxPitchChange = (int)((float)changepitch * 536870912.f / 45.f / (float)TICRATE);
139+
MaxPitchChange = FLOAT2ANGLE(changepitch * TICRATE);
140140
Range /= TICRATE;
141141

142142
TActorIterator<AActor> iterator (args[3]);
@@ -181,7 +181,7 @@ void AAimingCamera::Tick ()
181181
double dz = Z() - tracer->Z() - tracer->height/2;
182182
double dist = vect.Length();
183183
double ang = dist != 0.f ? atan2 (dz, dist) : 0;
184-
int desiredpitch = (angle_t)(ang * 2147483648.f / PI);
184+
int desiredpitch = (int)RAD2ANGLE(ang);
185185
if (abs (desiredpitch - pitch) < MaxPitchChange)
186186
{
187187
pitch = desiredpitch;

src/g_shared/a_movingcamera.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,12 @@ bool APathFollower::Interpolate ()
430430
}
431431
if (args[2] & 4)
432432
{ // adjust pitch; use floats for precision
433-
float fdx = FIXED2FLOAT(dx);
434-
float fdy = FIXED2FLOAT(dy);
435-
float fdz = FIXED2FLOAT(-dz);
436-
float dist = (float)sqrt (fdx*fdx + fdy*fdy);
437-
float ang = dist != 0.f ? (float)atan2 (fdz, dist) : 0;
438-
pitch = (angle_t)(ang * 2147483648.f / PI);
433+
double fdx = FIXED2DBL(dx);
434+
double fdy = FIXED2DBL(dy);
435+
double fdz = FIXED2DBL(-dz);
436+
double dist = sqrt (fdx*fdx + fdy*fdy);
437+
double ang = dist != 0.f ? atan2 (fdz, dist) : 0;
438+
pitch = (fixed_t)RAD2ANGLE(ang);
439439
}
440440
}
441441
else
@@ -449,28 +449,28 @@ bool APathFollower::Interpolate ()
449449
float lerped = Lerp (angle1, angle2 + 4294967296.f);
450450
if (lerped >= 4294967296.f)
451451
{
452-
angle = (angle_t)(lerped - 4294967296.f);
452+
angle = xs_CRoundToUInt(lerped - 4294967296.f);
453453
}
454454
else
455455
{
456-
angle = (angle_t)lerped;
456+
angle = xs_CRoundToUInt(lerped);
457457
}
458458
}
459459
else if (angle2 - angle1 >= 2147483648.f)
460460
{
461461
float lerped = Lerp (angle1, angle2 - 4294967296.f);
462462
if (lerped < 0.f)
463463
{
464-
angle = (angle_t)(lerped + 4294967296.f);
464+
angle = xs_CRoundToUInt(lerped + 4294967296.f);
465465
}
466466
else
467467
{
468-
angle = (angle_t)lerped;
468+
angle = xs_CRoundToUInt(lerped);
469469
}
470470
}
471471
else
472472
{
473-
angle = (angle_t)Lerp (angle1, angle2);
473+
angle = xs_CRoundToUInt(Lerp (angle1, angle2));
474474
}
475475
}
476476
if (args[2] & 1)
@@ -677,7 +677,7 @@ bool AMovingCamera::Interpolate ()
677677
double dz = FIXED2DBL(Z() - tracer->Z() - tracer->height/2);
678678
double dist = sqrt (dx*dx + dy*dy);
679679
double ang = dist != 0.f ? atan2 (dz, dist) : 0;
680-
pitch = (angle_t)(ang * 2147483648.f / PI);
680+
pitch = RAD2ANGLE(ang);
681681
}
682682

683683
return true;

src/m_fixed.h

+8
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,12 @@ inline SDWORD ModDiv (SDWORD num, SDWORD den, SDWORD *dmval)
141141
#define FIXED2FLOAT(f) ((f) / float(65536))
142142
#define FIXED2DBL(f) ((f) / double(65536))
143143

144+
#define ANGLE2DBL(f) ((f) * (90./ANGLE_90))
145+
#define ANGLE2FLOAT(f) (float((f) * (90./ANGLE_90)))
146+
#define FLOAT2ANGLE(f) ((angle_t)xs_CRoundToInt((f) * (ANGLE_90/90.)))
147+
148+
#define ANGLE2RAD(f) ((f) * (M_PI/ANGLE_180))
149+
#define ANGLE2RADF(f) ((f) * float(M_PI/ANGLE_180))
150+
#define RAD2ANGLE(f) ((angle_t)xs_CRoundToInt((f) * (ANGLE_180/M_PI)))
151+
144152
#endif

src/p_enemy.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1859,7 +1859,7 @@ DEFINE_ACTION_FUNCTION_PARAMS(AActor, A_LookEx)
18591859

18601860
AActor *targ = NULL; // Shuts up gcc
18611861
fixed_t dist;
1862-
angle_t fov = (fov_f == 0) ? ANGLE_180 : angle_t(fov_f * ANGLE_90 / 90);
1862+
angle_t fov = (fov_f == 0) ? ANGLE_180 : FLOAT2ANGLE(fov_f);
18631863
FLookExParams params = { fov, minseedist, maxseedist, maxheardist, flags, seestate };
18641864

18651865
if (self->flags5 & MF5_INCONVERSATION)
@@ -2827,7 +2827,7 @@ void A_Face (AActor *self, AActor *other, angle_t max_turn, angle_t max_pitch, a
28272827

28282828
double dist_z = target_z - source_z;
28292829
double ddist = sqrt(dist.X*dist.X + dist.Y*dist.Y + dist_z*dist_z);
2830-
int other_pitch = (int)rad2bam(asin(dist_z / ddist));
2830+
int other_pitch = (int)RAD2ANGLE(asin(dist_z / ddist));
28312831

28322832
if (max_pitch != 0)
28332833
{

src/p_udmf.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ fixed_t UDMFParserBase::CheckFixed(const char *key)
260260

261261
angle_t UDMFParserBase::CheckAngle(const char *key)
262262
{
263-
return angle_t(CheckFloat(key) * ANGLE_90 / 90.);
263+
return FLOAT2ANGLE(CheckFloat(key));
264264
}
265265

266266
bool UDMFParserBase::CheckBool(const char *key)

src/r_data/voxels.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ static void VOX_ReadOptions(FScanner &sc, VoxelOptions &opts)
531531
{
532532
sc.TokenMustBe(TK_FloatConst);
533533
}
534-
opts.AngleOffset = ANGLE_90 + angle_t(sc.Float * ANGLE_180 / 180.0);
534+
opts.AngleOffset = ANGLE_90 + FLOAT2ANGLE(sc.Float);
535535
}
536536
else if (sc.Compare("overridepalette"))
537537
{

src/r_plane.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,7 @@ void R_DrawNormalPlane (visplane_t *pl, fixed_t alpha, bool additive, bool maske
15311531
yscale = pl->yscale << (16 - ds_ybits);
15321532
if (planeang != 0)
15331533
{
1534-
double rad = bam2rad(planeang);
1534+
double rad = ANGLE2RAD(planeang);
15351535
double cosine = cos(rad), sine = sin(rad);
15361536

15371537
pviewx = xs_RoundToInt(pl->xoffs + viewx * cosine - viewy * sine);
@@ -1668,13 +1668,13 @@ void R_DrawTiltedPlane (visplane_t *pl, fixed_t alpha, bool additive, bool maske
16681668
// p is the texture origin in view space
16691669
// Don't add in the offsets at this stage, because doing so can result in
16701670
// errors if the flat is rotated.
1671-
ang = bam2rad(ANG270 - viewangle);
1671+
ang = ANGLE2RAD(ANG270 - viewangle);
16721672
p[0] = vx * cos(ang) - vy * sin(ang);
16731673
p[2] = vx * sin(ang) + vy * cos(ang);
16741674
p[1] = pl->height.ZatPoint(0.0, 0.0) - vz;
16751675

16761676
// m is the v direction vector in view space
1677-
ang = bam2rad(ANG180 - viewangle - pl->angle);
1677+
ang = ANGLE2RAD(ANG180 - viewangle - pl->angle);
16781678
m[0] = yscale * cos(ang);
16791679
m[2] = yscale * sin(ang);
16801680
// m[1] = FIXED2FLOAT(pl->height.ZatPoint (0, iyscale) - pl->height.ZatPoint (0,0));
@@ -1690,7 +1690,7 @@ void R_DrawTiltedPlane (visplane_t *pl, fixed_t alpha, bool additive, bool maske
16901690
// This code keeps the texture coordinates constant across the x,y plane no matter
16911691
// how much you slope the surface. Use the commented-out code above instead to keep
16921692
// the textures a constant size across the surface's plane instead.
1693-
ang = bam2rad(pl->angle);
1693+
ang = ANGLE2RAD(pl->angle);
16941694
m[1] = pl->height.ZatPoint(vx + yscale * sin(ang), vy + yscale * cos(ang)) - zeroheight;
16951695
ang += PI/2;
16961696
n[1] = pl->height.ZatPoint(vx + xscale * sin(ang), vy + xscale * cos(ang)) - zeroheight;

src/r_things.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ void R_ProjectSprite (AActor *thing, int fakeside, F3DFloor *fakefloor, F3DFloor
10121012
if (voxelspin != 0)
10131013
{
10141014
double ang = double(I_FPSTime()) * voxelspin / 1000;
1015-
vis->angle -= angle_t(ang * (4294967296.f / 360));
1015+
vis->angle -= FLOAT2ANGLE(ang);
10161016
}
10171017

10181018
vis->vx = viewx;

src/s_sound.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1953,7 +1953,7 @@ static void S_SetListener(SoundListener &listener, AActor *listenactor)
19531953
{
19541954
if (listenactor != NULL)
19551955
{
1956-
listener.angle = (float)(listenactor->angle) * ((float)PI / 2147483648.f);
1956+
listener.angle = ANGLE2RADF(listenactor->angle);
19571957
/*
19581958
listener.velocity.X = listenactor->velx * (TICRATE/65536.f);
19591959
listener.velocity.Y = listenactor->velz * (TICRATE/65536.f);

src/tables.h

-9
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,4 @@ inline angle_t absangle(angle_t a)
108108
// without additional checking.
109109
extern angle_t tantoangle[SLOPERANGE+1];
110110

111-
inline double bam2rad(angle_t ang)
112-
{
113-
return double(ang >> 1) * (PI / ANGLE_90);
114-
}
115-
inline angle_t rad2bam(double ang)
116-
{
117-
return angle_t(ang * (double(1<<30) / PI)) << 1;
118-
}
119-
120111
#endif // __TABLES_H__

src/zscript/vm.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ void VMDisasm(FILE *out, const VMOP *code, int codesize, const VMScriptFunction
892892
#define PARAM_COLOR_AT(p,x) assert((p) < numparam); assert(param[p].Type == REGT_INT); PalEntry x; x.d = param[p].i;
893893
#define PARAM_FLOAT_AT(p,x) assert((p) < numparam); assert(param[p].Type == REGT_FLOAT); double x = param[p].f;
894894
#define PARAM_FIXED_AT(p,x) assert((p) < numparam); assert(param[p].Type == REGT_FLOAT); fixed_t x = FLOAT2FIXED(param[p].f);
895-
#define PARAM_ANGLE_AT(p,x) assert((p) < numparam); assert(param[p].Type == REGT_FLOAT); angle_t x = angle_t(int(param[p].f * (ANGLE_90 / 90.0)));
895+
#define PARAM_ANGLE_AT(p,x) assert((p) < numparam); assert(param[p].Type == REGT_FLOAT); angle_t x = FLOAT2ANGLE(param[p].f);
896896
#define PARAM_STRING_AT(p,x) assert((p) < numparam); assert(param[p].Type == REGT_STRING); FString x = param[p].s();
897897
#define PARAM_STATE_AT(p,x) assert((p) < numparam); assert(param[p].Type == REGT_POINTER && (param[p].atag == ATAG_STATE || param[p].a == NULL)); FState *x = (FState *)param[p].a;
898898
#define PARAM_POINTER_AT(p,x,type) assert((p) < numparam); assert(param[p].Type == REGT_POINTER); type *x = (type *)param[p].a;
@@ -910,7 +910,7 @@ void VMDisasm(FILE *out, const VMOP *code, int codesize, const VMScriptFunction
910910
#define PARAM_COLOR_OPT_AT(p,x) PalEntry x; if ((p) < numparam && param[p].Type != REGT_NIL) { assert(param[p].Type == REGT_INT); x.d = param[p].i; } else
911911
#define PARAM_FLOAT_OPT_AT(p,x) double x; if ((p) < numparam && param[p].Type != REGT_NIL) { assert(param[p].Type == REGT_FLOAT); x = param[p].f; } else
912912
#define PARAM_FIXED_OPT_AT(p,x) fixed_t x; if ((p) < numparam && param[p].Type != REGT_NIL) { assert(param[p].Type == REGT_FLOAT); x = FLOAT2FIXED(param[p].f); } else
913-
#define PARAM_ANGLE_OPT_AT(p,x) angle_t x; if ((p) < numparam && param[p].Type != REGT_NIL) { assert(param[p].Type == REGT_FLOAT); x = angle_t(int(param[p].f * (ANGLE_90 / 90.0))); } else
913+
#define PARAM_ANGLE_OPT_AT(p,x) angle_t x; if ((p) < numparam && param[p].Type != REGT_NIL) { assert(param[p].Type == REGT_FLOAT); x = FLOAT2ANGLE(param[p].f); } else
914914
#define PARAM_STRING_OPT_AT(p,x) FString x; if ((p) < numparam && param[p].Type != REGT_NIL) { assert(param[p].Type == REGT_STRING); x = param[p].s(); } else
915915
#define PARAM_STATE_OPT_AT(p,x) FState *x; if ((p) < numparam && param[p].Type != REGT_NIL) { assert(param[p].Type == REGT_POINTER && (param[p].atag == ATAG_STATE || param[p].a == NULL)); x = (FState *)param[p].a; } else
916916
#define PARAM_POINTER_OPT_AT(p,x,type) type *x; if ((p) < numparam && param[p].Type != REGT_NIL) { assert(param[p].Type == REGT_POINTER); x = (type *)param[p].a; } else

src/zscript/vmexec.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <math.h>
22
#include "vm.h"
3+
#include "xs_Float.h"
34

45
#define IMPLEMENT_VMEXEC
56

src/zscript/vmexec.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,13 @@ static int Exec(VMFrameStack *stack, const VMOP *pc, VMReturn *ret, int numret)
339339
OP(SANG):
340340
ASSERTA(a); ASSERTF(B); ASSERTKD(C);
341341
GETADDR(PA,KC,X_WRITE_NIL);
342-
*(VM_UWORD *)ptr = (VM_UWORD)(reg.f[B] * ((1<<30) / 180.0)) << 1; // deg -> BAM
342+
*(VM_UWORD *)ptr = (VM_UWORD)(xs_CRoundToInt((reg.f[B]) * (0x40000000/90.))); // deg -> BAM
343343
NEXTOP;
344344
OP(SANG_R):
345345
ASSERTA(a); ASSERTF(B); ASSERTD(C);
346346
GETADDR(PA,RC,X_WRITE_NIL);
347-
*(VM_UWORD *)ptr = (VM_UWORD)(reg.f[B] * ((1<<30) / 180.0)) << 1;
347+
*(VM_UWORD *)ptr = (VM_UWORD)(xs_CRoundToInt((reg.f[B]) * (0x40000000/90.)));
348348
NEXTOP;
349-
350349
OP(SBIT):
351350
ASSERTA(a); ASSERTD(B);
352351
GETADDR(PA,0,X_WRITE_NIL);

0 commit comments

Comments
 (0)