From 7c5f93fac1b4ace4720d0e064a878a69cd7b77dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Sat, 12 Oct 2019 02:02:32 +0200 Subject: [PATCH] Optimize helper functions --- programs/modelViewer.c | 2 +- programs/test.c | 4 ++++ small3dlib.h | 14 ++++++++++---- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/programs/modelViewer.c b/programs/modelViewer.c index 465ea41..7b5b9f9 100644 --- a/programs/modelViewer.c +++ b/programs/modelViewer.c @@ -346,7 +346,7 @@ void setModel(uint8_t index) #undef modelCase - S3L_initTransoform3D(&(scene.models[0].transform)); + S3L_initTransform3D(&(scene.models[0].transform)); S3L_initDrawConfig(&(scene.models[0].config)); if (index == 3) diff --git a/programs/test.c b/programs/test.c index 35fb608..ccd33c0 100644 --- a/programs/test.c +++ b/programs/test.c @@ -6,7 +6,11 @@ */ #include + #define S3L_PIXEL_FUNCTION pixelFunc +#define S3L_RESOLUTION_X 100 +#define S3L_RESOLUTION_Y 100 + #include "../small3dlib.h" #define TEST_BUFFER_W 16 diff --git a/small3dlib.h b/small3dlib.h index 986cf76..5016d80 100644 --- a/small3dlib.h +++ b/small3dlib.h @@ -10,7 +10,7 @@ license: CC0 1.0 (public domain) found at https://creativecommons.org/publicdomain/zero/1.0/ + additional waiver of all IP - version: 0.851 + version: 0.852 Before including the library, define S3L_PIXEL_FUNCTION to the name of the function you'll be using to draw single pixels (this function will be called @@ -568,6 +568,7 @@ static inline S3L_Unit S3L_max(S3L_Unit v1, S3L_Unit v2); static inline S3L_Unit S3L_clamp(S3L_Unit v, S3L_Unit v1, S3L_Unit v2); static inline S3L_Unit S3L_wrap(S3L_Unit value, S3L_Unit mod); static inline S3L_Unit S3L_nonZero(S3L_Unit value); +static inline S3L_Unit S3L_zeroClamp(S3L_Unit value); S3L_Unit S3L_sin(S3L_Unit x); S3L_Unit S3L_asin(S3L_Unit x); @@ -1205,7 +1206,7 @@ void S3L_vec3Xmat4(S3L_Vec4 *v, S3L_Mat4 *m) S3L_Unit S3L_abs(S3L_Unit value) { - return value >= 0 ? value : -1 * value; + return value * (((value >= 0) << 1) - 1); } S3L_Unit S3L_min(S3L_Unit v1, S3L_Unit v2) @@ -1223,6 +1224,11 @@ S3L_Unit S3L_clamp(S3L_Unit v, S3L_Unit v1, S3L_Unit v2) return v >= v1 ? (v <= v2 ? v : v2) : v1; } +S3L_Unit S3L_zeroClamp(S3L_Unit value) +{ + return (value * (value >= 0)); +} + S3L_Unit S3L_wrap(S3L_Unit value, S3L_Unit mod) { return value >= 0 ? (value % mod) : (mod + (value % mod) - 1); @@ -1230,7 +1236,7 @@ S3L_Unit S3L_wrap(S3L_Unit value, S3L_Unit mod) S3L_Unit S3L_nonZero(S3L_Unit value) { - return value != 0 ? value : 1; + return (value + (value == 0)); } S3L_Unit S3L_interpolate(S3L_Unit v1, S3L_Unit v2, S3L_Unit t, S3L_Unit tMax) @@ -2600,7 +2606,7 @@ void S3L_drawScene(S3L_Scene scene) S3L_sortArray[S3L_sortArrayLength].modelIndex = modelIndex; S3L_sortArray[S3L_sortArrayLength].triangleIndex = triangleIndex; S3L_sortArray[S3L_sortArrayLength].sortValue = - S3L_max(0,(transformed0.w + transformed1.w + transformed2.w)) >> 2; + S3L_zeroClamp(transformed0.w + transformed1.w + transformed2.w) >> 2; /* ^ The w component here stores non-clamped z.