{#
 # The MIT License (MIT)
 #
 # Copyright (c) 2020 Philippe Proulx <pproulx@efficios.com>
 #
 # Permission is hereby granted, free of charge, to any person obtaining
 # a copy of this software and associated documentation files (the
 # "Software"), to deal in the Software without restriction, including
 # without limitation the rights to use, copy, modify, merge, publish,
 # distribute, sublicense, and/or sell copies of the Software, and to
 # permit persons to whom the Software is furnished to do so, subject to
 # the following conditions:
 #
 # The above copyright notice and this permission notice shall be
 # included in all copies or substantial portions of the Software.
 #
 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #}
{% import 'common.j2' as common %}
{% set incr_at %}ctx->at += {{ op.ft.size }}{% endset %}
{% if op.ft.alignment % 8 == 0 and
	op.ft.size in [8, 16, 32, 64] and
	cfg.trace.type.__class__ != barectf_config.TraceTypeWithUnknownNativeByteOrder %}
	{% set c_type %}uint{{ op.ft.size }}_t{% endset %}
{
	const {{ c_type }} tmp_val = ({{ c_type }}) {{ src }};

	memcpy(&ctx->buf[_BITS_TO_BYTES(ctx->at)], &tmp_val, sizeof(tmp_val));
	{{ incr_at }};
}
{%- else %}
	{% set bo = 'le' if cfg.trace.type.trace_byte_order == barectf_config.ByteOrder.LITTLE_ENDIAN else 'be' %}
	{% set c_type_non_const = c_type | replace('const ', '') %}
	{% set offset_in_byte = 'ctx->at % 8' if op.offset_in_byte == none else op.offset_in_byte %}
bt_bitfield_write_{{ bo }}(&ctx->buf[_BITS_TO_BYTES(ctx->at)], {{ offset_in_byte }}, {{ op.ft.size }},
	{{ c_type_non_const }}, ({{ c_type_non_const }}) {{ src }});
{{ incr_at }};
{%- endif %}
