gallium/util: rewrite global constructor system for half floats (GCC/MSVC only!)
[mesa.git] / progs / gallium / unit / u_format_test.c
index 5274311e0351d872b2d38c2c01f3cd76f13a811a..ca4c92f5b7ec42c4093ec0e1fb59834b17d5ef0b 100644 (file)
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <float.h>
 
+#include "util/u_half.h"
 #include "util/u_format.h"
 #include "util/u_format_tests.h"
-#include "util/u_format_pack.h"
+#include "util/u_format_s3tc.h"
 
 
 static boolean
-test_format_unpack_4f(const struct util_format_test_case *test)
+compare_float(float x, float y)
+{
+   float error = y - x;
+
+   if (error < 0.0f)
+      error = -error;
+
+   if (error > FLT_EPSILON) {
+      return FALSE;
+   }
+
+   return TRUE;
+}
+
+
+static void
+print_packed(const struct util_format_description *format_desc,
+             const char *prefix,
+             const uint8_t *packed,
+             const char *suffix)
 {
-   float unpacked[4];
    unsigned i;
+   const char *sep = "";
+
+   printf("%s", prefix);
+   for (i = 0; i < format_desc->block.bits/8; ++i) {
+      printf("%s%02x", sep, packed[i]);
+      sep = " ";
+   }
+   printf("%s", suffix);
+}
+
+
+static void
+print_unpacked_doubl(const struct util_format_description *format_desc,
+                     const char *prefix,
+                     const double unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4],
+                     const char *suffix)
+{
+   unsigned i, j;
+   const char *sep = "";
+
+   printf("%s", prefix);
+   for (i = 0; i < format_desc->block.height; ++i) {
+      for (j = 0; j < format_desc->block.width; ++j) {
+         printf("%s{%f, %f, %f, %f}", sep, unpacked[i][j][0], unpacked[i][j][1], unpacked[i][j][2], unpacked[i][j][3]);
+         sep = ", ";
+      }
+      sep = ",\n";
+   }
+   printf("%s", suffix);
+}
+
+
+static void
+print_unpacked_float(const struct util_format_description *format_desc,
+                     const char *prefix,
+                     const float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4],
+                     const char *suffix)
+{
+   unsigned i, j;
+   const char *sep = "";
+
+   printf("%s", prefix);
+   for (i = 0; i < format_desc->block.height; ++i) {
+      for (j = 0; j < format_desc->block.width; ++j) {
+         printf("%s{%f, %f, %f, %f}", sep, unpacked[i][j][0], unpacked[i][j][1], unpacked[i][j][2], unpacked[i][j][3]);
+         sep = ", ";
+      }
+      sep = ",\n";
+   }
+   printf("%s", suffix);
+}
+
+
+static void
+print_unpacked_8unorm(const struct util_format_description *format_desc,
+                      const char *prefix,
+                      const uint8_t unpacked[][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4],
+                      const char *suffix)
+{
+   unsigned i, j;
+   const char *sep = "";
+
+   printf("%s", prefix);
+   for (i = 0; i < format_desc->block.height; ++i) {
+      for (j = 0; j < format_desc->block.width; ++j) {
+         printf("%s{0x%02x, 0x%02x, 0x%02x, 0x%02x}", sep, unpacked[i][j][0], unpacked[i][j][1], unpacked[i][j][2], unpacked[i][j][3]);
+         sep = ", ";
+      }
+   }
+   printf("%s", suffix);
+}
+
+
+static boolean
+test_format_fetch_float(const struct util_format_description *format_desc,
+                        const struct util_format_test_case *test)
+{
+   float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
+   unsigned i, j, k;
    boolean success;
 
-   util_format_unpack_4f(test->format, unpacked, test->packed);
+   success = TRUE;
+   for (i = 0; i < format_desc->block.height; ++i) {
+      for (j = 0; j < format_desc->block.width; ++j) {
+         format_desc->fetch_float(unpacked[i][j], test->packed, j, i);
+         for (k = 0; k < 4; ++k) {
+            if (!compare_float(test->unpacked[i][j][k], unpacked[i][j][k])) {
+               success = FALSE;
+            }
+         }
+      }
+   }
+
+   if (!success) {
+      print_unpacked_float(format_desc, "FAILED: ", unpacked, " obtained\n");
+      print_unpacked_doubl(format_desc, "        ", test->unpacked, " expected\n");
+   }
+
+   return success;
+}
+
+
+static boolean
+test_format_unpack_float(const struct util_format_description *format_desc,
+                         const struct util_format_test_case *test)
+{
+   float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
+   unsigned i, j, k;
+   boolean success;
+
+   format_desc->unpack_float(&unpacked[0][0][0], sizeof unpacked[0], test->packed, 0, format_desc->block.width, format_desc->block.height);
 
    success = TRUE;
-   for (i = 0; i < 4; ++i)
-      if (test->unpacked[i] != unpacked[i])
-         success = FALSE;
+   for (i = 0; i < format_desc->block.height; ++i) {
+      for (j = 0; j < format_desc->block.width; ++j) {
+         for (k = 0; k < 4; ++k) {
+            if (!compare_float(test->unpacked[i][j][k], unpacked[i][j][k])) {
+               success = FALSE;
+            }
+         }
+      }
+   }
 
    if (!success) {
-      printf("FAILED: (%f %f %f %f) obtained\n", unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
-      printf("        (%f %f %f %f) expected\n", test->unpacked[0], test->unpacked[1], test->unpacked[2], test->unpacked[3]);
+      print_unpacked_float(format_desc, "FAILED: ", unpacked, " obtained\n");
+      print_unpacked_doubl(format_desc, "        ", test->unpacked, " expected\n");
    }
 
    return success;
@@ -58,33 +192,43 @@ test_format_unpack_4f(const struct util_format_test_case *test)
 
 
 static boolean
-test_format_pack_4f(const struct util_format_test_case *test)
+
+test_format_pack_float(const struct util_format_description *format_desc,
+                       const struct util_format_test_case *test)
 {
+   float unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
    uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
-   unsigned i;
+   unsigned i, j, k;
    boolean success;
 
+   if (test->format == PIPE_FORMAT_DXT1_RGBA) {
+      /*
+       * Skip S3TC as packed representation is not canonical.
+       *
+       * TODO: Do a round trip conversion.
+       */
+      return TRUE;
+   }
+
    memset(packed, 0, sizeof packed);
+   for (i = 0; i < format_desc->block.height; ++i) {
+      for (j = 0; j < format_desc->block.width; ++j) {
+         for (k = 0; k < 4; ++k) {
+            unpacked[i][j][k] = (float) test->unpacked[i][j][k];
+         }
+      }
+   }
 
-   util_format_pack_4f(test->format, packed, test->unpacked[0], test->unpacked[1], test->unpacked[2], test->unpacked[3]);
+   format_desc->pack_float(packed, 0, &unpacked[0][0][0], sizeof unpacked[0], format_desc->block.width, format_desc->block.height);
 
    success = TRUE;
-   for (i = 0; i < UTIL_FORMAT_MAX_PACKED_BYTES; ++i)
+   for (i = 0; i < format_desc->block.bits/8; ++i)
       if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
          success = FALSE;
 
    if (!success) {
-      /* TODO: print more than 4 bytes */
-      printf("FAILED: (%02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x) obtained\n",
-             packed[0], packed[1], packed[2], packed[3],
-             packed[4], packed[5], packed[6], packed[7],
-             packed[8], packed[9], packed[10], packed[11],
-             packed[12], packed[13], packed[14], packed[15]);
-      printf("        (%02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x) expected\n",
-             test->packed[0], test->packed[1], test->packed[2], test->packed[3],
-             test->packed[4], test->packed[5], test->packed[6], test->packed[7],
-             test->packed[8], test->packed[9], test->packed[10], test->packed[11],
-             test->packed[12], test->packed[13], test->packed[14], test->packed[15]);
+      print_packed(format_desc, "FAILED: ", packed, " obtained\n");
+      print_packed(format_desc, "        ", test->packed, " expected\n");
    }
 
    return success;
@@ -92,12 +236,12 @@ test_format_pack_4f(const struct util_format_test_case *test)
 
 
 static boolean
-convert_4f_to_4ub(uint8_t *dst, const double *src)
+convert_float_to_8unorm(uint8_t *dst, const double *src)
 {
    unsigned i;
    boolean accurate = TRUE;
 
-   for (i = 0; i < 4; ++i) {
+   for (i = 0; i < UTIL_FORMAT_MAX_UNPACKED_HEIGHT*UTIL_FORMAT_MAX_UNPACKED_WIDTH*4; ++i) {
       if (src[i] < 0.0) {
          accurate = FALSE;
          dst[i] = 0;
@@ -116,25 +260,32 @@ convert_4f_to_4ub(uint8_t *dst, const double *src)
 
 
 static boolean
-test_format_unpack_4ub(const struct util_format_test_case *test)
+test_format_unpack_8unorm(const struct util_format_description *format_desc,
+                          const struct util_format_test_case *test)
 {
-   uint8_t unpacked[4];
-   uint8_t expected[4];
-   unsigned i;
+   uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
+   uint8_t expected[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
+   unsigned i, j, k;
    boolean success;
 
-   util_format_unpack_4ub(test->format, unpacked, test->packed);
+   format_desc->unpack_8unorm(&unpacked[0][0][0], sizeof unpacked[0], test->packed, 0, 1, 1);
 
-   convert_4f_to_4ub(expected, test->unpacked);
+   convert_float_to_8unorm(&expected[0][0][0], &test->unpacked[0][0][0]);
 
    success = TRUE;
-   for (i = 0; i < 4; ++i)
-      if (expected[i] != unpacked[i])
-         success = FALSE;
+   for (i = 0; i < format_desc->block.height; ++i) {
+      for (j = 0; j < format_desc->block.width; ++j) {
+         for (k = 0; k < 4; ++k) {
+            if (expected[i][j][k] != unpacked[i][j][k]) {
+               success = FALSE;
+            }
+         }
+      }
+   }
 
    if (!success) {
-      printf("FAILED: (0x%02x 0x%02x 0x%02x 0x%02x) obtained\n", unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
-      printf("        (0x%02x 0x%02x 0x%02x 0x%02x) expected\n", expected[0], expected[1], expected[2], expected[3]);
+      print_unpacked_8unorm(format_desc, "FAILED: ", unpacked, " obtained\n");
+      print_unpacked_8unorm(format_desc, "        ", expected, " expected\n");
    }
 
    return success;
@@ -142,14 +293,24 @@ test_format_unpack_4ub(const struct util_format_test_case *test)
 
 
 static boolean
-test_format_pack_4ub(const struct util_format_test_case *test)
+test_format_pack_8unorm(const struct util_format_description *format_desc,
+                        const struct util_format_test_case *test)
 {
-   uint8_t unpacked[4];
+   uint8_t unpacked[UTIL_FORMAT_MAX_UNPACKED_HEIGHT][UTIL_FORMAT_MAX_UNPACKED_WIDTH][4];
    uint8_t packed[UTIL_FORMAT_MAX_PACKED_BYTES];
    unsigned i;
    boolean success;
 
-   if (!convert_4f_to_4ub(unpacked, test->unpacked)) {
+   if (test->format == PIPE_FORMAT_DXT1_RGBA) {
+      /*
+       * Skip S3TC as packed representation is not canonical.
+       *
+       * TODO: Do a round trip conversion.
+       */
+      return TRUE;
+   }
+
+   if (!convert_float_to_8unorm(&unpacked[0][0][0], &test->unpacked[0][0][0])) {
       /*
        * Skip test cases which cannot be represented by four unorm bytes.
        */
@@ -158,25 +319,16 @@ test_format_pack_4ub(const struct util_format_test_case *test)
 
    memset(packed, 0, sizeof packed);
 
-   util_format_pack_4ub(test->format, packed, unpacked[0], unpacked[1], unpacked[2], unpacked[3]);
+   format_desc->pack_8unorm(packed, 0, &unpacked[0][0][0], sizeof unpacked[0], 1, 1);
 
    success = TRUE;
-   for (i = 0; i < UTIL_FORMAT_MAX_PACKED_BYTES; ++i)
+   for (i = 0; i < format_desc->block.bits/8; ++i)
       if ((test->packed[i] & test->mask[i]) != (packed[i] & test->mask[i]))
          success = FALSE;
 
    if (!success) {
-      /* TODO: print more than 4 bytes */
-      printf("FAILED: (%02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x) obtained\n",
-             packed[0], packed[1], packed[2], packed[3],
-             packed[4], packed[5], packed[6], packed[7],
-             packed[8], packed[9], packed[10], packed[11],
-             packed[12], packed[13], packed[14], packed[15]);
-      printf("        (%02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x  %02x %02x %02x %02x) expected\n",
-             test->packed[0], test->packed[1], test->packed[2], test->packed[3],
-             test->packed[4], test->packed[5], test->packed[6], test->packed[7],
-             test->packed[8], test->packed[9], test->packed[10], test->packed[11],
-             test->packed[12], test->packed[13], test->packed[14], test->packed[15]);
+      print_packed(format_desc, "FAILED: ", packed, " obtained\n");
+      print_packed(format_desc, "        ", test->packed, " expected\n");
    }
 
    return success;
@@ -184,7 +336,8 @@ test_format_pack_4ub(const struct util_format_test_case *test)
 
 
 typedef boolean
-(*test_func_t)(const struct util_format_test_case *test);
+(*test_func_t)(const struct util_format_description *format_desc,
+               const struct util_format_test_case *test);
 
 
 static boolean
@@ -196,15 +349,27 @@ test_one(test_func_t func, const char *suffix)
 
    for (i = 0; i < util_format_nr_test_cases; ++i) {
       const struct util_format_test_case *test = &util_format_test_cases[i];
+      const struct util_format_description *format_desc;
+      bool skip = FALSE;
+
+      format_desc = util_format_description(test->format);
+
+      if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC &&
+          !util_format_s3tc_enabled) {
+         skip = TRUE;
+      }
+
       if (test->format != last_format) {
-         const struct util_format_description *format_desc;
-         format_desc = util_format_description(test->format);
-         printf("Testing util_format_%s_%s ...\n", format_desc->short_name, suffix);
+         printf("%s util_format_%s_%s ...\n",
+                skip ? "Skipping" : "Testing", format_desc->short_name, suffix);
          last_format = test->format;
       }
 
-      if (!func(&util_format_test_cases[i]))
-        success = FALSE;
+      if (!skip) {
+         if (!func(format_desc, &util_format_test_cases[i])) {
+           success = FALSE;
+         }
+      }
    }
 
    return success;
@@ -216,16 +381,19 @@ test_all(void)
 {
    bool success = TRUE;
 
-   if (!test_one(&test_format_pack_4f, "pack_4f"))
+   if (!test_one(&test_format_fetch_float, "fetch_float"))
+     success = FALSE;
+
+   if (!test_one(&test_format_pack_float, "pack_float"))
      success = FALSE;
 
-   if (!test_one(&test_format_unpack_4f, "unpack_4f"))
+   if (!test_one(&test_format_unpack_float, "unpack_float"))
      success = FALSE;
 
-   if (!test_one(&test_format_pack_4ub, "pack_4ub"))
+   if (!test_one(&test_format_pack_8unorm, "pack_8unorm"))
      success = FALSE;
 
-   if (!test_one(&test_format_unpack_4ub, "unpack_4ub"))
+   if (!test_one(&test_format_unpack_8unorm, "unpack_8unorm"))
      success = FALSE;
 
    return success;
@@ -236,6 +404,8 @@ int main(int argc, char **argv)
 {
    boolean success;
 
+   util_format_s3tc_init();
+
    success = test_all();
 
    return success ? 0 : 1;