Help with Arrays

Posts that don't fit in the other forums
Post Reply
DolphinG89
Posts: 28
Joined: Sat Apr 13, 2013 10:40 pm

Help with Arrays

Post by DolphinG89 » Wed May 22, 2013 7:14 pm

Does anyone have a guide. I'm trying to make 2D and 3D arrays. Also how do you declare the array so it will take inputs that are 6bit, 7bit and 8bit(64,128,256,combinations respectively). Thanks in advance.

sverx
Posts: 94
Joined: Wed Aug 12, 2009 2:56 pm
Location: github.com/sverx
Contact:

Re: Help with Arrays

Post by sverx » Sat May 25, 2013 2:41 pm

In C you can make n-dimensions array just using square brackets pairs.
for instance

Code: Select all

int myMatrix[10][30][50];
declares a 3D array (matrix) with 10x30x50 elements, each one of the specified base type - 'int' in the example

DolphinG89
Posts: 28
Joined: Sat Apr 13, 2013 10:40 pm

Re: Help with Arrays

Post by DolphinG89 » Wed Jun 05, 2013 4:26 am

an int is 16 bits so it would be 3 times as slow reading from memory should I use a char instead?

http://en.wikipedia.org/wiki/C_data_types

INTs also get larger "Also, int should be the integer type that the target processor is most efficient working with. This allows great flexibility: for example, all types can be 64-bit."

DolphinG89
Posts: 28
Joined: Sat Apr 13, 2013 10:40 pm

Re: Help with Arrays

Post by DolphinG89 » Wed Jun 05, 2013 4:31 am

I think you would have to cast the char to a u8 to get it to work but am not sure any advice?

DolphinG89
Posts: 28
Joined: Sat Apr 13, 2013 10:40 pm

Re: Help with Arrays

Post by DolphinG89 » Wed Jun 05, 2013 4:46 am

Also how big does a char usually run and how would i go about checking this? If it's 4 bits I guess I could increase the size with booleans and handle those during array address reads.

sverx
Posts: 94
Joined: Wed Aug 12, 2009 2:56 pm
Location: github.com/sverx
Contact:

Re: Help with Arrays

Post by sverx » Wed Jun 05, 2013 1:13 pm

an 'int' on a GBA/DS is 32 bits. You can use 'u8' or 'unsigned char' if you need to store only values from 0 to 255, and of course it will require less memory.
Speed is another matter, as you can't be sure that accessing smaller memory will require less time, since you should consider other details (bus size, memory type, caches and so on...)

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Help with Arrays

Post by elhobbs » Wed Jun 05, 2013 1:33 pm

this can vary by platform, but common sizes are:
char 8 bit
short 16 bit
int 32 bit

you can use sizeof(int) to see the size in bytes (a byte being 8 bits always).

printf("size of unsigned short: %d\n",sizeof(unsigned short));

you have to use base types to create declarations or structs or classes - by that I mean you cannot create a type that is 6 bits wide - it has to based on a char, short, int, etc. you can do bit packing, but underlying it is still an byte, short, int etc.

you probably should not worry about various read times for different data sizes at this point. if performance becomes an issue then look to see where it is slow then fix that area. so get something working first.

in your example 3d array example it probably does not matter what data type you use as you are looking at 15-60k. you could pack all 3 values into a single 32 bit integer. this will increase the complexity of your code as well as the likelihood that you will have bugs. if you need 1 array of this type then do not worry about it. if you need 100,000 of them then you may need to be creative (at least on the ds or gba).

DolphinG89
Posts: 28
Joined: Sat Apr 13, 2013 10:40 pm

Re: Help with Arrays

Post by DolphinG89 » Wed Jun 05, 2013 5:33 pm

How would I store 4 6 bit values into a 24 bit (a char and a short)value so i can later read each 6 bit value into a 1 byte value

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Help with Arrays

Post by elhobbs » Wed Jun 05, 2013 7:43 pm

before you go too far down this path - a better question would be why would you want to do this? just store it in a byte array and be done with it.

a common situation where this type of manipulation occurs is to write to a framebuffer in vram for updating the display. the ds can use a 16 bit display mode. This macro defines how to take 4 values - for alpha, red, green, and blue and pack them into a single value.

from libnds source video.h

Code: Select all

/** \brief Macro to convert 5 bit r g b components plus 1 bit alpha into a single 16 bit ARGB triplet */
#define ARGB16(a, r, g, b) ( ((a) << 15) | (r)|((g)<<5)|((b)<<10))
this example assumes that the r,g,b values do not exceed their capacity of 5 bits each - if they do then then the red bits can overflow into the green bits etc.

I recommend that you read up on bit masks and shift operations. This is also a good example of why you would want to do this - it is required by the hardware. saving a few bytes of memory is not a good reason. reading from from packed data requires locating the data then masking out the correct bits and shift it into the proper alignment. It is not free.

sverx
Posts: 94
Joined: Wed Aug 12, 2009 2:56 pm
Location: github.com/sverx
Contact:

Re: Help with Arrays

Post by sverx » Thu Jun 06, 2013 12:34 pm

DolphinG89 wrote:How would I store 4 6 bit values into a 24 bit (a char and a short)value so i can later read each 6 bit value into a 1 byte value
Storing each 6 bits in a separate unsigned char will be faster and easier, at the cost of some 'wasted' memory. But, as suggested, many times it's better to waste some memory and save on speed and code complexity. I would not do that byte array only if reaching a memory limit.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests