Skip to content
Snippets Groups Projects
Commit 68de4ff0 authored by Armando Luiz Nicolini Delgado's avatar Armando Luiz Nicolini Delgado :nerd:
Browse files

/* Criação do typedef rtime_t para uso em timestamp(). */

/* Pequenas alterações em comentários */
parent aca5c9c5
No related branches found
No related tags found
No related merge requests found
# CI1164 - Utils
Funções e bibliotecas de uso genérico para a disciplina CI1164 (DINF/UFPR)
#include <stdio.h>
#include <stdlib.h>
#include "matrix.h"
/* Cria com calloc() matriz 'n x m' */
double **criaMatriz (int n, int m)
{
double **C;
int i, j;
C = (double **) calloc(n,sizeof(double *));
C[0] = (double *) calloc(n*m,sizeof(double));
for (i=1; i < n; ++i)
C[i] = C[i-1] + m;
return C;
}
/* Retorna ponteiro para matriz inversa de matriz 'A'.
Matriz inversa é alocada com 'criaMatriz()'
*/
// Adaptação de https://www.programming-techniques.com/2011/09/numerical-methods-inverse-of-nxn-matrix.html
double **invMatriz (double **A, int n)
{
double ratio,a, **aux, **IA;
int i, j, k;
aux = criaMatriz(n, 2*n);
IA = criaMatriz(n,n);
for(i = 0; i < n; i++) {
for(j = 0; j < 2*n; j++) {
if (j < n)
aux[i][j] = A[i][j];
else if(i==(j-n))
aux[i][j] = 1.0;
else
aux[i][j] = 0.0;
}
}
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
if(i!=j){
ratio = aux[j][i]/aux[i][i];
for(k = 0; k < 2*n; k++){
aux[j][k] -= ratio * aux[i][k];
}
}
}
}
for(i = 0; i < n; i++){
a = aux[i][i];
for(j = n; j < 2*n; j++){
IA[i][j-n] = aux[i][j] / a;
}
}
freeMatriz(aux, n);
return IA;
}
/* Libera matriz com 'n' linhas, alocada com 'criaMatriz()' */
void freeMatriz (double **A, int n)
{
int i;
free(A[0]);
free(A);
}
/* Multiplica matrizes 'A' e 'B', ambas 'n x n' */
double **multMatriz (double **A, double **B, int n)
{
double **C;
int i, j,k;
C = (double **) malloc(n*sizeof(double *));
for (i=0; i < n; ++i) {
C[i] = (double *) calloc(n,sizeof(double));
for (j=0; j < n; ++j)
for (k=0; k < n; ++k)
C[i][j] += A[i][k] * B[k][j];
}
return C;
}
/* Mostra na tela matriz 'n x n' alocada com 'criaMatriz()' */
void prnMatriz (double **A, int n)
{
int i, j;
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
printf("%15.5lg", A[i][j]);
}
printf("\n");
}
}
/* Preenche matriz 'n x n' alocada com 'criaMatriz()' */
void lerMatriz (double **A, int n)
{
int i, j;
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
scanf("%lg", A[i]+j);
}
}
}
/* Cria com calloc() matriz 'n x m' */
double **criaMatriz (int n, int m);
/* Retorna ponteiro para matriz inversa de matriz 'A'.
Matriz inversa é alocada com 'criaMatriz()'
*/
double **invMatriz (double **A, int n);
/* Libera matriz com 'n' linhas, alocada com 'criaMatriz()' */
void freeMatriz (double **A, int n);
/* Multiplica matrizes 'A' e 'B', ambas 'n x n' */
double **multMatriz (double **A, double **B, int n);
/* Mostra na tela matriz 'n x n' alocada com 'criaMatriz()' */
void prnMatriz (double **A, int n);
/* Preenche matriz 'n x n' alocada com 'criaMatriz()' */
void lerMatriz (double **A, int n);
#include "randomNR.h"
// Gerador de números pseudo-aleatórios
// Período do gerador: 3.138e57 (aprox.)
// Fonte: Press, W.H.; "Numerical Recipes, 3rd ed.", pp.342)
static struct Ran {
Ullong u,v,w;
} randomNR = {0, 4101842887655102017LL, 1};
// Define a semente
void nrSeed(Ullong j)
{
if (j != 4101842887655102017LL) {
randomNR.u = j ^ randomNR.v; nrRandom64();
randomNR.v = randomNR.u; nrRandom64();
randomNR.w = randomNR.v; nrRandom64();
}
}
// Retorna int-64 bits entre 0 e 2⁶⁴ - 1
Ullong nrRandom64()
{
Ullong x;
randomNR.u = randomNR.u * 2862933555777941757LL + 7046029254386353087LL;
randomNR.v ^= randomNR.v >> 17;
randomNR.v ^= randomNR.v << 31;
randomNR.v ^= randomNR.v >> 8;
randomNR.w = 4294957665U*(randomNR.w & 0xffffffff) + (randomNR.w >> 32);
x = randomNR.u ^ (randomNR.u << 21);
x ^= x >> 35;
x ^= x << 4;
return (x + randomNR.v) ^ randomNR.w;
}
// Retorna int-64 bits entre 0 e 1.0
Doub nrDrandom() { return 5.42101086242752217E-20 * nrRandom64(); }
// Retorna int-32 bits entre 0 e 2³² - 1
Uint nrRandom32() { return (Uint) nrRandom64(); }
/*
struct Ranq1 {
Ullong v;
Ranq1(Ullong j) : v(4101842887655102017LL) {
v ^= j;
v = int64();
}
inline Ullong int64() {
v ^= v >> 21; v ^= v << 35; v ^= v >> 4;
return v * 2685821657736338717LL;
}
inline Doub doub() { return 5.42101086242752217E-20 * int64(); }
inline Uint int32() { return (Uint)int64(); }
};
struct Ranq2 {
Ullong v,w;
Ranq2(Ullong j) : v(4101842887655102017LL), w(1) {
v ^= j;
w = int64();
v = int64();
}
inline Ullong int64() {
v ^= v >> 17; v ^= v << 31; v ^= v >> 8;
w = 4294957665U*(w & 0xffffffff) + (w >> 32);
return v ^ w;
}
inline Doub doub() { return 5.42101086242752217E-20 * int64(); }
inline Uint int32() { return (Uint)int64(); }
};
struct Ranhash {
inline Ullong int64(Ullong u) {
Ullong v = u * 3935559000370003845LL + 2691343689449507681LL;
v ^= v >> 21; v ^= v << 37; v ^= v >> 4;
v *= 4768777513237032717LL;
v ^= v << 20; v ^= v >> 41; v ^= v << 5;
return v;
}
inline Uint int32(Ullong u)
{ return (Uint)(int64(u) & 0xffffffff) ; }
inline Doub doub(Ullong u)
{ return 5.42101086242752217E-20 * int64(u); }
};
struct Ranbyte {
Int s[256],i,j,ss;
Uint v;
Ranbyte(Int u) {
v = 2244614371U ^ u;
for (i=0; i<256; i++) {s[i] = i;}
for (j=0, i=0; i<256; i++) {
ss = s[i];
j = (j + ss + (v >> 24)) & 0xff;
s[i] = s[j]; s[j] = ss;
v = (v << 24) | (v >> 8);
}
i = j = 0;
for (Int k=0; k<256; k++) int8();
}
inline unsigned char int8() {
i = (i+1) & 0xff;
ss = s[i];
j = (j+ss) & 0xff;
s[i] = s[j]; s[j] = ss;
return (unsigned char)(s[(s[i]+s[j]) & 0xff]);
}
Uint int32() {
v = 0;
for (int k=0; k<4; k++) {
i = (i+1) & 0xff;
ss = s[i];
j = (j+ss) & 0xff;
s[i] = s[j]; s[j] = ss;
v = (v << 8) | s[(s[i]+s[j]) & 0xff];
}
return v;
}
Doub doub() {
return 2.32830643653869629E-10 * ( int32() +
2.32830643653869629E-10 * int32() );
}
};
struct Ranfib {
Doub dtab[55], dd;
Int inext, inextp;
Ranfib(Ullong j) : inext(0), inextp(31) {
Ranq1 init(j);
for (int k=0; k<55; k++) dtab[k] = init.doub();
}
Doub doub() {
if (++inext == 55) inext = 0;
if (++inextp == 55) inextp = 0;
dd = dtab[inext] - dtab[inextp];
if (dd < 0) dd += 1.0;
return (dtab[inext] = dd);
}
inline unsigned long int32()
{ return (unsigned long)(doub() * 4294967295.0);}
};
struct Ranlim32 {
Uint u,v,w1,w2;
Ranlim32(Uint j) : v(2244614371U), w1(521288629U), w2(362436069U) {
u = j ^ v; int32();
v = u; int32();
}
inline Uint int32() {
u = u * 2891336453U + 1640531513U;
v ^= v >> 13; v ^= v << 17; v ^= v >> 5;
w1 = 33378 * (w1 & 0xffff) + (w1 >> 16);
w2 = 57225 * (w2 & 0xffff) + (w2 >> 16);
Uint x = u ^ (u << 9); x ^= x >> 17; x ^= x << 6;
Uint y = w1 ^ (w1 << 17); y ^= y >> 15; y ^= y << 5;
return (x + v) ^ (y + w2);
}
inline Doub doub() { return 2.32830643653869629E-10 * int32(); }
inline Doub truedoub() {
return 2.32830643653869629E-10 * ( int32() +
2.32830643653869629E-10 * int32() );
}
};
*/
#ifndef _RANDOMNR_H_
#define _RANDOMNR_H_
// all the system #include's we'll ever need
#include <math.h>
#include <complex.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#define MAX(a, b) (b > a ? b : a)
#define MIN(a, b) (b < a ? b : a)
#define SIGN(a, b) (b >= 0 ? (a >= 0 ? a : -a) : (a >= 0 ? -a : a))
// basic type names (redefine if your bit lengths don't match)
typedef int Int; // 32 bit integer
typedef unsigned int Uint;
typedef long long int Llong; // 64 bit integer
typedef unsigned long long int Ullong;
typedef char Char; // 8 bit integer
typedef unsigned char Uchar;
typedef double Doub; // default floating type
typedef long double Ldoub;
// typedef complex<double> Complex; // default complex type
typedef unsigned char Bool;
// NaN: uncomment one of the following 3 methods of defining a global NaN
// you can test by verifying that (NaN != NaN) is true
//Uint proto_nan[2]={0xffffffff, 0x7fffffff};
//double NaN = *( double* )proto_nan;
//Doub NaN = sqrt(-1.);
// Functions
// Período do gerador: 3.138e57 (aprox.)
// Fonte: Press, W.H.; "Numerical Recipes, 3rd ed.", pp.342)
// Define a semente
void nrSeed(Ullong j);
// Retorna int-64 bits entre 0 e 2⁶⁴ - 1
Ullong nrRandom64();
// Retorna int-64 bits entre 0 e 1.0
Doub nrDrandom();
// Retorna int-32 bits entre 0 e 2³² - 1
Uint nrRandom32();
#endif /* _RANDOMNR_H_ */
......@@ -4,7 +4,7 @@
#include "utils.h"
/* Retorna tempo em milisegundos
/* Retorna tempo em milisegundos desde EPOCH
Forma de uso:
......@@ -14,11 +14,11 @@
tempo = timestamp() - tempo;
*/
double timestamp(void)
rtime_t timestamp (void)
{
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC_RAW, &tp);
return((double)(tp.tv_sec*1.0e3 + tp.tv_nsec*1.0e-6));
return ( (rtime_t) tp.tv_sec*1.0e3 + (rtime_t) tp.tv_nsec*1.0e-6 );
}
/* Gera string '<baseName>_n'
......@@ -49,3 +49,24 @@ int isPot2(int n)
return (k = log2(n)) == log2(n) ;
}
*/
/* Retorna tempo em milisegundos desde EPOCH
Forma de uso:
double tempo;
tempo = timestamp();
<trecho de programa do qual se deseja medir tempo>
tempo = timestamp() - tempo;
*/
/*
rtime_t timestamp(void)
{
struct timeval tp;
gettimeofday(&tp, NULL);
return ( (rtime_t) tp.tv_sec*1.0e3 + (rtime_t) tp.tv_usec*1.0e-3 );
}
*/
......@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
// Valor absoluto de um número. Alternativa ao uso da função 'fabs()'
#define ABS(num) ((num) < 0.0 ? -(num) : (num))
......@@ -16,10 +17,11 @@ typedef char * string_t;
// rtime_t: tipo usado para representar valores de tempo em ponto flutuante
typedef double rtime_t;
// SIMD alignment macros
// SIMD alignment macros (bytes)
#define ALIGN_64 __attribute__((aligned(64)))
#define ALIGN_32 __attribute__((aligned(32)))
#define ALIGN_16 __attribute__((aligned(16)))
#define ALIGN_8 __attribute__((aligned(8)))
// Número máximo de dígitos em um número
#define numDigits(n) 6 // ( (int) log10(n) + 1 )
......@@ -28,7 +30,7 @@ typedef double rtime_t;
#define isPot2(n) (n && !(n & (n - 1)))
// Funções
double timestamp(void);
rtime_t timestamp(void);
string_t markerName(string_t baseName, int n);
#endif // __UTILS_H__
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment