complete lab07
This commit is contained in:
7 files changed
+145
No files matched your search
@@ -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)
|
||||
@@ -0,0 +1,2 @@
|
||||
8
|
||||
E
|
||||
@@ -0,0 +1,4 @@
|
||||
8
|
||||
126
|
||||
-5
|
||||
E
|
||||
@@ -0,0 +1,90 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#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 <input_file> <output_file>\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;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
-128
|
||||
@@ -0,0 +1,2 @@
|
||||
min: -128 0xffffffffffffff80
|
||||
max: 127 0x000000000000007f
|
||||
@@ -0,0 +1 @@
|
||||
-128
|
||||
Reference in new issue
Block a user