vec_addADD TO COMPARE ADDED TO COMPARE
IBM Power 9 64-bit (64 bits)/ VSX
View official documentation
Location:
Arithmetic
>
Vector Add
Purpose:
Adds the corresponding elements of two vectors.
Result:
The value of each element of output is the sum of the corresponding elements of a and b. Modular arithmetic is used for both signed and unsigned integers.
Prototypes
Assembly Instruction:
vadduwm
Usage:
vector signed int output =
vec_add(
vector signed int a, vector signed int b
)
Example:
#include <altivec.h>
#include <stdio.h>
int main() {
vector signed int a = {
1, 2, 3, 4
};
vector signed int b = {
5, 6, 7, 8
};
vector signed int result = vec_add(a, b);
int* res = (int*)&result;
for (int i = 0; i < 4; ++i) {
printf("%d ", res[i]);
}
printf("\n");
return 0;
}
Assembly Instruction:
vadduhm
Usage:
vector signed short output =
vec_add(
vector signed short a, vector signed short b
)
Example:
#include <altivec.h>
#include <stdio.h>
int main() {
vector signed short a = {
1, 2, 3, 4, 5, 6, 7, 8
};
vector signed short b = {
8, 7, 6, 5, 4, 3, 2, 1
};
vector signed short result = vec_add(a, b);
short res[8];
vec_st(result, 0, res);
for (int i = 0; i < 8; ++i) {
printf("%d ", res[i]);
}
printf("\n");
return 0;
}
Assembly Instruction:
vaddudm
Usage:
vector signed long long output =
vec_add(
vector signed long long a, vector signed long long b
)
Example:
#include <altivec.h>
#include <stdio.h>
int main() {
vector signed long long a = {
1, 2, 3, 4
};
vector signed long long b = {
5, 6, 7, 8
};
vector signed long long result = vec_add(a, b);
long long* res = (long long*)&result;
printf("%lld %lld %lld %lld\n", res[0], res[1], res[2], res[3]);
return 0;
}
Assembly Instruction:
vadduhm
Usage:
vector unsigned short output =
vec_add(
vector unsigned short a, vector unsigned short b
)
Example:
#include <altivec.h>
#include <stdio.h>
int main() {
vector unsigned short a = {
1, 2, 3, 4, 5, 6, 7, 8
};
vector unsigned short b = {
8, 7, 6, 5, 4, 3, 2, 1
};
vector unsigned short result = vec_add(a, b);
unsigned short* res = (unsigned short*)&result;
for (int i = 0; i < 8; ++i) {
printf("%u ", res[i]);
}
printf("\n");
return 0;
}
Assembly Instruction:
vadduwm
Usage:
vector unsigned int output =
vec_add(
vector unsigned int a, vector unsigned int b
)
Example:
#include <altivec.h>
#include <stdio.h>
int main() {
vector unsigned int a = {
1, 2, 3, 4
};
vector unsigned int b = {
5, 6, 7, 8
};
vector unsigned int result = vec_add(a, b);
unsigned int* res = (unsigned int*)&result;
for (int i = 0; i < 4; ++i) {
printf("%u ", res[i]);
}
printf("\n");
return 0;
}
Assembly Instruction:
vaddubm
Usage:
vector signed char output =
vec_add(
vector signed char a, vector signed char b
)
Example:
#include <altivec.h>
#include <stdio.h>
int main() {
vector signed char a = {
1, 2, 3, 4, 5, 6, 7, 8
};
vector signed char b = {
8, 7, 6, 5, 4, 3, 2, 1
};
vector signed char result = vec_add(a, b);
signed char res[8];
vec_st(result, 0, res);
for (int i = 0; i < 8; i++) {
printf("%d ", res[i]);
}
printf("\n");
return 0;
}
Assembly Instruction:
vaddudm
Usage:
vector unsigned long long output =
vec_add(
vector unsigned long long a, vector unsigned long long b
)
Example:
#include <altivec.h>
#include <stdio.h>
int main() {
vector unsigned long long a = {
1ULL, 2ULL
};
vector unsigned long long b = {
3ULL, 4ULL
};
vector unsigned long long result = vec_add(a, b);
unsigned long long res[2];
for (int i = 0; i < 2; i++) {
res[i] = result[i];
}
printf("%llu %llu\n", res[0], res[1]);
return 0;
}
Assembly Instruction:
xvaddsp
Usage:
vector float output =
vec_add(
vector float a, vector float b
)
Example:
#include <altivec.h>
#include <stdio.h>
int main() {
vector float a = {
1.0f, 2.0f, 3.0f, 4.0f
};
vector float b = {
4.0f, 3.0f, 2.0f, 1.0f
};
vector float result = vec_add(a, b);
float* res = (float*)&result;
for (int i = 0; i < 4; ++i) {
printf("%f ", res[i]);
}
printf("\n");
return 0;
}
Assembly Instruction:
vaddudm
Usage:
vector unsigned long long output =
vec_add(
vector unsigned long long a, vector unsigned long long b
)
Example:
#include <altivec.h>
#include <stdio.h>
int main() {
vector unsigned long long a = {
1ULL, 2ULL
};
vector unsigned long long b = {
3ULL, 4ULL
};
vector unsigned long long result = vec_add(a, b);
unsigned long long res[2];
for (int i = 0; i < 2; i++) {
res[i] = result[i];
}
printf("%llu %llu\n", res[0], res[1]);
return 0;
}
Assembly Instruction:
vaddudm
Usage:
vector signed long long output =
vec_add(
vector signed long long a, vector signed long long b
)
Example:
#include <altivec.h>
#include <stdio.h>
int main() {
vector signed long long a = {
1, 2, 3, 4
};
vector signed long long b = {
5, 6, 7, 8
};
vector signed long long result = vec_add(a, b);
long long* res = (long long*)&result;
printf("%lld %lld %lld %lld\n", res[0], res[1], res[2], res[3]);
return 0;
}
Assembly Instruction:
xvadddp
Usage:
vector double output =
vec_add(
vector double a, vector double b
)
Example:
#include <altivec.h>
#include <stdio.h>
int main() {
vector double a = {
1.5, 2.5, 3.5, 4.5
};
vector double b = {
4.0, 1.0, 2.0, 5.0
};
vector double result = vec_add(a, b);
double* res = (double*)&result;
for (int i = 0; i < 4; ++i) {
printf("%f ", res[i]);
}
printf("\n");
return 0;
}
Assembly Instruction:
vaddubm
Usage:
vector unsigned char output =
vec_add(
vector unsigned char a, vector unsigned char b
)
Example:
#include <altivec.h>
#include <stdio.h>
int main() {
vector unsigned char a = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
};
vector unsigned char b = {
16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
};
vector unsigned char result = vec_add(a, b);
unsigned char* res = (unsigned char*)&result;
for (int i = 0; i < 16; i++) {
printf("%u ", res[i]);
}
printf("\n");
return 0;
}
Did you find this useful? Let's discuss.
DB statistics
SIMD Engines: | 5 |
C Intrinsics: | 10702 |
NEON: | 4232 |
AVX2: | 462 |
AVX512: | 4955 |
SSE4.2: | 652 |
VSX: | 401 |