| View previous topic :: View next topic   | 
	
	
	
		| Author | 
		Message | 
	
	
		
			georpo
 
 
  Joined: 18 Nov 2008 Posts: 281 Location: Athens, Greece. 
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| (*((int8 *)&x+1)) | 
			 
			
				 Posted: Thu Jun 11, 2020 2:43 am     | 
				     | 
			 
			
				
  | 
			 
			
				Can someone please explain this code?
 
 
It is in 24128.c driver
 
 
 
 	  | Code: | 	 		  
 
#define hi(x)  (*((int8 *)&x+1))
 
 | 	 
  _________________ George. | 
			 
		  | 
	
	
		  | 
	
	
		
			Ttelmah
 
 
  Joined: 11 Mar 2010 Posts: 19967
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jun 11, 2020 2:50 am     | 
				     | 
			 
			
				
  | 
			 
			
				It's potentially a slightly inefficient way of returning the high byte of the 
 
variable 'x'.
 
Inefficient, since you are taking the address of 'x', incrementing this
 
and then taking the byte addressed by this. However the compiler
 
optimiser is quite smart and in fact will just automatically solve all this
 
at compile time and just return the next byte, so it works OK. | 
			 
		  | 
	
	
		  | 
	
	
		
			georpo
 
 
  Joined: 18 Nov 2008 Posts: 281 Location: Athens, Greece. 
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jun 11, 2020 3:38 am     | 
				     | 
			 
			
				
  | 
			 
			
				I understood what is does but I can not even imagine what the syntax says.
 
I guess the make8() function would be better?
 
Or a simple >>=8 & 0xFF would be even better?
 
 
Thanks! _________________ George. | 
			 
		  | 
	
	
		  | 
	
	
		
			Ttelmah
 
 
  Joined: 11 Mar 2010 Posts: 19967
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jun 11, 2020 6:22 am     | 
				     | 
			 
			
				
  | 
			 
			
				make8 yes.
 
The 'simple' >>=8 &0xFF is potentially nearly as inefficient, but again
 
the optimiser is good and tidies this to match the make8.
 
It's inefficient, since it implies accessing the whole 16bit variable and
 
rotating this. On a chip which is '16bit', this would be OK, but on the
 
8bit PIC, this potentially means doing several operations....
 
 
Key is that all these are made 'efficient', when dealing with a variable at 
 
a fixed address, by the optimiser 'knowing' that they just mean to access
 
the next byte, and using the direct single byte access..... | 
			 
		  | 
	
	
		  | 
	
	
		 |