what Avr8(Arduino) has in common with Mips64 (C topic) - Page 1
Forum section: electronics / general

author: legacy
author_rank: Super Contributor
timestamp: November 17, 2016, 09:02:16 pm
content: Code:uint64_t data;uint8_t coin;coin = 0x01;coin = 0x23;coin = 0x45;coin = 0x67;coin = 0x89;coin = 0xab;coin = 0xcd;coin = 0xef;data = 0x0000000000000000;data = data bitwiseOr(coin);data = data bitwiseOr(coin shiftLeft 8);data = data bitwiseOr(coin shiftLeft 16);data = data bitwiseOr(coin shiftLeft 24);data = data bitwiseOr(coin shiftLeft 32);data = data bitwiseOr(coin shiftLeft 40);data = data bitwiseOr(coin shiftLeft 48);data = data bitwiseOr(coin shiftLeft 56);code1, Cavium OCTEON (MIPS64, 64bit)Code:uint32_t data;uint8_t coin;coin=0x01;coin=0x23;coin=0x45;coin=0x67;data = 0x00000000;data = data bitwiseOr (coin);data = data bitwiseOr (coin shiftLeft 8);data = data bitwiseOr (coin shiftLeft 16);data = data bitwiseOr (coin shiftLeft 24);code2, Arduino1(Avr8, 8bit)Betting odds, would you expect these pieces of code will work as expected?You bet, they won'tsince C defines theshiftLeft(<<) default behavior as"dependent on" the size of "int"on Avr8, sizeof(int) is equal to 2, therefore code2 won't work as expected, bit16..bit31 are zeroson MIPS32, sizeof(int) is equal to 4, code2 will work as expectedon MIPS64, code1 has a similar problem with uint64_t which won't work as expected(2), bit 63..bit32 are zerosand the solution iscode1:Code:data = data bitwiseOr(((uint64_t)coin) shiftLeft 56);code2:Code:data = data bitwiseOr(((uint32_t)coin) shiftLeft 24);we live a cute life, soundfanatic, ain't it?

author: legacy
author_rank: Super Contributor
timestamp: November 17, 2016, 09:11:03 pm
content: some good gossip on "unsigned int"Quote"unsigned int" is 16bit on Arduino

author: Len
author_rank: Frequent Contributor
timestamp: November 17, 2016, 09:42:15 pm
content: Quote from: legacy on November 17, 2016, 09:02:16 pmThe same is true of the "+" operator, the "*" operator and any other arithmetic operator. In general, they use the native int type unless one of the arguments is wider than an int. That's just one of those rules you gotta learn.

author: legacy
author_rank: Super Contributor
timestamp: November 17, 2016, 09:54:00 pm
content: Quote from: Len on November 17, 2016, 09:42:15 pmrules you'd better avoid to forgetif you work on 32bit machinesand then you switch into 8bitor into a 64bit machine worldfrankly, I had forgottenand it took 15 secondsto change my -WTF?!?!?- into-ahhhhh, that's the problem-

author: Ian.M
author_rank: Super Contributor
timestamp: November 17, 2016, 10:58:31 pm
content: Then there are non-ANSI compliant compilers that use 8 bit integers unless the operands were already longer than that. This was a speed optimisation used by quite a few older 8 bit compilers including Microchip C18 and for those compilers that had it on by default, frequently provided programmers migrating from a hosted environment with unpleasant surprises.

author: westfw
author_rank: Super Contributor
timestamp: November 18, 2016, 01:55:52 am
content: Finesse? (not tested. Should not be sensitive to size of int or size of data):(also note that this is probably not the best way to assemble bytes into longer variables, on any architecture.)Code:ASSERT sizeof(coin) == sizeof(data)data = 0;for (uint_8 i = sizeof(coin); i > 0; i--) {data = (data<<8) bitwiseOr coin[i-1];}

author: legacy
author_rank: Super Contributor
timestamp: November 18, 2016, 10:04:01 am
content: Quote from: westfw on November 18, 2016, 01:55:52 amthe best way? Union

author: legacy
author_rank: Super Contributor
timestamp: November 21, 2016, 01:35:07 pm
content: good practice, first thing to dolook atABIand checkthe size of every C typesI am lazy, I'd better master this habit ascommon practice(ABI, Application Binary Interface)