From 38dc48bc1360f77c6a3a6e0d4140614f5ddee95c Mon Sep 17 00:00:00 2001 From: Hugh Perkins Date: Sat, 27 May 2017 17:59:56 +0100 Subject: [PATCH] fix issue with flaot constants having scientific representation --- src/readIR.cpp | 4 +++- test/test_maths.ll | 12 ++++++++++++ test/test_maths.py | 17 +++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/test_maths.ll diff --git a/src/readIR.cpp b/src/readIR.cpp index e044423b..35daedd7 100644 --- a/src/readIR.cpp +++ b/src/readIR.cpp @@ -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) { diff --git a/test/test_maths.ll b/test/test_maths.ll new file mode 100644 index 00000000..51440212 --- /dev/null +++ b/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 +} diff --git a/test/test_maths.py b/test/test_maths.py index e3f582fe..fcf498fe 100644 --- a/test/test_maths.py +++ b/test/test_maths.py @@ -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 = """