Raised This Month: $ Target: $400
 0% 

Solved What is the difference between 'ByteCountToCells' and 'char'?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Leech_v2
Senior Member
Join Date: Mar 2011
Location: Chinese GuangDong
Old 11-01-2023 , 23:36   What is the difference between 'ByteCountToCells' and 'char'?
Reply With Quote #1

What is the difference between the 'ByteCountToCells' function and the 'char' reserved word?

Will there be different calculation results on different platforms?

PHP Code:
new gNormalArray[32 char];
new 
gDynamicArray;
public 
plugin_init()
{
    
gDynamicArray = new ArrayCreate(ByteCountToCells(32));
    
//gDynamicArray = new ArrayCreate(32 char);

cellarray.inc:
PHP Code:
/**
 * Returns the number of cells required to fit a string of the specified size
 * (including the null terminator).
 *
 * @param size          Number of bytes.
 *
 * @return              Minimum number of cells required to fit the byte count.
 */
stock ByteCountToCells(size)
{
    if (!
size)
    {
        return 
1;
    }

    return (
size 3) / 4;


Last edited by Leech_v2; 11-03-2023 at 03:33.
Leech_v2 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-02-2023 , 00:19   Re: What is the difference between 'ByteCountToCells' and' char '?
Reply With Quote #2

A char is 1 byte. A cell is 4 bytes. So, you can get 4 chars in a cell (which is evident by the calculation in ByteCountToCells which calculates the minimum number of cells required to fit all your bytes). With today's PCs, I don't see any reason to even try to do these optimizations. Memory is so abundant that it's not worth adding the complexity just to save a little memory.

Everything in AMX Mod X is the same between operating systems with respect to coding syntax.
__________________

Last edited by fysiks; 11-02-2023 at 00:25.
fysiks is online now
Leech_v2
Senior Member
Join Date: Mar 2011
Location: Chinese GuangDong
Old 11-02-2023 , 00:48   Re: What is the difference between 'ByteCountToCells' and' char '?
Reply With Quote #3

Quote:
Originally Posted by fysiks View Post
Everything in AMX Mod X is the same between operating systems with respect to coding syntax.
Is the sizeof (cell) on different platforms equal to the same value?
PHP Code:
case tCHAR:               /* char (compute required # of cells */
        
if (lval->ident==iCONSTEXPR) {
          
lval->constval *= /*8*/sCHARBITS/8;  /* from char to bytes */
          
lval->constval = (lval->constval sizeof(cell)-1) / sizeof(cell);
        } else {
          if (
lvalue)
            
rvalue(lval);       /* fetch value if not already in PRI */
          
char2addr();          /* from characters to bytes */
          
addconst(sizeof(cell)-1);     /* make sure the value is rounded up */
          
addr2cell();          /* truncate to number of cells */
        
/* if */
        
return FALSE
Additionally, the return value obtained by using char on an array also seems to be an array. Is this an incorrect usage?
Leech_v2 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-03-2023 , 00:09   Re: What is the difference between 'ByteCountToCells' and' char '?
Reply With Quote #4

Quote:
Originally Posted by Leech_v2 View Post
Is the sizeof (cell) on different platforms equal to the same value?
sizeof returns the size of the variable in number of cells. For a scalar, this will always be 1. You can consult the Pawn Language Guide to learn more about what the language does and doesn't provide.

Quote:
Originally Posted by Leech_v2 View Post
PHP Code:
case tCHAR:               /* char (compute required # of cells */
        
if (lval->ident==iCONSTEXPR) {
          
lval->constval *= /*8*/sCHARBITS/8;  /* from char to bytes */
          
lval->constval = (lval->constval sizeof(cell)-1) / sizeof(cell);
        } else {
          if (
lvalue)
            
rvalue(lval);       /* fetch value if not already in PRI */
          
char2addr();          /* from characters to bytes */
          
addconst(sizeof(cell)-1);     /* make sure the value is rounded up */
          
addr2cell();          /* truncate to number of cells */
        
/* if */
        
return FALSE
Additionally, the return value obtained by using char on an array also seems to be an array. Is this an incorrect usage?
There is no function or data type called "char" in the Pawn language so I'm not sure what mean by "using char". I see you're looking at code in the compiler, that's not something with with I am familiar and I suspect shouldn't really be related to how you're writing your AMX Mod X code (given that you're wiring in Pawn, not C).
__________________

Last edited by fysiks; 11-03-2023 at 00:10.
fysiks is online now
Leech_v2
Senior Member
Join Date: Mar 2011
Location: Chinese GuangDong
Old 11-03-2023 , 00:47   Re: What is the difference between 'ByteCountToCells' and' char '?
Reply With Quote #5

Quote:
Originally Posted by fysiks View Post
sizeof returns the size of the variable in number of cells. For a scalar, this will always be 1. You can consult the Pawn Language Guide to learn more about what the language does and doesn't provide.



There is no function or data type called "char" in the Pawn language so I'm not sure what mean by "using char". I see you're looking at code in the compiler, that's not something with with I am familiar and I suspect shouldn't really be related to how you're writing your AMX Mod X code (given that you're wiring in Pawn, not C).
'char' is a keyword in amxmodx, Currently, it is only known that using char for lvalues can obtain the number of cells required for compressed strings.
In the source code of char, The calculation process of char uses sizeof (cell). In my impression, In C language, using sizeof for value type keywords may not necessarily result in 1
Leech_v2 is offline
Leech_v2
Senior Member
Join Date: Mar 2011
Location: Chinese GuangDong
Old 11-03-2023 , 00:58   Re: What is the difference between 'ByteCountToCells' and' char '?
Reply With Quote #6

Quote:
Originally Posted by fysiks View Post
sizeof returns the size of the variable in number of cells. For a scalar, this will always be 1. You can consult the Pawn Language Guide to learn more about what the language does and doesn't provide.



There is no function or data type called "char" in the Pawn language so I'm not sure what mean by "using char". I see you're looking at code in the compiler, that's not something with with I am familiar and I suspect shouldn't really be related to how you're writing your AMX Mod X code (given that you're wiring in Pawn, not C).
You can see the code in the compiler source code to prove that cell does not necessarily equal 4 bytes.
PHP Code:
#if !defined PAWN_CELL_SIZE
  #define PAWN_CELL_SIZE 32     /* by default, use 32-bit cells */
#endif
#if PAWN_CELL_SIZE==16
  
typedef uint16_t  ucell;
  
typedef int16_t   cell;
#elif PAWN_CELL_SIZE==32
  
typedef uint32_t  ucell;
  
typedef int32_t   cell;
#elif PAWN_CELL_SIZE==64
  
typedef uint64_t  ucell;
  
typedef int64_t   cell;
#else
  #error Unsupported cell size (PAWN_CELL_SIZE)
#endif 
But I couldn't find the definition of PAWN_CELL_SIZE, so I thought, is it possible for cell to be equal to int64 on different platforms?
Leech_v2 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-03-2023 , 01:29   Re: What is the difference between 'ByteCountToCells' and' char '?
Reply With Quote #7

Well, I guess I can only reference the official documentation about how AMX Mod X was designed. Typically, things outside the intended design, even if some code exists, are prone to have unknown behavior.

However, I am curious now. How do you use this in an AMX Mod X plugin? I tried a couple things and I always get "invalid function or declaration".

According to the AMX Mod X documentation here the size of a cell is defined by the "the processor's integral pointer (4 bytes for 32bit processor, 8 bytes for 64bit processors)" (but AMX Mod X only runs on 32-bit games).
__________________

Last edited by fysiks; 11-03-2023 at 01:31.
fysiks is online now
Leech_v2
Senior Member
Join Date: Mar 2011
Location: Chinese GuangDong
Old 11-03-2023 , 01:37   Re: What is the difference between 'ByteCountToCells' and' char '?
Reply With Quote #8

Quote:
Originally Posted by fysiks View Post
Well, I guess I can only reference the official documentation about how AMX Mod X was designed. Typically, things outside the intended design, even if some code exists, are prone to have unknown behavior.

However, I am curious now. How do you use this in an AMX Mod X plugin? I tried a couple things and I always get "invalid function or declaration".

According to the AMX Mod X documentation here the size of a cell is defined by the "the processor's integral pointer (4 bytes for 32bit processor, 8 bytes for 64bit processors)" (but AMX Mod X only runs on 32-bit games).
like this :
PHP Code:
#include amxmodx
public plugin_init()
{
    
register_plugin("test""1.0.0""Oinling");
    
    new 
byteIdpackedText[8 char] = !"ijklmno";
    
server_print("[AMXX]packedText.Size = %d"sizeof(packedText));
    
server_print("[AMXX]packedText = %c%c%c%c%c%c%c"packedText{0}, packedText{1}, packedText{2}, packedText{3}, packedText{4}, packedText{5}, packedText{6});
    while (
packedText{byteId})
    {
        
packedText{byteId++} -= 1;
    }
    
server_print("[AMXX]packedText.Length = %d"byteId);
    
server_print("[AMXX]packedText = %c%c%c%c%c%c%c"packedText{0}, packedText{1}, packedText{2}, packedText{3}, packedText{4}, packedText{5}, packedText{6});

Should we use char instead of ByteCountToCells function?

Last edited by Leech_v2; 11-03-2023 at 01:42.
Leech_v2 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-03-2023 , 02:03   Re: What is the difference between 'ByteCountToCells' and' char '?
Reply With Quote #9

According to the Pawn Language Guide, you can calculate the characters per cell for packed strings with:

Code:
const CharsPerCell = cellbits / charbits
This returns 4 (as we expect in our situation). So, I guess you'd simply do this when needing to know the "size" of a packed string:

Code:
sizeof(packedText) * CharsPerCell
Which can be similarly used to initialize arrays when you can't use the packed string notation. Also, the way that packed strings are declared, according to the Pawn Language Guide, is to use braces instead of brackets when you declare your array:

Code:
new pstring{5} = "test"
This is why I didn't see the use of "char" anywhere when I was looking for it.

______

Some other things I noticed is that your original example code has the wrong syntax. It would actually be something like this:

PHP Code:
new Array:gDynamicArray

public plugin_init()
{
    
gDynamicArray ArrayCreate(ByteCountToCells(32));

__________________

Last edited by fysiks; 11-03-2023 at 02:27.
fysiks is online now
Leech_v2
Senior Member
Join Date: Mar 2011
Location: Chinese GuangDong
Old 11-03-2023 , 02:26   Re: What is the difference between 'ByteCountToCells' and' char '?
Reply With Quote #10

Quote:
Originally Posted by fysiks View Post
Code:
new pstring{5} = "test"
This code cannot be successfully compiled in the online compiler.
https://amx.icegame.ro/amxx/webcompiler.php
Anyway, since it has been confirmed that 'cell' must be equal to 'int32', I have decided to prioritize using 'char' over the function because it will become a constant expression
Leech_v2 is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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