Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

14
Jun

Clipping and Clamping with out ‘if condition’

Given an example, if the signed integer number is more than 255 then set the number to 255(clipping). On the other hand, if the signed integer is less than 0, then set the number to 0(clamping). This is implemented in C using ‘if condition’, which looks like the below code

int ClipNClamp(int val)
{
if(val < 0) val = 0;
if(val > 255) val = 255;
return val;
}

This operation is very popular in many DSP applications, so it is required to optimize the above functionality. One way to achieve optimization is to replace ‘if condition’ with some arithmetic and logical operations. Optimized code without ‘if condition’ is below:

int ClipNClamp(int val)
{
unsigned char op1,op2;
op1 = 255 + (val < 0) + (val > 0xff);
op2 = 256 - (val > 0xff);
return((val & op1)^op2);
}

We again few clock cycles on implementing the functionality using the later code compared to the former implementation. These few cycles will cost us greatly on calling the above function many(multiple of 1000) times. If any of you have better optimized solutions to implement clipping and clamping within a range of 0 to 255, please let me know.Thanks!!!

Popularity: 6%

22
May

What is a reentrant function?

A reentrant function is one that can be used by more than one task concurrently without fear of data corruption.Whereas, a non-reentrant function is one that cannot be shared by more than one task unless mutual exclusion to the function is ensured either by using a semaphore or by disabling interrupts during critical sections of code.A reentrant function can be interrupted at any time and resumed at a later time without loss of data. Reentrant functions either use local variables or protect their data when global variables are used.

In the early days of programming, non-reentrancy was not a threat to programmers; functions did not have concurrent access and there were no interrupts. In many older implementations of the C language, functions were expected to work in an environment of single-threaded processes.However, now concurrent programming is common practice and one should be aware of the pitfalls.It will be very difficult to figure out the bug caused when a signal handling functions triggers a non-reentrant function.

Typically a reentrant function does not hold static data over successive calls, does not return a pointer to static data,should not call any non-reentrant functions, uses local data or protection of global data by taking a local copy of the variable.

Typically a non-reentrant function calls a malloc or free, uses the static data structures, will be a part of input-output operations.

Popularity: 16%

22
Feb

Can we use printf() in an ISR?

No we cannot use printf() in ISR because ISRs’ are a part of kernel and kernel is not linked with libaray provided by C,so when we use printf() in ISR it should generate a linkage error.

Popularity: 5%

14
Feb

How the keyboard works?

Keyboard input follows an event path beginning with the keyboard controller chip and ending with characters being placed in a 30-byte array called the keyboard typeahead buffer(predefined system data i.e. 0×001E to 0×003D in BIOS Data Area). Up to 15 keystrokes can be held there at any given moment, because each keystroke generates 2 bytes - one is ASCII code and other byte is Scan code(all extended keys will have scan codes only,but no ASCII codes). The following events occur when the user presses a key:

  • The keyboard controller chip sends an 8-bit numeric scan code (sc) to the PC’s keyboard input port.
  • The input port is designed so that it triggers an interrupt, which is a predefined signal to the CPU that an input-output needs attention. The CPU responds by executing the INT 9h service routine.
  • The INT 9h service routine retrieves the keyboard Scan code (sc) from the input port and looks up the corresponding ASCII code (ac), if any. It inserts both the scan code and ASCII code into the keyboard typeahead buffer.If the Scan code has no matching ASCII code, then the ASCII code in the typeahead buffer is set to 0.
  • Once the Scan code and ASCII code are kept safely in the typeahead buffer, they stay until the current running program retrieves them. Current program retrieves the typeahead buffer either by INT 16h or INT 21h service routine.

 

 

Popularity: 6%

16
Jan

Frame Pointer Vs Stack Pointer

Stack look like the below figure, when we are starting in main() and no function is called from
the main() in a C program:

 

 

 



 

 

Suppose, inside of body of main() there’s a call to foo(), which takes two arguments. One way to pass the arguments to foo() is through the stack. When the function foo() is called, the function foo() takes some space in stack for its local variables, hence the stack looks like as below:

 

 

 



 

In the above figure, we notice SP and FP. SP is the stack pointer and FP is the frame pointer. The frame pointer points to the location where the stack pointer was, just before foo() moved the stack pointer for foo()’s own local variables.

Having a frame pointer is convenient when a function is likely to move the stack pointer several times throughout the course of running the function. The idea is to keep the frame pointer fixed for the duration of foo()’s stack frame. The stack pointer, in the meanwhile, can change values.

And, once it’s time to exit foo(), you just have to set the stack pointer to where the frame pointer is, which effectively pops off foo()’s stack frame. It’s quite handy to have a frame pointer.

So when we exit foo() the stack looks just as it did before we pushed on foo()’s stack frame, except this time the return value has been filled in.

 

 

Popularity: 12%

01
Nov

Find Max and Min of two integers without branching

FindMinMax(int x,int y)
{
?  int min;
?  int max;
?  /* To find Minimum of x and y */
?  min = y + ((x - y) & -(x < y));
?  /* To find Maximum of x and y */
?  max = x - ((x - y) & -(x < y));
}

Popularity: 3%

01
Nov

Swap Bits in a Byte without using any extra variable

SwapBitsInByte(unsigned char n)
{
  n = (n & 0xF0) >> 4 | (n & 0x0F) < < 4;
  n = (n & 0xCC) >> 2 | (n & 0×33) < < 2;
  n = (n & 0xAA) >> 1 | (n & 0×55) << 1;
  return n;
}

download this code

Popularity: 13%

31
Oct

Interrupt latency and its causes

Interrupt latency is the total delay experienced by a device from the time it raises an interrupt to the time its interrupt service routine begins to execute.The delay introduced can be any one or all of the following below reasons:

  • Time the processor takes to complete the current instruction,do the necessary chores(e.g flush the instruction pipeline and read the interrupt vector) and jump to the trap handler and interrurpt dispatcher part of the kernal
  • Time kernal takes to disable the external interrupts
  • Time required to complete the immediate ISR of higher priority interrupts if any
  • Time the kernal takes to save the context of the interrupted thread,identify the interrupting device, and get the starting address of ISR

The sum of all the above factors is called Interrupt latency.

Popularity: 4%