Using the function 'pow()' causes extremely high CPU Load

Titan Two general support. Questions, firmware update, feature request.

Using the function 'pow()' causes extremely high CPU Load

Postby IX_TASTY_XI » Sat Dec 15, 2018 8:30 pm

Problem:
Using the power function [1], causes extremely high CPU Load. I discovered this when I noticed static on my headset while using a particular script that uses this function. I commonly use similar scripts without the use of the power function with no problems and very low CPU Loads.

If somebody can answer why this occurs I would be grateful. I have included more details below.

Code:
Code: Select all
#pragma METAINFO("TEST4", 1, 0, "IX_TASTY_XI")
 
// y = m*x + c, is an equation of a line whose new magnitude (y) is a function of old magnitude (x),
// where (m) equals slope and (c) is a constant. Solved by 2 equations 2 unknowns.
 
main {
 
    // Definitions
 
    fix32 a = get_val(STICK_1_X);
    fix32 b = get_val(STICK_1_Y);
    fix32 x = sqrt(sq(a) + sq(b));                        // original magnitude
    fix32 d = 10.0;                                        // deadzone radius
    fix32 u = 1.0;                                        // exponent power
    fix32 x1 = 1.0;                                        // x1 coordinate, input magnitude
    fix32 y1 = d + x1;                                    // y1 coordinate, output magnitude
    fix32 x2 = 70.0;                                    // x2 coordinate, input magnitude
    fix32 y2 = 47.5;                                    // y2 coordinate, output magnitude
        if (x > 70.0) {
        u = 2.0;
        x1 = 70.0;
        y1 = 47.5;
        x2 = 100.0;
        y2 = 94.0;    }
    fix32 m = (y2 - y1)/(pow(x2,u)-pow(x1,u));            // slope
    fix32 c = y1 - (m * pow(x1,u));                        // constant
    fix32 y = clamp((m * pow(x,u)) + c,-94.0,94.0);     // new magnitude
    fix32 s = y / x;                                    // scaling factor
 
    // Scaling
 
    set_val(STICK_1_X, clamp(a * s,-100.0,100.0));
    set_val(STICK_1_Y, clamp(b * s,-100.0,100.0));
 
    // Cross Deadzone
 
    if (atan(abs(get_val(STICK_2_X)) / abs(get_val(STICK_2_Y))) < deg2rad(10.0) && abs(get_val(STICK_2_Y))>abs(get_val(STICK_2_X))    ) {
        set_val(STICK_2_X, 0.0);
    }
    if (atan(abs(get_val(STICK_2_Y)) / abs(get_val(STICK_2_X))) < deg2rad(10.0) && abs(get_val(STICK_2_X))>abs(get_val(STICK_2_Y))    ) {
        set_val(STICK_2_Y, 0.0);
    }   
}


Current Workaround:
Since I am only currently interested in only raising numbers to the 2nd power, I can use the sq function [2] to accomplish my calculations without the resulting high CPU Load.

References:
[1]https://www.consoletuner.com/wiki/index.php?id=t2:pow
[2]https://www.consoletuner.com/wiki/index.php?id=t2:sq
User avatar
IX_TASTY_XI
Sergeant First Class
Sergeant First Class
 
Posts: 24
Joined: Sun Oct 29, 2017 8:01 pm

Re: Using the function 'pow()' causes extremely high CPU Loa

Postby bonefisher » Sat Dec 15, 2018 8:37 pm

What do you call high cpu? 80% to 100% I've had it pegged at 100% for short period but wouldn't want it for to long!
bonefisher
Lieutenant General
Lieutenant General
 
Posts: 5413
Joined: Thu Jan 29, 2015 10:49 am

Re: Using the function 'pow()' causes extremely high CPU Loa

Postby IX_TASTY_XI » Sun Dec 16, 2018 8:15 pm

bonefisher wrote:What do you call high cpu? 80% to 100% I've had it pegged at 100% for short period but wouldn't want it for to long!


100 - 140%
User avatar
IX_TASTY_XI
Sergeant First Class
Sergeant First Class
 
Posts: 24
Joined: Sun Oct 29, 2017 8:01 pm

Re: Using the function 'pow()' causes extremely high CPU Loa

Postby J2Kbr » Mon Dec 17, 2018 3:22 pm

pow() with decimal numbers are complex, if you are interest on how this works please look power series for exp(x):

http://en.wikipedia.org/wiki/Exponentia ... definition

If used in moderate way the Titan Two can handle the processing with no significant CPU usage. However, your script is making use of this function 4 times on every execution of main, that is why you noticed such high CPU load.

The good new is, you are just making power of two, so you can replace with a simple multiplication:
Code: Select all
x1 * x1

or with a macro which considers the power value (1 or 2):
Code: Select all
#define pow(a, b)       ((b == 2.0) ? (a * a) : a)
ConsoleTuner Support Team
User avatar
J2Kbr
General of the Army
General of the Army
 
Posts: 20323
Joined: Tue Mar 18, 2014 1:39 pm

Re: Using the function 'pow()' causes extremely high CPU Loa

Postby Sillyasskid » Mon Dec 17, 2018 6:01 pm

J2Kbr wrote:The good new is, you are just making power of two, so you can replace with a simple multiplication:
Code: Select all
x1 * x1

or with a macro which considers the power value (1 or 2):
Code: Select all
#define pow(a, b)       ((b == 2.0) ? (a * a) : a)


would the ideal workaround be this?
Code: Select all
#define pow(a, b)       (((fix32)b == 2.0) ? (a * a) : pow(a, b))

This way you can still preserve the use of both POW and POWF instructions. For cases where b is not 2 or 2f.
User avatar
Sillyasskid
Captain
Captain
 
Posts: 574
Joined: Sat May 14, 2016 3:07 am

Re: Using the function 'pow()' causes extremely high CPU Loa

Postby IX_TASTY_XI » Tue Dec 18, 2018 5:02 am

J2Kbr wrote:pow() with decimal numbers are complex, if you are interest on how this works please look power series for exp(x):

http://en.wikipedia.org/wiki/Exponentia ... definition

If used in moderate way the Titan Two can handle the processing with no significant CPU usage. However, your script is making use of this function 4 times on every execution of main, that is why you noticed such high CPU load.

The good new is, you are just making power of two, so you can replace with a simple multiplication:
Code: Select all
x1 * x1

or with a macro which considers the power value (1 or 2):
Code: Select all
#define pow(a, b)       ((b == 2.0) ? (a * a) : a)

Thanks for the reply, I’ll check out the power series. From what I remember computers do everything by summing so I’m assuming that’s how computers handle exponents I’ll look more into it.

I have been meaning to get a basic understanding of computer programming.
User avatar
IX_TASTY_XI
Sergeant First Class
Sergeant First Class
 
Posts: 24
Joined: Sun Oct 29, 2017 8:01 pm

Re: Using the function 'pow()' causes extremely high CPU Loa

Postby J2Kbr » Tue Dec 18, 2018 10:01 am

Sillyasskid wrote:would the ideal workaround be this?
Code: Select all
#define pow(a, b)       (((fix32)b == 2.0) ? (a * a) : pow(a, b))
This way you can still preserve the use of both POW and POWF instructions. For cases where b is not 2 or 2f.

Yes, for a more generic solution your suggestion is better. However, his script does only exponent of 1.0 or 2.0, so the macro I suggested would be more optimized for his usage.
ConsoleTuner Support Team
User avatar
J2Kbr
General of the Army
General of the Army
 
Posts: 20323
Joined: Tue Mar 18, 2014 1:39 pm


Return to Titan Two Device

Who is online

Users browsing this forum: No registered users and 93 guests