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 namespace tint { 18 namespace writer { 19 namespace spirv { 20 21 // static Float(float val)22Operand Operand::Float(float val) { 23 Operand o(Kind::kFloat); 24 o.set_float(val); 25 return o; 26 } 27 28 // static Int(uint32_t val)29Operand Operand::Int(uint32_t val) { 30 Operand o(Kind::kInt); 31 o.set_int(val); 32 return o; 33 } 34 35 // static String(const std::string& val)36Operand Operand::String(const std::string& val) { 37 Operand o(Kind::kString); 38 o.set_string(val); 39 return o; 40 } 41 Operand(Kind kind)42Operand::Operand(Kind kind) : kind_(kind) {} 43 44 Operand::~Operand() = default; 45 length() const46uint32_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 55 // 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); 58 break; 59 } 60 return val; 61 } 62 63 } // namespace spirv 64 } // namespace writer 65 } // namespace tint 66