diff --git a/lab07/Makefile b/lab07/Makefile new file mode 100644 index 0000000..e476c89 --- /dev/null +++ b/lab07/Makefile @@ -0,0 +1,45 @@ +# Compiler and flags +CC = gcc +CFLAGS = -Wall -std=c11 + +#Input file +INPUT = input1.txt +OUTPUT = output.txt +REF = ref1.txt +# Source file +SRCS = lab7.c +# Output +TARGET = lab7.out + +# Default target +all: $(TARGET) + +# Rule to build the executable +$(TARGET): $(SRCS) + $(CC) $(CFLAGS) $(SRCS) -o $(TARGET) + +convert_input: $(INPUT) + cat $(INPUT) | dos2unix | tee $(INPUT) + @tail -c1 $(INPUT) | grep -q . && echo >> $(INPUT) + tr -d '\r' < $(INPUT) > temp_file && mv temp_file $(INPUT) + +convert_output: $(OUTPUT) + cat $(OUTPUT) | dos2unix | tee $(OUTPUT) + $(tail -c1 ($INPUT)) != "" && echo >> $(INPUT) + tr -d '\r' < $(OUTPUT) > temp_file && mv temp_file $(OUTPUT) + +check: $(OUTPUT) $(REF) + @diff -q $(OUTPUT) $(REF) > /dev/null; \ + if [ $$? -eq 0 ]; then \ + echo "Pass"; \ + else \ + echo "Fail"; \ + fi + +# Rule to run the program +run: $(TARGET) + ./$(TARGET) $(INPUT) $(OUTPUT) + +# Rule to clean generated files +clean: + rm -f $(TARGET) diff --git a/lab07/input.txt b/lab07/input.txt new file mode 100644 index 0000000..5f11930 --- /dev/null +++ b/lab07/input.txt @@ -0,0 +1,2 @@ +8 +E \ No newline at end of file diff --git a/lab07/input1.txt b/lab07/input1.txt new file mode 100644 index 0000000..fea9856 --- /dev/null +++ b/lab07/input1.txt @@ -0,0 +1,4 @@ +8 +126 +-5 +E \ No newline at end of file diff --git a/lab07/lab7.c b/lab07/lab7.c new file mode 100644 index 0000000..b09414f --- /dev/null +++ b/lab07/lab7.c @@ -0,0 +1,90 @@ +#include +#include + +#define SIGN_MASK 0x0000000000000001 + +long getClamp(long q, long min, long max) +{ + long diffToMin; + long diffToMax; + long diffToMinSign; + long diffToMaxSign; + + diffToMin = q + (~min + 1); + diffToMax = q + (~max + 1); + diffToMinSign = (diffToMin >> 63) & SIGN_MASK; + diffToMaxSign = (diffToMax >> 63) & SIGN_MASK; + + // If diff to min is 0, or the diff to min + // is greater than the min (positive sign), + // then check max + if (diffToMin == 0 || diffToMinSign == 0) + { + // if the diff to min is 0, or the diff to max + // is less than the min, (negative sign), + // return q + // otherwise return max + return (diffToMax == 0 || diffToMaxSign == 1) ? q : max; + } + + return min; +} + +int main(int argc, char **argv) +{ + if (argc != 3) + { + perror("Usage: lab5.c \n"); + return 1; + } + + char *inputPath; + char *outputPath; + FILE *inputFile; + FILE *outputFile; + + inputPath = argv[1]; + outputPath = argv[2]; + inputFile = fopen(inputPath, "r"); + outputFile = fopen(outputPath, "w"); + + if (inputFile == NULL || outputFile == NULL) + { + perror("Failed to open file"); + return 1; + } + + long bits; + long min; + long max; + long x; + long y; + long q; + long result; + + fscanf(inputFile, "%ld", &bits); + fscanf(inputFile, "%ld", &x); + fscanf(inputFile, "%ld", &y); + + min = (1 << (bits - 1)) * -1; + max = (1 << (bits - 1)) - 1; + + if (x == 0 && y == 0) + { + fprintf(outputFile, "min: %ld\t0x%016lx\n", min, min); + fprintf(outputFile, "max: %ld\t0x%016lx\n", max, max); + } + else + { + q = x * y; + + result = getClamp(q, min, max); + + fprintf(outputFile, "%ld\n", result); + } + + fclose(inputFile); + fclose(outputFile); + + return 0; +} diff --git a/lab07/output.txt b/lab07/output.txt new file mode 100644 index 0000000..98280ef --- /dev/null +++ b/lab07/output.txt @@ -0,0 +1 @@ +-128 diff --git a/lab07/ref.txt b/lab07/ref.txt new file mode 100644 index 0000000..13a65b7 --- /dev/null +++ b/lab07/ref.txt @@ -0,0 +1,2 @@ +min: -128 0xffffffffffffff80 +max: 127 0x000000000000007f diff --git a/lab07/ref1.txt b/lab07/ref1.txt new file mode 100644 index 0000000..98280ef --- /dev/null +++ b/lab07/ref1.txt @@ -0,0 +1 @@ +-128