• AVR Freaks

AnsweredHot!Boolean under the hood

Page: 12 > Showing page 1 of 2
Author
acharnley
Super Member
  • Total Posts : 523
  • Reward points : 0
  • Joined: 2016/05/01 06:51:28
  • Location: 0
  • Status: offline
2020/03/27 04:22:20 (permalink)
0

Boolean under the hood

I was thinking before that I don't have an answer to the following and I wonder how it works in terms of either optimisation, or hardware, or both. I have a complete lack of assembler knowledge which may explain the lack of understanding.

uint8_t isEnabled = 1; // 0, 1
uint8_t acFrequency = 100; // 0, 255
 
if (acFrequency) { // should take 8 CPU cycles
 
if (isEnabled) { // should take one CPU cycle not 8, how to ensure it?
 
#1
ric
Super Member
  • Total Posts : 26942
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Boolean under the hood 2020/03/27 04:25:21 (permalink)
+1 (1)
acharnley
if (acFrequency) { // should take 8 CPU cycles

Why?
It will take two, one for the test (assuming no bank selection is required), and one for a GOTO
 

if (isEnabled) { // should take one CPU cycle not 8, how to ensure it?

Why would this be any different to the first example?
 
post edited by ric - 2020/03/27 04:26:45

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
acharnley
Super Member
  • Total Posts : 523
  • Reward points : 0
  • Joined: 2016/05/01 06:51:28
  • Location: 0
  • Status: offline
Re: Boolean under the hood 2020/03/27 05:31:16 (permalink)
0
Doesn't it have to test all the bits?

Whereas for a boolean could optimise it to test only the first bit?

Again it may be down to my lack of assembly knowledge.
#3
du00000001
Just Some Member
  • Total Posts : 3665
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Boolean under the hood 2020/03/27 05:41:04 (permalink)
+1 (1)
acharnley
Doesn't it have to test all the bits?

Whereas for a boolean could optimise it to test only the first bit?



It's optimizing - testing the whole byte for not being zero in both cases.
Thus the test is the same for both.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#4
1and0
Access is Denied
  • Total Posts : 10779
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Boolean under the hood 2020/03/27 05:44:22 (permalink)
+1 (1)
acharnley
Doesn't it have to test all the bits?

Both of your if() statements test an uint8_t variable for zero/non-zero?
 

Whereas for a boolean could optimise it to test only the first bit?

The compiler does not know what exact value in the uint8_t variable; i.e. it does not know you use it to hold only 1 or 0.  Possible values for an uint8_t are 0 to 255.
 

Again it may be down to my lack of assembly knowledge.

Never hurt to know another programming or natural language. ;)
 
post edited by 1and0 - 2020/03/27 05:45:35
#5
1and0
Access is Denied
  • Total Posts : 10779
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Boolean under the hood 2020/03/27 07:10:26 (permalink)
+2 (2)
acharnley
I was thinking before that I don't have an answer to the following and I wonder how it works in terms of either optimisation, or hardware, or both. I have a complete lack of assembler knowledge which may explain the lack of understanding.

uint8_t isEnabled = 1; // 0, 1
uint8_t acFrequency = 100; // 0, 255
 
if (acFrequency) { // should take 8 CPU cycles
 
if (isEnabled) { // should take one CPU cycle not 8, how to ensure it?

As I've said earlier, both your if() statements test an uint8_t variable, and they will take at least this:
        movf    uint8_var
        skpnz
        bra/goto

 
To test a bit, you'll have to tell the compiler to do so, like this:
    if (isEnabled & 1) {

which may generate this:
        btfss   isEnabled,0
        bra/goto

 
Edit:  Alternately, you can declare it a bit variable:
bit isEnabledbit;

or
__bit isEnabledbit;  // for C99

which should use bit-oriented instructions.
post edited by 1and0 - 2020/03/27 07:41:06
#6
NKurzman
A Guy on the Net
  • Total Posts : 18655
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: Boolean under the hood 2020/03/27 08:26:48 (permalink)
0
Does XC8 use bits for bool? 
#7
1and0
Access is Denied
  • Total Posts : 10779
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Boolean under the hood 2020/03/27 08:41:30 (permalink)
+2 (2)
NKurzman
Does XC8 use bits for bool? 

I don't think so.
 
Edit:  Yup, from the XC8 User's Guide:

If you are compiling with the C90 standard, _Bool is not available, but there is a bool
type available if you include <stdbool.h>, but which is merely a typedef for
unsigned char.

post edited by 1and0 - 2020/03/27 08:50:00
#8
andersm
Super Member
  • Total Posts : 2796
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: online
Re: Boolean under the hood 2020/03/27 10:06:54 (permalink)
+1 (1)
It can't. You can take the address of a bool, but you can't of a bit.
#9
NKurzman
A Guy on the Net
  • Total Posts : 18655
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: Boolean under the hood 2020/03/27 10:15:20 (permalink)
0
andersm
It can't. You can take the address of a bool, but you can't of a bit.



I thought there was a standards reason for it.  That would be it.
#10
acharnley
Super Member
  • Total Posts : 523
  • Reward points : 0
  • Joined: 2016/05/01 06:51:28
  • Location: 0
  • Status: offline
Re: Boolean under the hood 2020/03/27 10:58:06 (permalink)
0
It's my lack of asm knowledge then, the skpnz instruction is the key.

Does the CPU execute that in one instruction? How about 'btfss   isEnabled,0'?
#11
NorthGuy
Super Member
  • Total Posts : 6072
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: Boolean under the hood 2020/03/27 11:02:22 (permalink)
+3 (3)
andersm
It can't. You can take the address of a bool, but you can't of a bit.



As long as you don't attempt to take the address of the bool, nothing prevents the compiler from using single bits as storage. Furthermore, the compiler can divide all bools in your program into two categories - the ones which can be stored as bits and the ones that must be stored as bytes. Would be a nice optimization for XC8.
post edited by NorthGuy - 2020/03/27 11:03:46
#12
du00000001
Just Some Member
  • Total Posts : 3665
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Boolean under the hood 2020/03/27 13:02:24 (permalink)
0
NorthGuy
andersm
It can't. You can take the address of a bool, but you can't of a bit.



As long as you don't attempt to take the address of the bool, nothing prevents the compiler from using single bits as storage. Furthermore, the compiler can divide all bools in your program into two categories - the ones which can be stored as bits and the ones that must be stored as bytes. Would be a nice optimization for XC8.



This optimization should be available - provided you store the bools in a struct where they are declared to occupy a single bit.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#13
NKurzman
A Guy on the Net
  • Total Posts : 18655
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re: Boolean under the hood 2020/03/27 13:30:59 (permalink)
0
It would be a nice improvement for the compiler to determine if a bit can be used.
But since the chips have more RAM these days, is it worth the effort any more.
 
#14
crosland
Super Member
  • Total Posts : 1936
  • Reward points : 0
  • Joined: 2005/05/10 10:55:05
  • Location: Warks, UK
  • Status: offline
Re: Boolean under the hood 2020/03/27 13:32:52 (permalink)
+1 (1)
acharnley
It's my lack of asm knowledge then, the skpnz instruction is the key.



You showed C in your OP. I would say it's your lack of C knowledge, i.e., what if() does when given a non-boolean variable to test. As others have stated, C just does a test for zero/non-zero. This should be clear from any C text.
#15
1and0
Access is Denied
  • Total Posts : 10779
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Boolean under the hood 2020/03/27 13:42:49 (permalink)
0
du00000001
This optimization should be available - provided you store the bools in a struct where they are declared to occupy a single bit.

That would not be an optimization. It is rather a single-bit bit-field of type bool.
#16
1and0
Access is Denied
  • Total Posts : 10779
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Boolean under the hood 2020/03/27 13:45:34 (permalink) ☼ Best Answerby acharnley 2020/03/28 02:54:16
+3 (3)
acharnley
It's my lack of asm knowledge then, the skpnz instruction is the key.

The skpnz is a macro for
        btfsc   STATUS,Z

 

Does the CPU execute that in one instruction? How about 'btfss   isEnabled,0'?

Look them up in your PIC datasheet.
 
#17
acharnley
Super Member
  • Total Posts : 523
  • Reward points : 0
  • Joined: 2016/05/01 06:51:28
  • Location: 0
  • Status: offline
Re: Boolean under the hood 2020/03/28 02:51:26 (permalink)
0
crosland
acharnley
It's my lack of asm knowledge then, the skpnz instruction is the key.



You showed C in your OP. I would say it's your lack of C knowledge, i.e., what if() does when given a non-boolean variable to test. As others have stated, C just does a test for zero/non-zero. This should be clear from any C text.




That much is obvious, but I'm specifically asking about how it translates into CPU overhead. The question was whether each bit was tested individually and this has been answered by explaining there's a single CPU instruction which does the lot in one go.
#18
crosland
Super Member
  • Total Posts : 1936
  • Reward points : 0
  • Joined: 2005/05/10 10:55:05
  • Location: Warks, UK
  • Status: offline
Re: Boolean under the hood 2020/03/28 03:10:58 (permalink)
+1 (1)
acharnley
crosland
acharnley
It's my lack of asm knowledge then, the skpnz instruction is the key.



You showed C in your OP. I would say it's your lack of C knowledge, i.e., what if() does when given a non-boolean variable to test. As others have stated, C just does a test for zero/non-zero. This should be clear from any C text.




That much is obvious, but I'm specifically asking about how it translates into CPU overhead. The question was whether each bit was tested individually and this has been answered by explaining there's a single CPU instruction which does the lot in one go.




I think You are still missing the point :) There is no bit testing when using a char, etc., as a proxy for a boolean. It's a simple test for 0 to determine true or false.
#19
acharnley
Super Member
  • Total Posts : 523
  • Reward points : 0
  • Joined: 2016/05/01 06:51:28
  • Location: 0
  • Status: offline
Re: Boolean under the hood 2020/03/28 12:23:05 (permalink)
0
No, I think you're missing the point :)

1and0 has explained that under-the-hood it's using a mask and then checking any bit using
btfsc   STATUS,Z

Due to my lack of assembly knowledge I queried if there was only an instruction which evaluated a specific bit, thus checking an 8 bit value would require 8 CPU cycles, and if I were using using the one bit in the byte I wondered how this could be optimised.


#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2020 APG vNext Commercial Version 4.5