Path: blob/21.2-virgl/src/microsoft/compiler/dxil_buffer.c
4564 views
/*1* Copyright © Microsoft Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "dxil_buffer.h"24#include <assert.h>2526void27dxil_buffer_init(struct dxil_buffer *b, unsigned abbrev_width)28{29blob_init(&b->blob);30b->buf = 0;31b->buf_bits = 0;3233b->abbrev_width = abbrev_width;34}3536void37dxil_buffer_finish(struct dxil_buffer *b)38{39blob_finish(&b->blob);40}4142static bool43flush_dword(struct dxil_buffer *b)44{45assert(b->buf_bits >= 32 && b->buf_bits < 64);4647uint32_t lower_bits = b->buf & UINT32_MAX;48if (!blob_write_bytes(&b->blob, &lower_bits, sizeof(lower_bits)))49return false;5051b->buf >>= 32;52b->buf_bits -= 32;5354return true;55}5657bool58dxil_buffer_emit_bits(struct dxil_buffer *b, uint32_t data, unsigned width)59{60assert(b->buf_bits < 32);61assert(width > 0 && width <= 32);62assert((data & ~((UINT64_C(1) << width) - 1)) == 0);6364b->buf |= ((uint64_t)data) << b->buf_bits;65b->buf_bits += width;6667if (b->buf_bits >= 32)68return flush_dword(b);6970return true;71}7273bool74dxil_buffer_emit_vbr_bits(struct dxil_buffer *b, uint64_t data,75unsigned width)76{77assert(width > 1 && width <= 32);7879uint32_t tag = UINT32_C(1) << (width - 1);80uint32_t max = tag - 1;81while (data > max) {82uint32_t value = (data & max) | tag;83data >>= width - 1;8485if (!dxil_buffer_emit_bits(b, value, width))86return false;87}8889return dxil_buffer_emit_bits(b, data, width);90}9192bool93dxil_buffer_align(struct dxil_buffer *b)94{95assert(b->buf_bits < 32);9697if (b->buf_bits) {98b->buf_bits = 32;99return flush_dword(b);100}101102return true;103}104105106