Skip to content

Commit

Permalink
fix issue with flaot constants having scientific representation
Browse files Browse the repository at this point in the history
  • Loading branch information
hughperkins committed May 27, 2017
1 parent 2faaa5e commit 38dc48b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/readIR.cpp
Expand Up @@ -100,7 +100,9 @@ string ReadIR::dumpFloatConstant(bool forceSingle, ConstantFP *constantFP) {
} else if(valuestr == "-inf") {
return "-INFINITY";
}
if(valuestr.find('.') == string::npos) {

// check for scientific notation, and lack of decimal point. if neither, add ".0"
if(valuestr.find('.') == string::npos && valuestr.find("e") == string::npos) {
valuestr += ".0";
}
if(!isDouble || forceSingle) {
Expand Down
12 changes: 12 additions & 0 deletions test/test_maths.ll
@@ -0,0 +1,12 @@
define void @kernel_float_constants(float* nocapture %data) #1 {
store float 0x3E7AD7F2A0000000, float* %data
; %1 = getelementptr inbounds float, float* %data, i64 1
; store float 0x43DFFC0000000000, float* %1
; %2 = getelementptr inbounds float, float* %data, i64 2
; store float 0xFFF0000000000000, float* %2
; %3 = getelementptr inbounds float, float* %data, i64 3
; store float 0x7FF0000000000000, float* %3
; %4 = getelementptr inbounds float, float* %data, i64 4
; store float 0xFFEFFFFFFFFFFFFF, float* %4
ret void
}
17 changes: 17 additions & 0 deletions test/test_maths.py
Expand Up @@ -71,6 +71,23 @@ def test_floatconstants(context, q, float_data, float_data_gpu):
assert float_data[7] == - np.inf


def test_float_constants_from_ll(context, q, float_data, float_data_gpu):
with open('test/test_maths.ll', 'r') as f:
ll_code = f.read()
cl_code = test_common.ll_to_cl(ll_code, 'kernel_float_constants', 1)
print('cl_code', cl_code)
# try compiling it, just to be sure...
kernel = test_common.build_kernel(context, cl_code, 'kernel_float_constants')
kernel(q, (32,), (32,), float_data_gpu, offset_type(0), cl.LocalMemory(32))
from_gpu = np.copy(float_data)
cl.enqueue_copy(q, from_gpu, float_data_gpu)
q.finish()
print('from_gpu[0]', from_gpu[0])
print(type(from_gpu[0]), type(1e-7))
assert abs(float(from_gpu[0]) - 1e-7) <= 1e-10
assert 'data[0] = 1e-07f' in cl_code


@pytest.mark.skip
def test_double_ieeefloats(context, q, float_data, float_data_gpu):
cu_code = """
Expand Down

0 comments on commit 38dc48b

Please sign in to comment.