From 52d571e189d5ba48dbbc2ad0cf24608f30c6bbc2 Mon Sep 17 00:00:00 2001 From: Sergej Reich Date: Tue, 11 Nov 2014 18:16:20 +0100 Subject: Avoid calling powf with integer exponent in more places Move powX functions from particle code into math library and use them. --- source/blender/blenkernel/intern/brush.c | 2 +- source/blender/blenkernel/intern/cloth.c | 2 +- source/blender/blenkernel/intern/particle_system.c | 32 +++++----------------- source/blender/blenkernel/intern/smoke.c | 2 +- source/blender/blenlib/BLI_math_base.h | 5 ++++ source/blender/blenlib/intern/math_base_inline.c | 18 ++++++++++++ .../blender/editors/interface/interface_handlers.c | 2 +- .../blender/editors/interface/interface_widgets.c | 2 +- source/blender/editors/space_view3d/drawobject.c | 2 +- source/blender/editors/uvedit/uvedit_draw.c | 2 +- source/blender/render/intern/source/rayshade.c | 4 +-- 11 files changed, 39 insertions(+), 34 deletions(-) (limited to 'source/blender') diff --git a/source/blender/blenkernel/intern/brush.c b/source/blender/blenkernel/intern/brush.c index ae6ae6087af..0fdd838f5fc 100644 --- a/source/blender/blenkernel/intern/brush.c +++ b/source/blender/blenkernel/intern/brush.c @@ -1048,7 +1048,7 @@ struct ImBuf *BKE_brush_gen_radial_control_imbuf(Brush *br, bool secondary) for (i = 0; i < side; ++i) { for (j = 0; j < side; ++j) { - float magn = sqrtf(powf(i - half, 2) + powf(j - half, 2)); + float magn = sqrtf(pow2f(i - half) + pow2f(j - half)); im->rect_float[i * side + j] = BKE_brush_curve_strength_clamp(br, magn, half); } } diff --git a/source/blender/blenkernel/intern/cloth.c b/source/blender/blenkernel/intern/cloth.c index aacf02555d4..3932a8ead2f 100644 --- a/source/blender/blenkernel/intern/cloth.c +++ b/source/blender/blenkernel/intern/cloth.c @@ -789,7 +789,7 @@ static void cloth_apply_vgroup ( ClothModifierData *clmd, DerivedMesh *dm ) // Kicking goal factor to simplify things...who uses that anyway? // ABS ( clmd->sim_parms->maxgoal - clmd->sim_parms->mingoal ); - verts->goal = powf(verts->goal, 4.0f); + verts->goal = pow4f(verts->goal); if ( verts->goal >= SOFTGOALSNAP ) verts->flags |= CLOTH_VERT_FLAG_PINNED; } diff --git a/source/blender/blenkernel/intern/particle_system.c b/source/blender/blenkernel/intern/particle_system.c index 155299b69c3..7ad99c14565 100644 --- a/source/blender/blenkernel/intern/particle_system.c +++ b/source/blender/blenkernel/intern/particle_system.c @@ -2764,24 +2764,6 @@ static void sph_force_cb(void *sphdata_v, ParticleKey *state, float *force, floa sphdata->pass++; } -/* powf is really slow for raising to integer powers. */ -MINLINE float pow2(float x) -{ - return x * x; -} -MINLINE float pow3(float x) -{ - return pow2(x) * x; -} -MINLINE float pow4(float x) -{ - return pow2(pow2(x)); -} -MINLINE float pow7(float x) -{ - return pow2(pow3(x)) * x; -} - static void sphclassical_density_accum_cb(void *userdata, int index, float UNUSED(squared_dist)) { SPHRangeData *pfr = (SPHRangeData *)userdata; @@ -2803,7 +2785,7 @@ static void sphclassical_density_accum_cb(void *userdata, int index, float UNUSE /* Smoothing factor. Utilise the Wendland kernel. gnuplot: * q1(x) = (2.0 - x)**4 * ( 1.0 + 2.0 * x) * plot [0:2] q1(x) */ - q = qfac / pow3(pfr->h) * pow4(2.0f - rij_h) * ( 1.0f + 2.0f * rij_h); + q = qfac / pow3f(pfr->h) * pow4f(2.0f - rij_h) * ( 1.0f + 2.0f * rij_h); q *= pfr->npsys->part->mass; if (pfr->use_size) @@ -2857,7 +2839,7 @@ static void sphclassical_force_cb(void *sphdata_v, ParticleKey *state, float *fo float rest_density = fluid->rest_density * (fluid->flag & SPH_FAC_DENSITY ? 4.77f : 1.0f); // Use speed of sound squared - float stiffness = pow2(fluid->stiffness_k); + float stiffness = pow2f(fluid->stiffness_k); ParticleData *npa; float vec[3]; @@ -2878,10 +2860,10 @@ static void sphclassical_force_cb(void *sphdata_v, ParticleKey *state, float *fo pfr.pa = pa; sph_evaluate_func(NULL, psys, state->co, &pfr, interaction_radius, sphclassical_neighbour_accum_cb); - pressure = stiffness * (pow7(pa->sphdensity / rest_density) - 1.0f); + pressure = stiffness * (pow7f(pa->sphdensity / rest_density) - 1.0f); /* multiply by mass so that we return a force, not accel */ - qfac2 *= sphdata->mass / pow3(pfr.h); + qfac2 *= sphdata->mass / pow3f(pfr.h); pfn = pfr.neighbors; for (i = 0; i < pfr.tot_neighbors; i++, pfn++) { @@ -2902,19 +2884,19 @@ static void sphclassical_force_cb(void *sphdata_v, ParticleKey *state, float *fo if (rij_h > 2.0f) continue; - npressure = stiffness * (pow7(npa->sphdensity / rest_density) - 1.0f); + npressure = stiffness * (pow7f(npa->sphdensity / rest_density) - 1.0f); /* First derivative of smoothing factor. Utilise the Wendland kernel. * gnuplot: * q2(x) = 2.0 * (2.0 - x)**4 - 4.0 * (2.0 - x)**3 * (1.0 + 2.0 * x) * plot [0:2] q2(x) * Particles > 2h away are excluded above. */ - dq = qfac2 * (2.0f * pow4(2.0f - rij_h) - 4.0f * pow3(2.0f - rij_h) * (1.0f + 2.0f * rij_h) ); + dq = qfac2 * (2.0f * pow4f(2.0f - rij_h) - 4.0f * pow3f(2.0f - rij_h) * (1.0f + 2.0f * rij_h) ); if (pfn->psys->part->flag & PART_SIZEMASS) dq *= npa->size; - pressureTerm = pressure / pow2(pa->sphdensity) + npressure / pow2(npa->sphdensity); + pressureTerm = pressure / pow2f(pa->sphdensity) + npressure / pow2f(npa->sphdensity); /* Note that 'minus' is removed, because vec = vecBA, not vecAB. * This applies to the viscosity calculation below, too. */ diff --git a/source/blender/blenkernel/intern/smoke.c b/source/blender/blenkernel/intern/smoke.c index 90ac712e55a..1a13fcf99d9 100644 --- a/source/blender/blenkernel/intern/smoke.c +++ b/source/blender/blenkernel/intern/smoke.c @@ -2020,7 +2020,7 @@ BLI_INLINE void apply_inflow_fields(SmokeFlowSettings *sfs, float emission_value if (fuel && fuel[index] > FLT_EPSILON) { /* instead of using 1.0 for all new fuel add slight falloff * to reduce flow blockiness */ - float value = 1.0f - powf(1.0f - emission_value, 2.0f); + float value = 1.0f - pow2f(1.0f - emission_value); if (value > react[index]) { float f = fuel_flow / fuel[index]; diff --git a/source/blender/blenlib/BLI_math_base.h b/source/blender/blenlib/BLI_math_base.h index 07de074e717..5711d09b530 100644 --- a/source/blender/blenlib/BLI_math_base.h +++ b/source/blender/blenlib/BLI_math_base.h @@ -157,6 +157,11 @@ static const int NAN_INT = 0x7FC00000; /******************************* Float ******************************/ +MINLINE float pow2f(float x); +MINLINE float pow3f(float x); +MINLINE float pow4f(float x); +MINLINE float pow7f(float x); + MINLINE float sqrt3f(float f); MINLINE double sqrt3d(double d); diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c index e6217329145..39116d6f30f 100644 --- a/source/blender/blenlib/intern/math_base_inline.c +++ b/source/blender/blenlib/intern/math_base_inline.c @@ -44,6 +44,24 @@ # define UNLIKELY(x) (x) #endif +/* powf is really slow for raising to integer powers. */ +MINLINE float pow2f(float x) +{ + return x * x; +} +MINLINE float pow3f(float x) +{ + return pow2f(x) * x; +} +MINLINE float pow4f(float x) +{ + return pow2f(pow2f(x)); +} +MINLINE float pow7f(float x) +{ + return pow2f(pow3f(x)) * x; +} + MINLINE float sqrt3f(float f) { if (UNLIKELY(f == 0.0f)) return 0.0f; diff --git a/source/blender/editors/interface/interface_handlers.c b/source/blender/editors/interface/interface_handlers.c index 992c72cacf8..bca7b7c43b9 100644 --- a/source/blender/editors/interface/interface_handlers.c +++ b/source/blender/editors/interface/interface_handlers.c @@ -5381,7 +5381,7 @@ static bool ui_numedit_but_HISTOGRAM(uiBut *but, uiHandleButtonData *data, int m float dy = my - data->draglasty; /* scale histogram values (dy / 10 for better control) */ - const float yfac = min_ff(powf(hist->ymax, 2.0f), 1.0f) * 0.5f; + const float yfac = min_ff(pow2f(hist->ymax), 1.0f) * 0.5f; hist->ymax += (dy * 0.1f) * yfac; /* 0.1 allows us to see HDR colors up to 10 */ diff --git a/source/blender/editors/interface/interface_widgets.c b/source/blender/editors/interface/interface_widgets.c index 97f560df7be..b402981dfde 100644 --- a/source/blender/editors/interface/interface_widgets.c +++ b/source/blender/editors/interface/interface_widgets.c @@ -2119,7 +2119,7 @@ void ui_hsvcircle_pos_from_vals(uiBut *but, const rcti *rect, float *hsv, float ang = 2.0f * (float)M_PI * hsv[0] + 0.5f * (float)M_PI; if ((but->flag & UI_BUT_COLOR_CUBIC) && (U.color_picker_type == USER_CP_CIRCLE_HSV)) - radius_t = (1.0f - powf(1.0f - hsv[1], 3.0f)); + radius_t = (1.0f - pow3f(1.0f - hsv[1])); else radius_t = hsv[1]; diff --git a/source/blender/editors/space_view3d/drawobject.c b/source/blender/editors/space_view3d/drawobject.c index f8f7bd1137e..ecedf35dde2 100644 --- a/source/blender/editors/space_view3d/drawobject.c +++ b/source/blender/editors/space_view3d/drawobject.c @@ -1331,7 +1331,7 @@ static void drawlamp(View3D *v3d, RegionView3D *rv3d, Base *base, /* draw the circle/square representing spotbl */ if (la->type == LA_SPOT) { - float spotblcirc = fabsf(z) * (1.0f - powf(la->spotblend, 2)); + float spotblcirc = fabsf(z) * (1.0f - pow2f(la->spotblend)); /* hide line if it is zero size or overlaps with outer border, * previously it adjusted to always to show it but that seems * confusing because it doesn't show the actual blend size */ diff --git a/source/blender/editors/uvedit/uvedit_draw.c b/source/blender/editors/uvedit/uvedit_draw.c index a6e83ce0311..525cedb859f 100644 --- a/source/blender/editors/uvedit/uvedit_draw.c +++ b/source/blender/editors/uvedit/uvedit_draw.c @@ -325,7 +325,7 @@ static void draw_uvs_stretch(SpaceImage *sima, Scene *scene, BMEditMesh *em, MTe BM_ITER_ELEM_INDEX (l, &liter, efa, BM_LOOPS_OF_FACE, i) { luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset); a = fabsf(uvang[i] - ang[i]) / (float)M_PI; - weight_to_rgb(col, 1.0f - powf((1.0f - a), 2.0f)); + weight_to_rgb(col, 1.0f - pow2f(1.0f - a)); glColor3fv(col); glVertex2fv(luv->uv); } diff --git a/source/blender/render/intern/source/rayshade.c b/source/blender/render/intern/source/rayshade.c index ff1f502cc1a..54fb3b8d8a3 100644 --- a/source/blender/render/intern/source/rayshade.c +++ b/source/blender/render/intern/source/rayshade.c @@ -1299,7 +1299,7 @@ static void trace_refract(float col[4], ShadeInput *shi, ShadeResult *shr) float v_refract[3], v_refract_new[3]; float sampcol[4], colsq[4]; - float blur = powf(1.0f - shi->mat->gloss_tra, 3); + float blur = pow3f(1.0f - shi->mat->gloss_tra); short max_samples = shi->mat->samp_gloss_tra; float adapt_thresh = shi->mat->adapt_thresh_tra; @@ -1400,7 +1400,7 @@ static void trace_reflect(float col[3], ShadeInput *shi, ShadeResult *shr, float float v_nor_new[3], v_reflect[3]; float sampcol[4], colsq[4]; - float blur = powf(1.0f - shi->mat->gloss_mir, 3); + float blur = pow3f(1.0f - shi->mat->gloss_mir); short max_samples = shi->mat->samp_gloss_mir; float adapt_thresh = shi->mat->adapt_thresh_mir; float aniso = 1.0f - shi->mat->aniso_gloss_mir; -- cgit v1.2.3