-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Mandelbrot SIMD test to the benchmark tests
- Loading branch information
1 parent
708e231
commit d3f02b5
Showing
3 changed files
with
131 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,64 @@ | ||
/* | ||
* Copyright (c) 2023-present Samsung Electronics Co., Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdbool.h> | ||
#include <math.h> | ||
#include <stdint.h> | ||
|
||
#define LOOP 600 | ||
#define X_MIN -1.5 | ||
#define X_MAX 0.5 | ||
#define Y_MIN -1.0 | ||
#define Y_MAX 1.0 | ||
#define X_RES 512 | ||
#define WIDTH 1600 | ||
#define HIGHT 1400 | ||
#define N 20 | ||
#define REAL_AXIS_SHIFT -1.8 // ~ horizontal shift | ||
#define IMAGINARY_AXIS_SHIFT -1.0 // ~ vertical shift | ||
#define ZOOM 0.0015 | ||
|
||
uint32_t runtime() { | ||
const int yRes = (X_RES * (Y_MAX - Y_MIN)) / (X_MAX - X_MIN); | ||
#define getNthBit(b, n) ((b & (1 << (7 - n))) > 0) | ||
|
||
double dx = (X_MAX - X_MIN) / X_RES; | ||
double dy = (Y_MAX - Y_MIN) / yRes; | ||
double x, y; | ||
int k; | ||
#define clearNthBit(b, n) b = b & (0xFF - (1 << (7 - n))) | ||
|
||
uint16_t retValLower = 0; | ||
uint16_t retValHigher = 0; | ||
#define setNthBit(b, n) b = b | (1 << (7 - n)) | ||
|
||
for (int j = 0; j < yRes; j++) { | ||
y = Y_MAX - j * dy; | ||
#define ABS_COMPLEX(z_real, z_complex) (sqrtf(z_real * z_real + z_complex * z_complex)) | ||
|
||
for (int i = 0; i < X_RES; i++) { | ||
double u = 0; | ||
double v = 0; | ||
double u2 = 0; | ||
double v2 = 0; | ||
typedef uint8_t byte; | ||
|
||
x = X_MIN + i * dx; | ||
byte isInMandelbrotSet(float c_real, float c_imaginary) | ||
{ | ||
byte result = 0b10000000; | ||
float z_real = 0; | ||
float z_imaginary = 0; | ||
for (size_t k = 0; k < N; k++) { | ||
float complex_abs = ABS_COMPLEX(z_real, z_imaginary); | ||
if (getNthBit(result, 0) == 1 && complex_abs > 2) { | ||
clearNthBit(result, 0); | ||
} else { | ||
float next_z_real = (z_real * z_real - z_imaginary * z_imaginary) + c_real; | ||
float next_z_imaginary = ((float)2.0 * z_real * z_imaginary) + c_imaginary; | ||
z_real = next_z_real; | ||
z_imaginary = next_z_imaginary; | ||
} | ||
|
||
for (k = 1; k < LOOP && (u2 + v2 < 4.0); k++) { | ||
v = 2 * u * v + y; | ||
u = u2 - v2 + x; | ||
u2 = u * u; | ||
v2 = v * v; | ||
} | ||
if (result == 0) { | ||
break; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
if (k >= LOOP) { | ||
retValLower++; | ||
} else { | ||
retValHigher++; | ||
uint32_t runtime() { | ||
uint32_t setSize = 0; | ||
for (int i = 0; i < HIGHT; i++) { | ||
for (int j = 0; j < WIDTH; j++) { | ||
float real = ((float)j * (float)ZOOM) + (float)REAL_AXIS_SHIFT; | ||
float imaginary = ((float)i * (float)ZOOM) + (float)IMAGINARY_AXIS_SHIFT; | ||
if (getNthBit(isInMandelbrotSet(real, imaginary),0)) { | ||
setSize++; | ||
} | ||
} | ||
} | ||
|
||
return retValLower + (retValHigher << 16); | ||
return setSize; | ||
} | ||
|
||
|
||
int main() { | ||
printf("%d\n", runtime()); | ||
printf("%u\n", runtime()); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include <stdio.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
|
||
#include <wasm_simd128.h> | ||
|
||
#define WIDTH 1600 | ||
#define HIGHT 1400 | ||
#define N 20 | ||
#define REAL_AXIS_SHIFT -1.8 // ~ horizontal shift | ||
#define IMAGINARY_AXIS_SHIFT -1.0 // ~ vertical shift | ||
#define ZOOM 0.0015 | ||
|
||
#define getNthBit(b, n) ((b & (1 << (7 - n))) > 0) | ||
|
||
#define clearNthBit(b, n) b = b & (0xFF - (1 << (7 - n))) | ||
|
||
#define setNthBit(b, n) b = b | (1 << (7 - n)) | ||
|
||
#define SQUARE(z) wasm_f32x4_mul(z, z) | ||
|
||
#define ABS_COMPLEX(z_real, z_complex) wasm_f32x4_sqrt(wasm_f32x4_add(SQUARE(z_real), SQUARE(z_imaginary))) | ||
|
||
typedef uint8_t byte; | ||
|
||
byte areInMandelbrotSet(v128_t c_real, v128_t c_imaginary) | ||
{ | ||
byte result = 0b11110000; | ||
v128_t z_real = wasm_f32x4_const_splat(0); | ||
v128_t z_imaginary = wasm_f32x4_const_splat(0); | ||
for (size_t k = 0; k < N; k++) { | ||
v128_t cmp_result = wasm_f32x4_gt(ABS_COMPLEX(z_real, z_imaginary), wasm_f32x4_const_splat(2)); | ||
for (size_t i = 0; i < 4; i++) { | ||
if (getNthBit(result, i) == 1 && ((float*)&cmp_result)[i] != 0) { | ||
clearNthBit(result, i); | ||
} | ||
} | ||
v128_t next_z_real = wasm_f32x4_add(wasm_f32x4_sub(SQUARE(z_real), SQUARE(z_imaginary)), c_real); | ||
v128_t next_z_imaginary = wasm_f32x4_add(wasm_f32x4_mul(wasm_f32x4_mul(z_real, z_imaginary), wasm_f32x4_const_splat(2)), c_imaginary); | ||
z_real = next_z_real; | ||
z_imaginary = next_z_imaginary; | ||
|
||
if (result == 0) { | ||
break; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
uint32_t runtime() { | ||
uint32_t setSize = 0; | ||
for (int i = 0; i < HIGHT; i++) { | ||
for (int j = 0; j < WIDTH; j+=4) { | ||
v128_t real = wasm_f32x4_add(wasm_f32x4_mul(wasm_f32x4_make(j, j+1, j+2, j+3), wasm_f32x4_const_splat(ZOOM)), wasm_f32x4_const_splat(REAL_AXIS_SHIFT)); | ||
v128_t imaginary = wasm_f32x4_add(wasm_f32x4_mul(wasm_f32x4_make(i, i, i, i), wasm_f32x4_const_splat(ZOOM)), wasm_f32x4_const_splat(IMAGINARY_AXIS_SHIFT)); | ||
byte pixels = areInMandelbrotSet(real, imaginary); | ||
for (int i = 0; i < 4; i++) { | ||
if (getNthBit(pixels, i)) { | ||
setSize++; | ||
} | ||
} | ||
} | ||
} | ||
return setSize; | ||
} | ||
|
||
int main() { | ||
printf("%u\n", runtime()); | ||
return 0; | ||
} |