Skip to content

Commit

Permalink
unary to binary converted
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhijitRaut04 committed Nov 14, 2024
1 parent 8f1f317 commit 6ca0a3f
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,27 @@ static double rand_double( void ) {
*/
static double benchmark( void ) {
double elapsed;
double x1;
double x2;
double x1[100];
double x2[100];
double* y;
double t;
int i;

for ( i = 0; i < 100; i++ ) {
x1[ i ] = 2.0 * rand_double();
x2[ i ] = 2.0 * rand_double();
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x1 = ( 20.0*rand_double() ) - 10.0;
x2 = ( 20.0*rand_double() ) - 10.0;
y = stdlib_base_minmax( x1, x2 );
if ( y[0] != y[0] && y[1] != y[1] ) {
y = stdlib_base_minmax( x1[ i % 100 ], x2[ i % 100 ] );
if ( y != y ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( y[0] != y[0] && y[1] != y[1] ) {
if ( y != y ) {
printf( "should not return NaN\n" );
}
return elapsed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@
# limitations under the License.
#/


# VARIABLES #

ifndef VERBOSE
QUIET := @
else
QUIET :=
endif

# Specify the path to Cephes:
CEPHES ?=

# Specify a list of Cephes source files:
CEPHES_SRC ?=

# Determine the OS:
# Determine the OS ([1][1], [2][2]).
#
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
# [2]: http://stackoverflow.com/a/27776822/2225624
Expand All @@ -42,6 +37,10 @@ ifneq (, $(findstring MSYS,$(OS)))
else
ifneq (, $(findstring CYGWIN,$(OS)))
OS := WINNT
else
ifneq (, $(findstring Windows_NT,$(OS)))
OS := WINNT
endif
endif
endif
endif
Expand All @@ -60,7 +59,7 @@ CFLAGS ?= \
-Wall \
-pedantic

# Determine whether to generate [position independent code][1]:
# Determine whether to generate position independent code ([1][1], [2][2]).
#
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
Expand All @@ -70,43 +69,77 @@ else
fPIC ?= -fPIC
endif

# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`):
INCLUDE ?=

# List of source files:
SOURCE_FILES ?=

# List of libraries (e.g., `-lopenblas -lpthread`):
LIBRARIES ?=

# List of library paths (e.g., `-L /foo/bar -L /beep/boop`):
LIBPATH ?=

# List of C targets:
c_targets := benchmark.out


# TARGETS #
# RULES #

# Default target.
#/
# Compiles source files.
#
# This target is the default target.

# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`)
# @param {string} [CFLAGS] - C compiler options
# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`)
# @param {string} [SOURCE_FILES] - list of source files
# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`)
# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`)
#
# @example
# make
#
# @example
# make all
#/
all: $(c_targets)

.PHONY: all


# Compile C source.
#/
# Compiles C source files.
#
# This target compiles C source files.

# @private
# @param {string} CC - C compiler (e.g., `gcc`)
# @param {string} CFLAGS - C compiler options
# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`)
# @param {string} SOURCE_FILES - list of source files
# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`)
# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`)
#/
$(c_targets): %.out: %.c
$(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $(CEPHES_SRC) $< -lm

$(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES)

# Run a benchmark.
#/
# Runs compiled benchmarks.
#
# This target runs a benchmark.

# @example
# make run
#/
run: $(c_targets)
$(QUIET) ./$<

.PHONY: run


# Perform clean-up.
#/
# Removes generated files.
#
# This target removes generated files.

# @example
# make clean
#/
clean:
$(QUIET) -rm -f *.o *.out

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,27 @@ static double rand_double( void ) {
*/
static double benchmark( void ) {
double elapsed;
double x1;
double x2;
double x1[100];
double x2[100];
double* y;
double t;
int i;

for ( i = 0; i < 100; i++ ) {
x1[ i ] = 2.0 * rand_double();
x2[ i ] = 2.0 * rand_double();
}

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
x1 = ( 20.0 * rand_double() ) - 10.0;
x2 = ( 20.0 * rand_double() ) - 10.0;
y = stdlib_base_minmax( x1, x2 );
if ( y[0] != y[0] && y[1] != y[1] ) {
y = stdlib_base_minmax( x1[ i % 100 ], x2[ i % 100 ] );
if ( y != y ) {
printf( "should not return NaN\n" );
break;
}
}
elapsed = tic() - t;
if ( y[0] != y[0] && y[1] != y[1] ) {
if ( y != y ) {
printf( "should not return NaN\n" );
}
return elapsed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@
# limitations under the License.
#/


# VARIABLES #

ifndef VERBOSE
QUIET := @
else
QUIET :=
endif

# Specify the path to Cephes:
CEPHES ?=

# Specify a list of Cephes source files:
CEPHES_SRC ?=

# Determine the OS:
# Determine the OS ([1][1], [2][2]).
#
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
# [2]: http://stackoverflow.com/a/27776822/2225624
Expand All @@ -42,6 +37,10 @@ ifneq (, $(findstring MSYS,$(OS)))
else
ifneq (, $(findstring CYGWIN,$(OS)))
OS := WINNT
else
ifneq (, $(findstring Windows_NT,$(OS)))
OS := WINNT
endif
endif
endif
endif
Expand All @@ -60,7 +59,7 @@ CFLAGS ?= \
-Wall \
-pedantic

# Determine whether to generate [position independent code][1]:
# Determine whether to generate position independent code ([1][1], [2][2]).
#
# [1]: https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#Code-Gen-Options
# [2]: http://stackoverflow.com/questions/5311515/gcc-fpic-option
Expand All @@ -70,43 +69,77 @@ else
fPIC ?= -fPIC
endif

# List of includes (e.g., `-I /foo/bar -I /beep/boop/include`):
INCLUDE ?=

# List of source files:
SOURCE_FILES ?=

# List of libraries (e.g., `-lopenblas -lpthread`):
LIBRARIES ?=

# List of library paths (e.g., `-L /foo/bar -L /beep/boop`):
LIBPATH ?=

# List of C targets:
c_targets := example.out


# TARGETS #
# RULES #

# Default target.
#/
# Compiles source files.
#
# This target is the default target.

# @param {string} [C_COMPILER] - C compiler (e.g., `gcc`)
# @param {string} [CFLAGS] - C compiler options
# @param {(string|void)} [fPIC] - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
# @param {string} [INCLUDE] - list of includes (e.g., `-I /foo/bar -I /beep/boop/include`)
# @param {string} [SOURCE_FILES] - list of source files
# @param {string} [LIBPATH] - list of library paths (e.g., `-L /foo/bar -L /beep/boop`)
# @param {string} [LIBRARIES] - list of libraries (e.g., `-lopenblas -lpthread`)
#
# @example
# make
#
# @example
# make all
#/
all: $(c_targets)

.PHONY: all


# Compile C source.
#/
# Compiles C source files.
#
# This target compiles C source files.

# @private
# @param {string} CC - C compiler (e.g., `gcc`)
# @param {string} CFLAGS - C compiler options
# @param {(string|void)} fPIC - compiler flag determining whether to generate position independent code (e.g., `-fPIC`)
# @param {string} INCLUDE - list of includes (e.g., `-I /foo/bar`)
# @param {string} SOURCE_FILES - list of source files
# @param {string} LIBPATH - list of library paths (e.g., `-L /foo/bar`)
# @param {string} LIBRARIES - list of libraries (e.g., `-lopenblas`)
#/
$(c_targets): %.out: %.c
$(QUIET) $(CC) $(CFLAGS) $(fPIC) -o $@ $(CEPHES_SRC) $< -lm

$(QUIET) $(CC) $(CFLAGS) $(fPIC) $(INCLUDE) -o $@ $(SOURCE_FILES) $< $(LIBPATH) -lm $(LIBRARIES)

# Run a benchmark.
#/
# Runs compiled examples.
#
# This target runs a benchmark.

# @example
# make run
#/
run: $(c_targets)
$(QUIET) ./$<

.PHONY: run


# Perform clean-up.
#/
# Removes generated files.
#
# This target removes generated files.

# @example
# make clean
#/
clean:
$(QUIET) -rm -f *.o *.out

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@

int main(void) {

const double x[] = { 0.523, 0.785, 1.047, 3.14, 0.56 };

size_t n = sizeof(x) / sizeof(x[0]);
const double x1[] = { 1.0, 0.45, -0.89, 0.0 / 0.0, -0.78, -0.22, 0.66, 0.11, -0.55, 0.0 };
const double x2[] = { -0.22, 0.66, 0.0, -0.55, 0.33, 1.0, 0.0 / 0.0, 0.11, 0.45, -0.78 };

for (size_t i = 0; i < n - 1; i++) {

double* result = stdlib_base_minmax(x[i], x[i + 1]);

printf("Pair %zu: min = %lf, max = %lf\n", i, result[0], result[1]);
}

return 0;
double* v;
int i;
for ( i = 0; i < 10; i++ ) {
v = stdlib_base_minmax( x1[ i ], x2[ i ] );
printf( "x1[ %d ]: %lf, x2[ %d ]: %lf, minmax( x1[ %d ], x2[ %d ] ): ( %lf, %lf )\n", i, x1[ i ], i, x2[ i ], i, i, v[0], v[1] );
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extern "C" {
*
* @param x First value
* @param y Second value
* @return A double* containing the minimum and maximum values
* @return A double containing the minimum and maximum values
*/
double* stdlib_base_minmax(const double x, const double y);

Expand Down
Loading

0 comments on commit 6ca0a3f

Please sign in to comment.