blob: 36f4c237757a60528419e1d0cddb5a91398f29a8 [file] [log] [blame]
dan sinclaire4392c92020-03-17 21:00:22 +00001// Copyright 2020 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "src/writer/spirv/operand.h"
16
dan sinclaire4392c92020-03-17 21:00:22 +000017namespace tint {
18namespace writer {
19namespace spirv {
20
21// static
22Operand Operand::Float(float val) {
23 Operand o(Kind::kFloat);
24 o.set_float(val);
25 return o;
26}
27
28// static
29Operand Operand::Int(uint32_t val) {
30 Operand o(Kind::kInt);
31 o.set_int(val);
32 return o;
33}
34
35// static
36Operand Operand::String(const std::string& val) {
37 Operand o(Kind::kString);
38 o.set_string(val);
39 return o;
40}
41
42Operand::Operand(Kind kind) : kind_(kind) {}
43
44Operand::~Operand() = default;
45
46uint32_t Operand::length() const {
47 uint32_t val = 0;
48 switch (kind_) {
49 case Kind::kFloat:
50 case Kind::kInt:
51 val = 1;
52 break;
53 case Kind::kString:
54 // SPIR-V always nul-terminates strings. The length is rounded up to a
Ryan Harrisonfd1526b2020-04-22 17:46:36 +000055 // multiple of 4 bytes with 0 bytes padding the end. Accounting for the
56 // nul terminator is why '+ 4u' is used here instead of '+ 3u'.
57 val = static_cast<uint32_t>((str_val_.length() + 4u) >> 2);
dan sinclaire4392c92020-03-17 21:00:22 +000058 break;
59 }
60 return val;
61}
62
63} // namespace spirv
64} // namespace writer
65} // namespace tint