nir/algebraic: Simplify fsat of fsign
[mesa.git] / src / compiler / blob.c
index 65c450ee97923dc14492d4436fef01e23033386d..c89092e1cf3a98b0c2f9b9222cd23fd73e13d8df 100644 (file)
@@ -158,10 +158,10 @@ blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write)
    return true;
 }
 
-ssize_t
+intptr_t
 blob_reserve_bytes(struct blob *blob, size_t to_write)
 {
-   ssize_t ret;
+   intptr_t ret;
 
    if (! grow_to_fit (blob, to_write))
       return -1;
@@ -172,6 +172,20 @@ blob_reserve_bytes(struct blob *blob, size_t to_write)
    return ret;
 }
 
+intptr_t
+blob_reserve_uint32(struct blob *blob)
+{
+   align_blob(blob, sizeof(uint32_t));
+   return blob_reserve_bytes(blob, sizeof(uint32_t));
+}
+
+intptr_t
+blob_reserve_intptr(struct blob *blob)
+{
+   align_blob(blob, sizeof(intptr_t));
+   return blob_reserve_bytes(blob, sizeof(intptr_t));
+}
+
 bool
 blob_write_uint32(struct blob *blob, uint32_t value)
 {
@@ -180,11 +194,15 @@ blob_write_uint32(struct blob *blob, uint32_t value)
    return blob_write_bytes(blob, &value, sizeof(value));
 }
 
+#define ASSERT_ALIGNED(_offset, _align) \
+   assert(ALIGN((_offset), (_align)) == (_offset))
+
 bool
 blob_overwrite_uint32 (struct blob *blob,
                        size_t offset,
                        uint32_t value)
 {
+   ASSERT_ALIGNED(offset, sizeof(value));
    return blob_overwrite_bytes(blob, offset, &value, sizeof(value));
 }
 
@@ -204,6 +222,15 @@ blob_write_intptr(struct blob *blob, intptr_t value)
    return blob_write_bytes(blob, &value, sizeof(value));
 }
 
+bool
+blob_overwrite_intptr (struct blob *blob,
+                       size_t offset,
+                       intptr_t value)
+{
+   ASSERT_ALIGNED(offset, sizeof(value));
+   return blob_overwrite_bytes(blob, offset, &value, sizeof(value));
+}
+
 bool
 blob_write_string(struct blob *blob, const char *str)
 {
@@ -211,10 +238,10 @@ blob_write_string(struct blob *blob, const char *str)
 }
 
 void
-blob_reader_init(struct blob_reader *blob, uint8_t *data, size_t size)
+blob_reader_init(struct blob_reader *blob, const void *data, size_t size)
 {
    blob->data = data;
-   blob->end = data + size;
+   blob->end = blob->data + size;
    blob->current = data;
    blob->overrun = false;
 }
@@ -229,7 +256,7 @@ ensure_can_read(struct blob_reader *blob, size_t size)
    if (blob->overrun)
       return false;
 
-   if (blob->current < blob->end && blob->end - blob->current >= size)
+   if (blob->current <= blob->end && blob->end - blob->current >= size)
       return true;
 
    blob->overrun = true;
@@ -237,10 +264,10 @@ ensure_can_read(struct blob_reader *blob, size_t size)
    return false;
 }
 
-void *
+const void *
 blob_read_bytes(struct blob_reader *blob, size_t size)
 {
-   void *ret;
+   const void *ret;
 
    if (! ensure_can_read (blob, size))
       return NULL;
@@ -253,9 +280,9 @@ blob_read_bytes(struct blob_reader *blob, size_t size)
 }
 
 void
-blob_copy_bytes(struct blob_reader *blob, uint8_t *dest, size_t size)
+blob_copy_bytes(struct blob_reader *blob, void *dest, size_t size)
 {
-   uint8_t *bytes;
+   const void *bytes;
 
    bytes = blob_read_bytes(blob, size);
    if (bytes == NULL)
@@ -264,6 +291,13 @@ blob_copy_bytes(struct blob_reader *blob, uint8_t *dest, size_t size)
    memcpy(dest, bytes, size);
 }
 
+void
+blob_skip_bytes(struct blob_reader *blob, size_t size)
+{
+   if (ensure_can_read (blob, size))
+      blob->current += size;
+}
+
 /* These next three read functions have identical form. If we add any beyond
  * these first three we should probably switch to generating these with a
  * preprocessor macro.