~ubuntu-branches/ubuntu/trusty/ocaml-sha/trusty-proposed

« back to all changes in this revision

Viewing changes to op.h

  • Committer: Bazaar Package Importer
  • Author(s): Eric Cooper
  • Date: 2006-09-03 09:13:11 UTC
  • Revision ID: james.westby@ubuntu.com-20060903091311-xwexszy9lb22ffqa
Tags: upstream-1.3
ImportĀ upstreamĀ versionĀ 1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      Copyright (C) 2006 Vincent Hanquez <tab@snarc.org>
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published
 
6
 * by the Free Software Foundation; version 2 only.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * SHA implementation low level operation
 
14
 */
 
15
 
 
16
#ifndef SHA_OP_H
 
17
#define SHA_OP_H
 
18
 
 
19
typedef unsigned long long u64;
 
20
 
 
21
 
 
22
static inline unsigned int rol32(unsigned int word, unsigned int shift)
 
23
{
 
24
        return (word << shift) | (word >> (32 - shift));
 
25
}
 
26
 
 
27
static inline unsigned int ror32(unsigned int word, unsigned int shift)
 
28
{
 
29
        return (word >> shift) | (word << (32 - shift));
 
30
}
 
31
 
 
32
static inline u64 rol64(u64 word, unsigned int shift)
 
33
{
 
34
        return (word << shift) | (word >> (64 - shift));
 
35
}
 
36
 
 
37
static inline u64 ror64(u64 word, unsigned int shift)
 
38
{
 
39
        return (word >> shift) | (word << (64 - shift));
 
40
}
 
41
 
 
42
#if (defined(__i386__) || defined(__x86_64__)) && !defined(NO_INLINE_ASM)
 
43
static inline unsigned int swap32(unsigned int a)
 
44
{
 
45
        asm ("bswap %0" : "=r" (a) : "0" (a));
 
46
        return a;
 
47
}
 
48
#else
 
49
static inline unsigned int swap32(unsigned int a)
 
50
{
 
51
        return (a << 24) | ((a & 0xff00) << 8) | ((a >> 8) & 0xff00) | (a >> 24);
 
52
}
 
53
#endif
 
54
 
 
55
#if defined(__x86_64__) && !defined(NO_INLINE_ASM)
 
56
static inline u64 swap64(u64 a)
 
57
{
 
58
        asm ("bswap %0" : "=r" (a) : "0" (a));
 
59
        return a;
 
60
}
 
61
#else
 
62
static inline u64 swap64(u64 a)
 
63
{
 
64
        return ((u64) swap32((unsigned int) (a >> 32))) |
 
65
               (((u64) swap32((unsigned int) a)) << 32);
 
66
}
 
67
#endif
 
68
 
 
69
/* big endian to cpu */
 
70
#include <endian.h>
 
71
#if LITTLE_ENDIAN == BYTE_ORDER
 
72
#define be32_to_cpu(a) swap32(a)
 
73
#define cpu_to_be32(a) swap32(a)
 
74
#define be64_to_cpu(a) swap64(a)
 
75
#define cpu_to_be64(a) swap64(a)
 
76
#elif BIG_ENDIAN == BYTE_ORDER
 
77
#define be32_to_cpu(a) (a)
 
78
#define cpu_to_be32(a) (a)
 
79
#define be64_to_cpu(a) (a)
 
80
#define cpu_to_be64(a) (a)
 
81
#else
 
82
#error "endian not supported"
 
83
#endif
 
84
 
 
85
#endif /* !SHA_OP_H */