arithmetic_ansi.h

00001 /*
00002  *      ANSI C implementation of vector operations.
00003  *
00004  * Copyright (c) 2007, Naoaki Okazaki
00005  * All rights reserved.
00006  *
00007  * Permission is hereby granted, free of charge, to any person obtaining a copy
00008  * of this software and associated documentation files (the "Software"), to deal
00009  * in the Software without restriction, including without limitation the rights
00010  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011  * copies of the Software, and to permit persons to whom the Software is
00012  * furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included in
00015  * all copies or substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00022  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00023  * THE SOFTWARE.
00024  */
00025 
00026 /* $Id: arithmetic_ansi.h 14 2007-10-25 02:04:09Z naoaki $ */
00027 
00028 #include <stdlib.h>
00029 #include <memory.h>
00030 
00031 #if      LBFGS_FLOAT == 32 && LBFGS_IEEE_FLOAT
00032 #define  fsigndiff(x, y)   (((*(uint32_t*)(x)) ^ (*(uint32_t*)(y))) & 0x80000000U)
00033 #else
00034 #define  fsigndiff(x, y) (*(x) * (*(y) / fabs(*(y))) < 0.)
00035 #endif/*LBFGS_IEEE_FLOAT*/
00036 
00037 inline static void* vecalloc(size_t size)
00038 {
00039    void *memblock = malloc(size);
00040    if (memblock) {
00041       memset(memblock, 0, size);
00042    }
00043    return memblock;
00044 }
00045 
00046 inline static void vecfree(void *memblock)
00047 {
00048    free(memblock);
00049 }
00050 
00051 inline static void vecset(lbfgsfloatval_t *x, const lbfgsfloatval_t c, const int n)
00052 {
00053    int i;
00054    
00055    for (i = 0;i < n;++i) {
00056       x[i] = c;
00057    }
00058 }
00059 
00060 inline static void veccpy(lbfgsfloatval_t *y, const lbfgsfloatval_t *x, const int n)
00061 {
00062    int i;
00063 
00064    for (i = 0;i < n;++i) {
00065       y[i] = x[i];
00066    }
00067 }
00068 
00069 inline static void vecncpy(lbfgsfloatval_t *y, const lbfgsfloatval_t *x, const int n)
00070 {
00071    int i;
00072 
00073    for (i = 0;i < n;++i) {
00074       y[i] = -x[i];
00075    }
00076 }
00077 
00078 inline static void vecadd(lbfgsfloatval_t *y, const lbfgsfloatval_t *x, const lbfgsfloatval_t c, const int n)
00079 {
00080    int i;
00081 
00082    for (i = 0;i < n;++i) {
00083       y[i] += c * x[i];
00084    }
00085 }
00086 
00087 inline static void vecdiff(lbfgsfloatval_t *z, const lbfgsfloatval_t *x, const lbfgsfloatval_t *y, const int n)
00088 {
00089    int i;
00090 
00091    for (i = 0;i < n;++i) {
00092       z[i] = x[i] - y[i];
00093    }
00094 }
00095 
00096 inline static void vecscale(lbfgsfloatval_t *y, const lbfgsfloatval_t c, const int n)
00097 {
00098    int i;
00099 
00100    for (i = 0;i < n;++i) {
00101       y[i] *= c;
00102    }
00103 }
00104 
00105 inline static void vecmul(lbfgsfloatval_t *y, const lbfgsfloatval_t *x, const int n)
00106 {
00107    int i;
00108 
00109    for (i = 0;i < n;++i) {
00110       y[i] *= x[i];
00111    }
00112 }
00113 
00114 inline static void vecdot(lbfgsfloatval_t* s, const lbfgsfloatval_t *x, const lbfgsfloatval_t *y, const int n)
00115 {
00116    int i;
00117    *s = 0.;
00118    for (i = 0;i < n;++i) {
00119       *s += x[i] * y[i];
00120    }
00121 }
00122 
00123 inline static void vecnorm(lbfgsfloatval_t* s, const lbfgsfloatval_t *x, const int n)
00124 {
00125    vecdot(s, x, x, n);
00126    *s = (lbfgsfloatval_t)sqrt(*s);
00127 }
00128 
00129 inline static void vecrnorm(lbfgsfloatval_t* s, const lbfgsfloatval_t *x, const int n)
00130 {
00131    vecnorm(s, x, n);
00132    *s = (lbfgsfloatval_t)(1.0 / *s);
00133 }

Generated on Tue Feb 10 10:01:29 2009 for imaging2 by  doxygen 1.5.5