1#!/usr/bin/python3 2 3# Copyright 2020 Google LLC 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# https://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17# Generator for emboss_arithmetic_maximum_operation_generated.h. 18 19# Maximum number of explicit arguments in the recursive overloads. This script 20# will generate overloads for 5...OVERLOADS arguments, plus a special overload 21# that handles >OVERLOADS arguments using a variadic template. 22# 23# This should probably be a power of 2. 24OVERLOADS = 64 25 26# Copyright header in the generated code complies with Google policies. 27print("""// Copyright 2020 Google LLC 28// 29// Licensed under the Apache License, Version 2.0 (the "License"); 30// you may not use this file except in compliance with the License. 31// You may obtain a copy of the License at 32// 33// https://www.apache.org/licenses/LICENSE-2.0 34// 35// Unless required by applicable law or agreed to in writing, software 36// distributed under the License is distributed on an "AS IS" BASIS, 37// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 38// See the License for the specific language governing permissions and 39// limitations under the License. 40 41// GENERATED CODE. DO NOT EDIT. REGENERATE WITH 42// runtime/cpp/generators/maximum_operation_do.py""") 43 44for i in range(5, OVERLOADS + 1): 45 print(""" 46 template <typename T> 47 static inline constexpr T Do({0}) {{ 48 return Do(Do({1}), Do({2})); 49 }}""".strip().format( 50 ", ".join(["T v{}".format(n) for n in range(i)]), 51 ", ".join(["v{}".format(n) for n in range(i // 2)]), 52 ", ".join(["v{}".format(n) for n in range(i // 2, i)]))) 53 54# The "more than OVERLOADS arguments" overload uses a variadic template to 55# handle the remaining arguments, even though all arguments should have the 56# same type; this is necessary because C++11 variadic functions are either 57# variadic templates (one template argument per argument) or C-style variadic 58# functions (which operate under very different rules). 59# 60# This also uses one explicit argument, rest0, to ensure that it does not get 61# confused with the last non-variadic overload. 62print(""" 63 template <typename T, typename... RestT> 64 static inline constexpr T Do({0}, T rest0, RestT... rest) {{ 65 return Do(Do({1}), Do(rest0, rest...)); 66 }}""".format( 67 ", ".join(["T v{}".format(n) for n in range(OVERLOADS)]), 68 ", ".join(["v{}".format(n) for n in range(OVERLOADS)]), 69 OVERLOADS)) 70