17ec681f3Smrg/*
27ec681f3Smrg * Copyright © Microsoft Corporation
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
207ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
217ec681f3Smrg * IN THE SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#include "dxil_buffer.h"
257ec681f3Smrg#include <assert.h>
267ec681f3Smrg
277ec681f3Smrgvoid
287ec681f3Smrgdxil_buffer_init(struct dxil_buffer *b, unsigned abbrev_width)
297ec681f3Smrg{
307ec681f3Smrg   blob_init(&b->blob);
317ec681f3Smrg   b->buf = 0;
327ec681f3Smrg   b->buf_bits = 0;
337ec681f3Smrg
347ec681f3Smrg   b->abbrev_width = abbrev_width;
357ec681f3Smrg}
367ec681f3Smrg
377ec681f3Smrgvoid
387ec681f3Smrgdxil_buffer_finish(struct dxil_buffer *b)
397ec681f3Smrg{
407ec681f3Smrg   blob_finish(&b->blob);
417ec681f3Smrg}
427ec681f3Smrg
437ec681f3Smrgstatic bool
447ec681f3Smrgflush_dword(struct dxil_buffer *b)
457ec681f3Smrg{
467ec681f3Smrg   assert(b->buf_bits >= 32 && b->buf_bits < 64);
477ec681f3Smrg
487ec681f3Smrg   uint32_t lower_bits = b->buf & UINT32_MAX;
497ec681f3Smrg   if (!blob_write_bytes(&b->blob, &lower_bits, sizeof(lower_bits)))
507ec681f3Smrg      return false;
517ec681f3Smrg
527ec681f3Smrg   b->buf >>= 32;
537ec681f3Smrg   b->buf_bits -= 32;
547ec681f3Smrg
557ec681f3Smrg   return true;
567ec681f3Smrg}
577ec681f3Smrg
587ec681f3Smrgbool
597ec681f3Smrgdxil_buffer_emit_bits(struct dxil_buffer *b, uint32_t data, unsigned width)
607ec681f3Smrg{
617ec681f3Smrg   assert(b->buf_bits < 32);
627ec681f3Smrg   assert(width > 0 && width <= 32);
637ec681f3Smrg   assert((data & ~((UINT64_C(1) << width) - 1)) == 0);
647ec681f3Smrg
657ec681f3Smrg   b->buf |= ((uint64_t)data) << b->buf_bits;
667ec681f3Smrg   b->buf_bits += width;
677ec681f3Smrg
687ec681f3Smrg   if (b->buf_bits >= 32)
697ec681f3Smrg      return flush_dword(b);
707ec681f3Smrg
717ec681f3Smrg   return true;
727ec681f3Smrg}
737ec681f3Smrg
747ec681f3Smrgbool
757ec681f3Smrgdxil_buffer_emit_vbr_bits(struct dxil_buffer *b, uint64_t data,
767ec681f3Smrg                          unsigned width)
777ec681f3Smrg{
787ec681f3Smrg   assert(width > 1 && width <= 32);
797ec681f3Smrg
807ec681f3Smrg   uint32_t tag = UINT32_C(1) << (width - 1);
817ec681f3Smrg   uint32_t max = tag - 1;
827ec681f3Smrg   while (data > max) {
837ec681f3Smrg      uint32_t value = (data & max) | tag;
847ec681f3Smrg      data >>= width - 1;
857ec681f3Smrg
867ec681f3Smrg      if (!dxil_buffer_emit_bits(b, value, width))
877ec681f3Smrg         return false;
887ec681f3Smrg   }
897ec681f3Smrg
907ec681f3Smrg   return dxil_buffer_emit_bits(b, data, width);
917ec681f3Smrg}
927ec681f3Smrg
937ec681f3Smrgbool
947ec681f3Smrgdxil_buffer_align(struct dxil_buffer *b)
957ec681f3Smrg{
967ec681f3Smrg   assert(b->buf_bits < 32);
977ec681f3Smrg
987ec681f3Smrg   if (b->buf_bits) {
997ec681f3Smrg      b->buf_bits = 32;
1007ec681f3Smrg      return flush_dword(b);
1017ec681f3Smrg   }
1027ec681f3Smrg
1037ec681f3Smrg   return true;
1047ec681f3Smrg}
105