Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020 Project CHIP Authors
4 : * Copyright (c) 2013-2017 Nest Labs, Inc.
5 : *
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * You may obtain a copy of the License at
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : */
18 :
19 : /**
20 : * @file
21 : * This file implements utility functions for deriving a fibonacci
22 : * number from an index.
23 : *
24 : */
25 :
26 : #include <stdint.h>
27 : #include <stdlib.h>
28 :
29 : #include "FibonacciUtils.h"
30 :
31 : namespace chip {
32 :
33 21 : uint32_t GetFibonacciForIndex(uint32_t inIndex)
34 : {
35 21 : uint32_t retval = 0;
36 21 : uint32_t tmp = 0;
37 : uint32_t vals[2];
38 21 : uint32_t index = 0;
39 :
40 21 : vals[0] = 0;
41 21 : vals[1] = 1;
42 :
43 21 : if (inIndex < 2)
44 : {
45 12 : retval = vals[inIndex];
46 : }
47 : else
48 : {
49 54 : for (index = 2; index <= inIndex; index++)
50 : {
51 45 : tmp = vals[0] + vals[1];
52 :
53 45 : vals[0] = vals[1];
54 45 : vals[1] = tmp;
55 : }
56 :
57 9 : retval = tmp;
58 : }
59 :
60 21 : return retval;
61 : }
62 :
63 : } // namespace chip
|