Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added 1 square root optimization to unclassified #6804

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions code/unclassified/src/magic_square/magic_square.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <iostream>
#include <vector>
using namespace std;

// Function to generate odd-order magic square
void generateMagicSquare(int n) {
vector<vector<int>> magicSquare(n, vector<int>(n, 0));

// Starting position for 1
int i = 0, j = n / 2;

// Fill the magic square by placing values
for (int num = 1; num <= n * n; num++) {
magicSquare[i][j] = num;

// Store previous position
int new_i = (i - 1 + n) % n;
int new_j = (j + 1) % n;

// Check if the calculated position is already filled
if (magicSquare[new_i][new_j]) {
i = (i + 1) % n;
} else {
i = new_i;
j = new_j;
}
}

// Output the magic square
cout << "The Magic Square for n = " << n << ":\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << magicSquare[i][j] << "\t";
}
cout << endl;
}

// Display the sum of each row (which is the same for all rows/columns/diagonals)
cout << "Sum of each row or column = " << n * (n * n + 1) / 2 << endl;
}

int main() {
int n;
cout << "Enter the size of the magic square (odd number): ";
cin >> n;

if (n % 2 == 0) {
cout << "Magic square works only for odd numbers!" << endl;
return 1;
}

generateMagicSquare(n);
return 0;
}
22 changes: 22 additions & 0 deletions code/unclassified/src/sqrt_optimizations/sse_sqrt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <xmmintrin.h> // SSE
#include <iostream>

inline void SSESqrt_Recip_Times_X4(float* __restrict pOut, const float* __restrict pIn) {
__m128 in = _mm_loadu_ps(pIn); // Load 4 floats unaligned
_mm_storeu_ps(pOut, _mm_mul_ps(in, _mm_rsqrt_ps(in))); // Store 4 results unaligned
}

int main() {
// Use an array of 4 floats
float in[4] = {8.0f, 16.0f, 32.0f, 64.0f};
float out[4];

SSESqrt_Recip_Times_X4(out, in); // square root method

// Print the output values
for (int i = 0; i < 4; i++) {
std::cout << out[i] << std::endl;
}

return 0;
}