Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #15 on: October 13, 2012, 08:26:41 pm » |
I tried that too but it didn't work! I also updated to 1.0.1 but still no luck.
Can you please post what you tried, and what you got. Saying I "tried that" and it "didn't work" is very non-specific. My code here: void setup () { Serial.begin (115200); }
void loop () { long address; byte a = 0x0F; byte b = 0x0E; byte c = 0x0C; byte d = 0x0D; byte e = 0x0A; address = long (a) << 16 | long (b) << 12 | c << 8 | d << 4 | e; Serial.println (address, HEX); while (1); }
Works. Output: FECDA
So I can't really believe you tried it and it didn't work.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 20
|
 |
« Reply #16 on: October 13, 2012, 09:07:22 pm » |
I tried that too but it didn't work! I also updated to 1.0.1 but still no luck.
Can you please post what you tried, and what you got. Saying I "tried that" and it "didn't work" is very non-specific. My code here: void setup () { Serial.begin (115200); }
void loop () { long address; byte a = 0x0F; byte b = 0x0E; byte c = 0x0C; byte d = 0x0D; byte e = 0x0A; address = long (a) << 16 | long (b) << 12 | c << 8 | d << 4 | e; Serial.println (address, HEX); while (1); }
Works. Output: FECDA
So I can't really believe you tried it and it didn't work. Ok the long (b) << 12 did the job BUT it doesn't work in the for loop! Any reason why? If i remove the 2 "long" then the address is increased as it should! With the 2 "long" in there, what i get is the initial value and then i get the last value! Not any other values between the two, just min and max values! Weird!!!
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Tesla Member
Karma: 71
Posts: 6596
Arduino rocks
|
 |
« Reply #17 on: October 14, 2012, 05:30:20 am » |
Show us the whole code and output as it is now - there is clearly another issue now. In particular str1, str2, str3 etc - what types/values are they.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #18 on: October 14, 2012, 05:49:32 am » |
Using union or pointers to reconstitute a different data type is actually fairly common. Yes, when the constituent types align to the platform's native word size. Still waiting to see dhenry's code to perform the equivalent of a four bit shift with pointers and/or structs/unions.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Milton Keynes UK
Offline
Tesla Member
Karma: 88
Posts: 6286
-
|
 |
« Reply #19 on: October 14, 2012, 08:24:55 am » |
Still waiting to see dhenry's code to perform the equivalent of a four bit shift with pointers and/or structs/unions.
I have a suspicion that it might be possible to achieve using bitfields, but even assuming you could muddle a way through to make it work the result would be pretty convoluted. Straight forward bit shifts offer a far more appropriate solution in this case.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #20 on: October 14, 2012, 08:41:39 am » |
I have a suspicion... Keep your speculation coming. Think out of the box and keep it simple.
|
|
|
|
|
Logged
|
|
|
|
|
Milton Keynes UK
Offline
Tesla Member
Karma: 88
Posts: 6286
-
|
 |
« Reply #21 on: October 14, 2012, 10:32:46 am » |
I have a suspicion... Keep your speculation coming. Think out of the box and keep it simple. Well, I suppose a union of a long and the appropriate number of bit fields correctly padded and aligned would produce the desired effect. This would still not be my preferred solution, but I think it could be made to work if the compiler cooperates. You would be at the mercy of the compiler, though, and I don't know whether it would actually work in Arduino. Have you tried it and got it working?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #22 on: October 14, 2012, 11:47:18 am » |
it could be made to work if the compiler cooperates. A big if. You probably will have to find a compiler that doesn't automatically pad.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #23 on: October 14, 2012, 11:55:00 am » |
Are we going to see the grand unveiling of this miracle algorithm, or should I stand the marching bands and the fireworks down, and call more dhenry BS?
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
Milton Keynes UK
Offline
Tesla Member
Karma: 88
Posts: 6286
-
|
 |
« Reply #24 on: October 14, 2012, 12:48:55 pm » |
You probably will have to find a compiler that doesn't automatically pad.
I will have to find? I thought this was the solution that you have been enigmatically hinting was possible, while not actually telling us what you thought the solution was.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #25 on: October 14, 2012, 03:16:25 pm » |
Ok the long (b) << 12 did the job BUT it doesn't work in the for loop! Any reason why? If i remove the 2 "long" then the address is increased as it should! With the 2 "long" in there, what i get is the initial value and then i get the last value! Not any other values between the two, just min and max values! Weird!!! Any reason for ignoring my earlier advice? ... Can you please post what you tried, and what you got. Saying I "tried that" and it "didn't work" is very non-specific. And what would that reason be?
|
|
|
|
|
Logged
|
|
|
|
|
North Queensland, Australia
Offline
Edison Member
Karma: 30
Posts: 1167
|
 |
« Reply #26 on: October 14, 2012, 05:28:52 pm » |
I have a suspicion... Keep your speculation coming. Think out of the box and keep it simple. Well, I suppose a union of a long and the appropriate number of bit fields correctly padded and aligned would produce the desired effect. This would still not be my preferred solution, but I think it could be made to work if the compiler cooperates. You would be at the mercy of the compiler, though, and I don't know whether it would actually work in Arduino. Have you tried it and got it working? I tried with bit fields, no luck at getting the code close to nicks example's output above. It is a hardware based solution available to any avr I think the 'MUL' avr instruction is what will do the work in 2 clock cycles. I don't know ASM so could someone check it out. I'll investigate myself but may be some time as I have to read a little first. EDIT again: ( x << n ) == ( x MUL 2 ^n ) 8 << 3 == 8 MUL 8 ^ being power of not C++ XOR
|
|
|
|
« Last Edit: October 14, 2012, 05:51:35 pm by pYro_65 »
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #27 on: October 14, 2012, 05:52:57 pm » |
I think the 'MUL' avr instruction is what will do the work in 2 clock cycles. Wouldn't any reasonably good compiler implement that statement as left shift instead? My approach works on mcus without hardware multiplier, so we can rule that out.
|
|
|
|
|
Logged
|
|
|
|
|
North Queensland, Australia
Offline
Edison Member
Karma: 30
Posts: 1167
|
 |
« Reply #28 on: October 14, 2012, 06:17:41 pm » |
I'm trying to think of your specific case, you stated "It is a hardware based solution available to any avr" there is no barrel shifter and you don't expect hardware multiplication. Just to make sure, you aren't thinking of some obscure method using the SPI shift register or port registers in loop back config are you.
Is it something using asm, ( BTW compiler won't change asm MUL operation, inline asm subverts the optimiser. ) Or just c++?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 114
Posts: 2205
|
 |
« Reply #29 on: October 14, 2012, 07:00:56 pm » |
I was thinking about an approach that fits the following:
1) hardware / software (well, that doesn't exclude much); 2) 2 instructions (actually can be done in 1, depending on how you code it), in C. 3) does not rely on a hardware multiplier;
Whether it is obscure or not is subjective so I would stay away from that.
|
|
|
|
|
Logged
|
|
|
|
|
|