1 // 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 
17 #include <cmath>
18 
19 namespace tint {
20 namespace writer {
21 namespace spirv {
22 
23 // static
Float(float val)24 Operand Operand::Float(float val) {
25   Operand o(Kind::kFloat);
26   o.set_float(val);
27   return o;
28 }
29 
30 // static
Int(uint32_t val)31 Operand Operand::Int(uint32_t val) {
32   Operand o(Kind::kInt);
33   o.set_int(val);
34   return o;
35 }
36 
37 // static
String(const std::string & val)38 Operand Operand::String(const std::string& val) {
39   Operand o(Kind::kString);
40   o.set_string(val);
41   return o;
42 }
43 
Operand(Kind kind)44 Operand::Operand(Kind kind) : kind_(kind) {}
45 
46 Operand::~Operand() = default;
47 
length() const48 uint32_t Operand::length() const {
49   uint32_t val = 0;
50   switch (kind_) {
51     case Kind::kFloat:
52     case Kind::kInt:
53       val = 1;
54       break;
55     case Kind::kString:
56       // SPIR-V always nul-terminates strings. The length is rounded up to a
57       // multiple of 4 bytes with 0 bytes padding the end. Accounting for the
58       // nul terminator is why '+ 4u' is used here instead of '+ 3u'.
59       val = static_cast<uint32_t>((str_val_.length() + 4u) >> 2);
60       break;
61   }
62   return val;
63 }
64 
65 }  // namespace spirv
66 }  // namespace writer
67 }  // namespace tint
68