Skip to content

Instantly share code, notes, and snippets.

@danking
Last active August 29, 2015 14:21
Show Gist options
  • Save danking/1b1a5be5d28e7a464ae0 to your computer and use it in GitHub Desktop.
Save danking/1b1a5be5d28e7a464ae0 to your computer and use it in GitHub Desktop.
The challenge problem for Real Semantics' final presentation challenge in cs260r Spring 2015 (http://read-new.seas.harvard.edu/cs260r/2015/w/Main_Page)

Welcome to your first day on the job of MAX_SPEED Rockets!

Bad news! The MAX_SPEED PlusPlus Rocket is on a collision course with the space station. The flight controller is reporting eroneous results for certain trajectories. Your colleagues have isolated the fault to these pair of functions, f1 and f2. Unfortunately, your colleagues "were never good at math" so they don't really know what this function does or why it sometimes goes wrong.

Since you're a freshly minted BS, they decided to take a lunch break and let you sort it out. At least they left you a test suite that you can run with:

make test

And they mentioned a tool called Real Semantics that you could use. It's located here:

https://github.com/huangyihe/llvm-3.6.0/releases/tag/final-presentation-v1.1

You'll need to modify the REAL_LLI variable in Makefile to point to the executable you downloaded. You should also have clang installed

You can set the HIDE_REFERENCE macro to 0 to prevent the test harness from printing (and thus Real Semantics from reporting errors on) the reference implementation

Also, the rocket, unless this code is fixed, will destroy the space station in 10 minutes. Oh, and due to Bay's Rule it will then spontaneously destroy Earth and the Universe.

Have fun!

/* You can include any desired C or C++ libraries */
#include <math.h>
#include "challenge.h"
/**
* Welcome to your first day on the job of MAX_SPEED Rockets!
*
* Bad news! The MAX_SPEED PlusPlus Rocket is on a collision course with the
* space station. The flight controller is reporting eroneous results for
* certain trajectories. Your colleagues have isolated the fault to these pair
* of functions, `f1` and `f2`. Unfortunately, your colleagues "were never good
* at math" so they don't really know what this function does or why it
* sometimes goes wrong.
*
* Since you're a freshly minted BS, they decided to take a lunch break and let
* you sort it out. At least they left you a test suite that you can run with:
*
* make test
*
* And they mentioned a tool called Real Semantics that you could use. It's
* located here:
*
* https://github.com/huangyihe/llvm-3.6.0/releases/tag/final-presentation-1.0
*
* You'll need to modify the `REAL_LLI` variable in `Makefile` to point to the
* executable you downloaded. You should also have `clang` installed
*
* You can set the `HIDE_REFERENCE` macro to 0 to prevent the test harness from
* printing (and thus Real Semantics from reporting errors on) the reference
* implementation
*
* Also, the rocket, unless this code is fixed, will destroy the space station
* in 10 minutes. Oh, and due to Bay's Rule it will then spontaneously destroy
* Earth and the Universe.
*
* Have fun!
*/
double reference(double a, double b, double c) {
double numerator = (-b-sqrt(b*b-4.0000000000000*a*c)); /* maximum zeros for maximum precision */
double denominator = 2.0 * a;
double result = numerator / denominator;
return result;
}
/* Put your solution in the following function */
double solution(double a, double b, double c) {
double numerator = (-b-sqrt(b*b-4.0000000000000*a*c)); /* maximum zeros for maximum precision */
double denominator = 2.0 * a;
double result = numerator / denominator;
return result;
}
double reference(double a, double b, double c);
double solution(double a, double b, double c);
BITCODEFILES=test.bc challenge.bc
READABLEFILES=test.ll challenge.ll
LLVM_LINK=./llvm-link
REAL_LLI=../llvm-3.6.0/build/bin/lli
default: $(BITCODEFILES)
$(LLVM_LINK) $(BITCODEFILES) -o a.bc
readable: $(READABLEFILES)
$(LLVM_LINK) -S $(BITCODEFILES) -o a.ll
clean:
rm -rf $(BITCODEFILES) $(READABLEFILES) a.bc a.ll
%.bc: %.cpp
clang++ -emit-llvm $*.cpp -o $@
%.bc: %.c
clang -emit-llvm -c $*.c -o $@
%.ll: %.cpp
clang++ -S -emit-llvm -c $*.cpp -o $@
%.ll: %.c
clang -S -emit-llvm -c $*.c -o $@
test: $(BITCODEFILES) default
$(REAL_LLI) -force-interpreter a.bc
#include <stdio.h>
#include "challenge.h"
#define TEST_CASE_COUNT 15
#define HIDE_REFERENCE 1
double test_cases [TEST_CASE_COUNT * 3] =
{ 100 , -8356218543e201 , -321432, // 0
100 , -8356218543e23 , -321432, // 1
-21343243e5 , -3421893e57 , 729e22, // 2
-10 , -50 , 10, // 3
-1e10 , -1e10 , 1e10, // 4
-1e-10 , -1e-10 , 1e-10, // 5
3.214323e-201 , 5e-100 , 1.32423, // 6
1e-10 , 1e-4 , 1e-10, // 7
1 , 3 , 2.25, // 8
1 , 10 , 1, // 9
1e10 , 1e11 , 1e10, // 10
-342e57 , 1e10 , 1e-10, // 11
98045376e102 , 432789658e201 , 42e-42, // 12
4328973e5 , 432789658e201 , 2134e2, // 13
4328973e202 , 432789658e201 , 2134e2 // 14
};
int main(int argc, char ** argv) {
int i;
for (i = 0 ; i < TEST_CASE_COUNT * 3 ; i += 3) {
double ref = reference(test_cases[i + 0], test_cases[i + 1], test_cases[i + 2]);
double soln = solution(test_cases[i + 0], test_cases[i + 1], test_cases[i + 2]);
if (HIDE_REFERENCE) {
printf("For test case %2d, the reference implementation was: %.16e\n", i / 3, ref);
printf(" your solution produced: %.16e\n", soln);
} else {
printf("For test case %2d, your solution produced: %.16e\n", i / 3, soln);
}
}
printf("Ran %d test cases.\n", i / 3);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment