Wednesday, July 8, 2015

C Language : Challenging Algorithms

Bitwise Operators
1. Write a program to determine the ranges of char,short,int and long variables.

2. Write a function setbits(x,p,n,y)  that returns x with n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged.

3. Write a function invert(x,p,n) that return x with the n bits that begin at position p inverted(i.e 1 changed to 0 and vice versa), leaving the others unchanged.

4. Write a function rightrot(x,n) that returns the value of integer x rotated to right by n bit positions.

5. Write a faster bitcount(x) funtion to count the 1 bits in x.

6. How to reverse the bits in an interger?

Others
1. Write a macro to convert a character into lower case from uppercase.

2. Implement a memcpy function.

3. How to declare an array of N pointers to functions returning pointers to functions returning pointers to characters?

4. How do you find the middle of a linked list? Write a C program to return the middle of a linked list.

5. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

6. Reversal of Linklist(with or without recursion)

7. Reverse the list without reversing the links.

8. Find a cycle/loop in a Linklist.

9. How to create an exact copy of a linked list.

10.Write a C program to return the nth node from the end of a linked list.

11.How would you find out if one of the pointers in a linked list is corrupted or not?

12.Write your own C program to implement the atoi() function. 

13.Implement the memmove() function. What is the difference between the memmove() and memcpy () function?

14.Write C code to implement the strstr() (search for a substring) function.

15.Write your own printf() function in C.

16.Write a C program to print a square matrix helically.

17.Write a C program to calculate pow(x,n)?

18.How do you calculate the maximum subarray of a list of numbers?

19.How to generate fibonacci numbers? How to find out if a given number is a fibonacci number or not? Write C programs to do both.

20.What Little-Endian and Big-Endian? How can I determine whether a machine's byte order is bigendian or little endian? How can we convert from one to another?

21.Write a C progam to convert from decimal to any base (binary, hex, oct etc...)

22.Write C code to check if an integer is a power of 2 or not in a single line?

23.Write a C program to find the GCD of two numbers.

24.Write C code to dynamically allocate one, two and three dimensional arrays (using malloc()).

25.How would you find the size of structure without using sizeof()?

26.Write a C program to multiply two matrices.





Discussion on Float Operations in Linux Kernel on ARM

Hello Arun,

On Wed, Jul 8, 2015 at 1:26 PM, Arun KS <getarunks@gmail.com> wrote:
> Hello Priyaranjan,
>
> On Mon, Jul 6, 2015 at 1:13 PM, priyaranjan <priyaranjan45678@gmail.com> wrote:
>> On Wed, Jul 01, 2015 at 01:58:30PM +0530, priyaranjan wrote:
>>> Hi Arun,
>>>
>>>
>>> >On Thu, Jun 25, 2015 at 2:33 PM, Arun KS <getarunks@gmail.com> wrote:
>>> >Hello Mudongliang,
>>> >
>>> >
>>> > >On Tue, Jun 23, 2015 at 9:01 AM, 慕冬亮 <mudongliangabcd@gmail.com> wrote:
>>> > >>
>>> > >> I know there are rarely float-point operations! What's the exception?
>>> > > In the linux kernel, how does it handle the float-point operations in
>>> > the >userland?
>>> >
>>> > >>Most of the userspace programs do not use FP instructions. So by
>>> > >>default floating point engine is turned off during a context switch.
>>> > >>When a process executes floating point instruction, an undefined
>>> > >>exception is generated. Exception handler enables the floating point
>>> > >>engine and jump back to the same instruction which caused the
>>> > >>exception so that it will get re executed with FP engine on.
>>> >
>>> > Is this somehow related to the platform in which linux runs? If the FP
>>> > operations are valid, then will that still generate the exception or it
>>> > does a context switch, turns on FP engine and re-executed?
>
> Usually kernel code never uses FP instructions. In case if kernel uses
> FP, it has to save and restore the FP registers.
>
> FP engine is turned off when a process starts to execute. When a
> process encounters FP instruction for the first time, it results in an
> undefined exception(because FP engine is off). Now the control jumps
> into kernel undefined exception handler. Here kernel check the op-code
> which resulted in undefined exception. Why? Because kernel need to
> verify that it is a co processsor instruction or is it really an
> undefined instruction. In the later case, kernel kills the task. In
> the former case, the kernel enables the corresponding co processor(in
> our case FP engine) and jumps back to user space and re-execute the
> same instruction(this time with co processor turned on). From now on
> all the FP instruction will not result in undef exception. Now when
> kernel decides to move this task away from cpu, it saves the FP
> registers in thread_info of that corresponding task and disables the
> FP engine. The explained scenario is for SMP kernel where same task
> can migrate to different cpus. For single cpu systems, FP register
> saving is done in lazy manner.
>
> This is how ARM handles the FP instructions. I think other
> architectures also does it in similar manner.
>
> Thanks,
> Arun
>
Thanks a lot !

Is this explained anywhere? I guess, I need to dig into kernel docs.


>>> >
>>
>>>Hi Priyaranjan,
>>
>>>Your email got mangled. Please don't send HTML attachments in your >emails to
>>>the Linux Kernel mailing lists.
>>
>>>Try again to reply to Arun.
>>
>>>Thanks,
>>>Luis
>>
>> Sent in plain text format