[Beignet] [PATCH] Add long support for printf

junyan.he at inbox.com junyan.he at inbox.com
Wed Sep 17 08:04:04 PDT 2014


From: Junyan He <junyan.he at linux.intel.com>

Signed-off-by: Junyan He <junyan.he at linux.intel.com>
---
 backend/src/ir/printf.cpp               |   25 ++++++++++++++++++++-----
 backend/src/llvm/llvm_printf_parser.cpp |   22 +++++++++++++++-------
 kernels/test_printf.cl                  |    3 +++
 3 files changed, 38 insertions(+), 12 deletions(-)

diff --git a/backend/src/ir/printf.cpp b/backend/src/ir/printf.cpp
index 9d60402..fc7e2fe 100644
--- a/backend/src/ir/printf.cpp
+++ b/backend/src/ir/printf.cpp
@@ -149,20 +149,35 @@ namespace gbe
                     switch (slot.state->conversion_specifier) {
                       case PRINTF_CONVERSION_D:
                       case PRINTF_CONVERSION_I:
-                        PRINT_SOMETHING(int, d);
+                        if (slot.state->length_modifier == PRINTF_LM_L)
+                          PRINT_SOMETHING(long, d);
+                        else
+                          PRINT_SOMETHING(int, d);
                         break;
 
                       case PRINTF_CONVERSION_O:
-                        PRINT_SOMETHING(int, o);
+                        if (slot.state->length_modifier == PRINTF_LM_L)
+                          PRINT_SOMETHING(long, o);
+                        else
+                          PRINT_SOMETHING(int, o);
                         break;
                       case PRINTF_CONVERSION_U:
-                        PRINT_SOMETHING(int, u);
+                        if (slot.state->length_modifier == PRINTF_LM_L)
+                          PRINT_SOMETHING(long, u);
+                        else
+                          PRINT_SOMETHING(int, u);
                         break;
                       case PRINTF_CONVERSION_X:
-                        PRINT_SOMETHING(int, X);
+                        if (slot.state->length_modifier == PRINTF_LM_L)
+                          PRINT_SOMETHING(long, X);
+                        else
+                          PRINT_SOMETHING(int, X);
                         break;
                       case PRINTF_CONVERSION_x:
-                        PRINT_SOMETHING(int, x);
+                        if (slot.state->length_modifier == PRINTF_LM_L)
+                          PRINT_SOMETHING(long, x);
+                        else
+                          PRINT_SOMETHING(int, x);
                         break;
 
                       case PRINTF_CONVERSION_C:
diff --git a/backend/src/llvm/llvm_printf_parser.cpp b/backend/src/llvm/llvm_printf_parser.cpp
index 00e1ef8..42cc878 100644
--- a/backend/src/llvm/llvm_printf_parser.cpp
+++ b/backend/src/llvm/llvm_printf_parser.cpp
@@ -640,14 +640,22 @@ error:
           case PRINTF_CONVERSION_U:
           case PRINTF_CONVERSION_x:
           case PRINTF_CONVERSION_X:
-            /* If the bits change, we need to consider the signed. */
-            if (arg->getType() != Type::getInt32Ty(module->getContext())) {
-              arg = builder->CreateIntCast(arg, Type::getInt32Ty(module->getContext()), sign);
-            }
+            if (slot.state->length_modifier == PRINTF_LM_L) { /* we would rather print long. */
+              if (arg->getType() != Type::getInt64Ty(module->getContext())) {
+                arg = builder->CreateIntCast(arg, Type::getInt64Ty(module->getContext()), sign);
+              }
+              dst_type = Type::getInt64PtrTy(module->getContext(), 1);
+              sizeof_size = sizeof(ulong);
+            } else {
+              /* If the bits change, we need to consider the signed. */
+              if (arg->getType() != Type::getInt32Ty(module->getContext())) {
+                arg = builder->CreateIntCast(arg, Type::getInt32Ty(module->getContext()), sign);
+              }
 
-            /* Int to Int, just store. */
-            dst_type = Type::getInt32PtrTy(module->getContext(), 1);
-            sizeof_size = sizeof(int);
+              /* Int to Int, just store. */
+              dst_type = Type::getInt32PtrTy(module->getContext(), 1);
+              sizeof_size = sizeof(int);
+            }
             return true;
 
           case PRINTF_CONVERSION_C:
diff --git a/kernels/test_printf.cl b/kernels/test_printf.cl
index 84bb478..c2844f4 100644
--- a/kernels/test_printf.cl
+++ b/kernels/test_printf.cl
@@ -7,6 +7,7 @@ test_printf(void)
   uint a = 'x';
   float f = 5.0f;
   int3 vec;
+  ulong cc = 1004294967296;
   vec.x = x;
   vec.y = y;
   vec.z = z;
@@ -15,6 +16,8 @@ test_printf(void)
     printf("--- Welcome to the printf test of %s ---\n", "Intel Beignet");
 
     printf("### output a char is %c\n", a);
+
+    printf("@@@ A long value is %ld\n", cc);
   }
 
   if (x % 15 == 0)
-- 
1.7.9.5





More information about the Beignet mailing list