| 1 |
|
| /* |
| 2 |
|
| * ANSI C implementation of vector operations. |
| 3 |
|
| * |
| 4 |
|
| * Copyright (c) 2007, Naoaki Okazaki |
| 5 |
|
| * All rights reserved. |
| 6 |
|
| * |
| 7 |
|
| * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 |
|
| * of this software and associated documentation files (the "Software"), to deal |
| 9 |
|
| * in the Software without restriction, including without limitation the rights |
| 10 |
|
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 |
|
| * copies of the Software, and to permit persons to whom the Software is |
| 12 |
|
| * furnished to do so, subject to the following conditions: |
| 13 |
|
| * |
| 14 |
|
| * The above copyright notice and this permission notice shall be included in |
| 15 |
|
| * all copies or substantial portions of the Software. |
| 16 |
|
| * |
| 17 |
|
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
|
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
|
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
|
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
|
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
|
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 |
|
| * THE SOFTWARE. |
| 24 |
|
| */ |
| 25 |
|
| |
| 26 |
|
| /* $Id: arithmetic_ansi.h 14 2007-10-25 02:04:09Z naoaki $ */ |
| 27 |
|
| |
| 28 |
|
| #include <stdlib.h> |
| 29 |
|
| #include <memory.h> |
| 30 |
|
| |
| 31 |
|
| #if LBFGS_FLOAT == 32 && LBFGS_IEEE_FLOAT |
| 32 |
|
| #define fsigndiff(x, y) (((*(uint32_t*)(x)) ^ (*(uint32_t*)(y))) & 0x80000000U) |
| 33 |
|
| #else |
| 34 |
|
| #define fsigndiff(x, y) (*(x) * (*(y) / fabs(*(y))) < 0.) |
| 35 |
|
| #endif/*LBFGS_IEEE_FLOAT*/ |
| 36 |
|
| |
| 37 |
72 |
| inline static void* vecalloc(size_t size) |
| 38 |
|
| { |
| 39 |
72 |
| void *memblock = malloc(size); |
| 40 |
72 |
▶ | if (memblock) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (memblock) { |
72 |
0 |
72 |
|
| 41 |
72 |
| memset(memblock, 0, size); |
| 42 |
|
| } |
| 43 |
72 |
| return memblock; |
| 44 |
|
| } |
| 45 |
|
| |
| 46 |
72 |
| inline static void vecfree(void *memblock) |
| 47 |
|
| { |
| 48 |
72 |
| free(memblock); |
| 49 |
72 |
| } |
| 50 |
|
| |
| 51 |
|
| inline static void vecset(lbfgsfloatval_t *x, const lbfgsfloatval_t c, const int n) |
| 52 |
|
| { |
| 53 |
|
| int i; |
| 54 |
|
| |
| 55 |
|
| for (i = 0;i < n;++i) { |
| 56 |
|
| x[i] = c; |
| 57 |
|
| } |
| 58 |
|
| } |
| 59 |
|
| |
| 60 |
50 |
| inline static void veccpy(lbfgsfloatval_t *y, const lbfgsfloatval_t *x, const int n) |
| 61 |
|
| { |
| 62 |
|
| int i; |
| 63 |
|
| |
| 64 |
1000122 |
▶ | for (i = 0;i < n;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < n;++i) { |
1000072 |
50 |
1000122 |
|
| 65 |
1000072 |
| y[i] = x[i]; |
| 66 |
|
| } |
| 67 |
50 |
| } |
| 68 |
|
| |
| 69 |
12 |
| inline static void vecncpy(lbfgsfloatval_t *y, const lbfgsfloatval_t *x, const int n) |
| 70 |
|
| { |
| 71 |
|
| int i; |
| 72 |
|
| |
| 73 |
200030 |
▶ | for (i = 0;i < n;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < n;++i) { |
200018 |
12 |
200030 |
|
| 74 |
200018 |
| y[i] = -x[i]; |
| 75 |
|
| } |
| 76 |
12 |
| } |
| 77 |
|
| |
| 78 |
42 |
| inline static void vecadd(lbfgsfloatval_t *y, const lbfgsfloatval_t *x, const lbfgsfloatval_t c, const int n) |
| 79 |
|
| { |
| 80 |
|
| int i; |
| 81 |
|
| |
| 82 |
600110 |
▶ | for (i = 0;i < n;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < n;++i) { |
600068 |
42 |
600110 |
|
| 83 |
600068 |
| y[i] += c * x[i]; |
| 84 |
|
| } |
| 85 |
42 |
| } |
| 86 |
|
| |
| 87 |
16 |
| inline static void vecdiff(lbfgsfloatval_t *z, const lbfgsfloatval_t *x, const lbfgsfloatval_t *y, const int n) |
| 88 |
|
| { |
| 89 |
|
| int i; |
| 90 |
|
| |
| 91 |
200042 |
▶ | for (i = 0;i < n;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < n;++i) { |
200026 |
16 |
200042 |
|
| 92 |
200026 |
| z[i] = x[i] - y[i]; |
| 93 |
|
| } |
| 94 |
16 |
| } |
| 95 |
|
| |
| 96 |
8 |
| inline static void vecscale(lbfgsfloatval_t *y, const lbfgsfloatval_t c, const int n) |
| 97 |
|
| { |
| 98 |
|
| int i; |
| 99 |
|
| |
| 100 |
100021 |
▶ | for (i = 0;i < n;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < n;++i) { |
100013 |
8 |
100021 |
|
| 101 |
100013 |
| y[i] *= c; |
| 102 |
|
| } |
| 103 |
8 |
| } |
| 104 |
|
| |
| 105 |
|
| inline static void vecmul(lbfgsfloatval_t *y, const lbfgsfloatval_t *x, const int n) |
| 106 |
|
| { |
| 107 |
|
| int i; |
| 108 |
|
| |
| 109 |
|
| for (i = 0;i < n;++i) { |
| 110 |
|
| y[i] *= x[i]; |
| 111 |
|
| } |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
98 |
| inline static void vecdot(lbfgsfloatval_t* s, const lbfgsfloatval_t *x, const lbfgsfloatval_t *y, const int n) |
| 115 |
|
| { |
| 116 |
|
| int i; |
| 117 |
98 |
| *s = 0.; |
| 118 |
1500251 |
▶ | for (i = 0;i < n;++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0;i < n;++i) { |
1500153 |
98 |
1500251 |
|
| 119 |
1500153 |
| *s += x[i] * y[i]; |
| 120 |
|
| } |
| 121 |
98 |
| } |
| 122 |
|
| |
| 123 |
28 |
| inline static void vecnorm(lbfgsfloatval_t* s, const lbfgsfloatval_t *x, const int n) |
| 124 |
|
| { |
| 125 |
28 |
| vecdot(s, x, x, n); |
| 126 |
28 |
| *s = (lbfgsfloatval_t)sqrt(*s); |
| 127 |
28 |
| } |
| 128 |
|
| |
| 129 |
4 |
| inline static void vecrnorm(lbfgsfloatval_t* s, const lbfgsfloatval_t *x, const int n) |
| 130 |
|
| { |
| 131 |
4 |
| vecnorm(s, x, n); |
| 132 |
4 |
| *s = (lbfgsfloatval_t)(1.0 / *s); |
| 133 |
4 |
| } |