AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Extensions (https://forums.alliedmods.net/forumdisplay.php?f=134)
-   -   uint (Operations with unsigned int) (https://forums.alliedmods.net/showthread.php?t=330373)

client21 01-31-2021 13:18

uint (Operations with unsigned int)
 
2 Attachment(s)
uint v1.0.3

As soon as the value exceeds int limit (2147483647), SourcePawn starts to have problems, example:
PHP Code:

int x 2147483650 2// Must be: 1073741825
PrintToServer("%%u = %u\n%%d = %d\n%u > 1000 = %s"xxx1000 "yes" "no"); 

Result: all values are incorrect + comparison does not work:
Code:

%u = 3221225473
%d = -1073741823
3221225473 > 1000 = no

Now you can:
PHP Code:

int x uint(2147483650'/'2);
PrintToServer("%u"x); // 1073741825
PrintToServer("3221225473 > 1000 = %s"uintcmp(32212254731000) > "yes" "no"); // 3221225473 > 1000 = yes 

Before:
PHP Code:

FormatTime(ssizeof(s), "%d.%m.%Y - %H:%M:%S"2147483648);
// plugin crash with error: [SM] Exception reported: Invalid time format or buffer too small 

Now:
PHP Code:

FormatUTime(ssizeof(s), "%d.%m.%Y - %H:%M:%S"2147483648);
PrintToServer(s); // 19.01.2038 - 05:14:08 

FormatTime safe version:

PHP Code:

stock void FormatTime_(char[] bufferint maxlength, const char[] formatint stamp=-1) {
    
FormatTime(buffermaxlengthformatstamp >= -stamp 2147483647); // but int limit = bad


uint.inc
PHP Code:

#define UINT_SIZE 12

/**
 * Math operations with unsigned int ('+', '-', '*', '/', '%')
 * 
 * Example:
 * int i = uint(2147483650, '/', 2);
 * PrintToServer("%u", i); // 1073741825
 * 
 * Note:
 * unsigned int limit (4294967295) is not violated:
 * 4294967295 + 1 = 4294967295
 * 0 - 1 = 0
 * 4294967295 * 2 = 4294967295
 */
native int uint(int aint opint b);

/**
 * Comparison two unsigned int values
 * 
 * Example:
 * bool b = uintcmp(5, 3) >= 0; // true, if 5 >= 3 (instead of operator >=, use any. 0 must be always).
 */
native int uintcmp(int aint b);

/**
 * Replacement FormatTime, because int limit and:
 * 
 * char s[128];
 * FormatTime(s, sizeof(s), "%d.%m.%Y - %H:%M", 2147483648);
 * Plugin crash with error: [SM] Exception reported: Invalid time format or buffer too small
 */
native bool FormatUTime(char[] bufferint maxlength, const char[] formatint stamp=0);

// Convert unsigned int to string (IntToString can't) (return: Number of cells written)
stock int UIntToString(int valuechar[] sint size) {
    return 
FormatEx(ssize"%u"value);
}

// Extract unsigned int from the field (SQL_FetchInt can't)
stock int SQL_FetchUInt(Handle resultsint fieldDBResult &result=DBVal_Errorint error=0)
{
    
char s[UINT_SIZE];
    return 
SQL_FetchString(resultsfieldssizeof(s), result) && result == DBVal_Data StringToInt(s) : error;


Changelog

v1.0.3
Function FormatUTime is faster at 46.72% (cycles for 4294967295 was: 73005, now: 49759)

Bacardi 01-31-2021 13:44

Re: uint (Operations with unsigned int)
 
:bacon: :bacon!:

Want learn more about C++

thx

kossolax 02-02-2021 12:54

Re: uint (Operations with unsigned int)
 
Hello,

Are you comparing this below with FormatTime or vs previous version of your extension?
Quote:

Function FormatUTime is faster at 46.72% (cycles for 4294967295 was: 73005, now: 49759)

Thanks,
Steve.

client21 02-02-2021 21:59

Re: uint (Operations with unsigned int)
 
Quote:

Originally Posted by kossolax (Post 2735465)
Are you comparing this below with FormatTime or vs previous version of your extension?

This is a comparison with the previous version FormatUTime v1.0.2 (uint.zip). Standart function FormatTime is faster, but I don't think that's a problem. I would be glad if someone could suggest a better solution.


All times are GMT -4. The time now is 23:18.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.