arm neon instruction [closed] - arm

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have some code and, I want to use Neon instruction to change it, but I really don't know how to complete it... Can anyone help me?
void add(int n,float *a,float *b,float t) {
int i, size = (n+2) * (n+2);
for(i = 0; i < size; i++)
a[i] += t * b[i];
}

By using NEON intrinsics.. something like this:
void add(int n,float *a,float *b,float t) {
int i, size = (n+2) * (n+2);
float32x4_t temptt = vdupq_n_f32(t);
for(i = 0; i < size; i+=4) {
float32x4_t temp1 = vld1q_f32(a+i);
float32x4_t temp2 = vld1q_f32(b+i);
temp1 = vmlaq_f32(temp2, tempt, temp1);
vst1q_f32(a + i, temp1);
}
}
This does 4 iterations at once, and would only work when size is a multiple of 4.

Related

Finding the interval between two pointers to a tab in c [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'd like to know how many element I have between two pointers to a tab, with a function.
For example in this code, nb would be 20.
int main(void)
{
int t[50] = {0};
int nb;
nb = finding_number_element(&t[10], &t[30]);
return (0);
}
Have you got a idea?
Try
int finding_number_element(int *a, int *b)
{
return (b - a);
}
if a and b will be pointers to the same array this should work.
If you need something that doesn't care about the data type of t
#define ele_size(x) sizeof(x[0])
int main(void)
{
int t[50] = {0};
int nb;
nb = finding_number_element(ele_size(t),&t[10], &t[30]);
return (0);
}
int finding_number_element(unsigned int ui_ele_size, void *a, void *b)
{
unsigned int ui_pointer1 = (unsigned int)a;
unsigned int ui_pointer2 = (unsigned int)b;
return ((b - a)/ui_ele_size);
}

How to write a replace function in c [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Write a function that will return a value in which i-th byte of x has been replaced by b:
unsigned replace_f (unsigned x, int i, unsigned char b){
}
EX: replace_f(0x12345678, 2, 0xBC) --> 0x12BC5678
unsigned replace_f (unsigned x, int i, unsigned char b){
unsigned char *place = (unsigned char*)&x;
place[sizeof(int)-i] = b;
return x;
}
Assume little endian
Didn't try out, but this could work:
unsigned replace_f (unsigned x, int i, unsigned char b){
char *c;
c = (char *)&x;
c[i] = b;
return x;
}

Input and output in C [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
How to take some unknown number of integers from input and display them on the console?
The number of values will be given through console.
Well the prototype for main is:
main(void) or
main(int argc, char *argv[]);
This are the command line arguments of the program
you can iterate through them with something along this lines
int i = 0;
for (i=0; i < argc; i++) {
printf("%s" argv[i]);
}
Untested but should be enough to get you going.
You can dynamically allocate memory for the numbers using malloc.
int count=0;
scanf("%d",&count);
int* numbers=malloc(sizeof(int)*count);
//take integers from input here
free(numbers);
normally your main method should look something like this
int main(int argc, char** argv)
when you run the you program you will supply a number of arguments to it, say n numbers, so you will have n+1 arguments in total (recall that your program name is argument 0!). therefore you can do something like:
int* array = malloc(sizeof(int) * (argc-1));
int i;
for (i = 0; i < argc-1; i++) {
array[i] = atoi(argv[i+1]);
}
then you can do:
for (i = 0; i < argc-1; i++) {
printf("%d ", array[i]);
}
sorry if there are any syntax errors
hope this helps
Print one-by-one as you go (ignore the first: the number of numbers)
int main(void) {
<READ_NUMBER>; // and promptly ignore it
while (<READ_NUMBER>) {
printf(<PRINT_NUMBER_JUST_READ>);
}
return 0;
}

Swapping first and last digit in an integer using bitwise operator [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have been asked in an interview to swap the last and first digit in an integer using bitwise operators. Tried a lot but I could not find the solution. How can I do this?
Use int digits = log10(x) to get the number of digits.
Use int first = x / pow(10,digits) to get the first digit.
Use int last = x % 10 to get the last digit.
Put it all together and you have
int swapped = x + (last - first) * pow(10,digits) + (first - last)
A trivial solution:
def swap_digit(n):
x = str(n)
if len(x) < 2:
return x
return int(x[-1] + x[1:-1] + x[0])
EDIT: Added a quick and dirty C solution
#include <stdio.h>
#include <string.h>
int main()
{
int n = 123456789;
char buf[100];
int r = snprintf(buf, sizeof(buf), "%d", n);
char t = buf[0];
buf[0] = buf[r-1];
buf[r-1] = t;
int swap;
sscanf(buf, "%d", &swap);
printf("n = %d, swap = %d\n", n, swap);
return 0;
}
def swap(i):
s = list(str(i))
s[0], s[-1] = s[-1], s[0]
i = int(''.join(s))
return i
print swap(123456789) # 923456781

[C]--binary function [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I'm working in a small program using Language C.
I want to create a function which transform a decimal number to binary.
So i Have that:
#include< stdio.h>
int *binaire(int nb,int base)
{
int d,i=0,j;
int *tab= (int*) malloc(sizeof(int));
while(nb>0){
tab[i]=(nb%base);//inverser les bits
nb=nb/base;
i++;
}
return tab;
}
please Help
Thxs
sizeof returns the size in bytes, not in bits. May be you are looking for this:
int *tab= (int*) malloc(sizeof(int) * 8);
EDIT: As pointed in the comments, there are other problems here. Please check them too.
EDIT2: The previous code contains a bug in calculation. I am assuming that you are trying to store the bits in an int array. So the array will contain sizeof(int) * 8 integers, all either zero or one. So the code will be:
int arraySize = sizeof(int) * 8;
int *tab = (int *) malloc(arraySize * sizeof(int));
In fact you don't need to use full int to store a single bit. One byte character is enough to store a bit.
Somethign along those lines:
#include <math.h>
#include <string.h>
#include <stdio.h>
typedef struct value_
{
int* digits;
int nb_digits;
} value;
value* base_convert(int nb,int base) ;
value* make_value(int nb, int base)
{
value* v = malloc(sizeof(value));
if(v)
{
v->nb_digits = 1+round(log(nb)/log(base));
v->digits = malloc(sizeof(int)*v->nb_digits);
if(v->digits) base_convert(v, nb, base);
}
return v;
}
void destroy_value(value* v)
{
if(v->digits) free(v->digits);
free(v);
}
void base_convert(value* v, int nb,int base)
{
int i;
for(i=0;i<v->nb_digits;++i)
{
v->digits[v->nb_digits-i-1]=(nb%base);//inverser les bits
nb=nb/base;
}
return v;
}
int main()
{
int i;
value* v = make_value(137,2);
for(i=0;i<v->nb_digits;++i) printf("%d",v->digits[i]);
destroy_value(v);
return 0;
}
This needs :
a get_digit function to mask the rough access to value::digits
Number of digits is computed using log to be generic. The abse 2 version can be computed in a faster way using bitshifts.

Resources