Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
tea.h
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2020 Hubert Kowalski.
4 Copyright (C) 2020 Ralf Brown.
5 Copyright (C) 2022 Martin Bařinka.
6 Copyright (C) 2024 Alynx Zhou.
7 Copyright (C) 2026 Aurélien PIERRE.
8
9 darktable is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 darktable is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with darktable. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#ifndef DT_PIXEL_TEA_H
24#define DT_PIXEL_TEA_H
25
27
28// Allocate a buffer for storing the internal state of 'numthreads' parallel instances of the Tiny Encryption
29// Algorithm. We need to ensure that each state falls in a separate cache line, or all threads sharing a
30// cache line will be running in lock-step as the cache line bounces back and forth between them, effectively
31// cutting throughput by a factor equal to the number of threads sharing a cache line (8 with a 64-byte cache
32// line and 32-bit ints)
33#define TEA_STATE_SIZE (MAX(DT_CACHELINE_BYTES, 2*sizeof(unsigned int)))
34static inline unsigned int* alloc_tea_states(size_t numthreads)
35{
36 unsigned int* states = dt_pixelpipe_cache_alloc_align_cache(numthreads * TEA_STATE_SIZE, 0);
37 if (states) memset(states, 0, numthreads * TEA_STATE_SIZE);
38 return states;
39}
40
41// retrieve the state for the instance in the given thread from the array of states previously allocated with
42// alloc_tea_states()
43static inline unsigned int* get_tea_state(unsigned int* const states, int threadnum)
44{
45 return states + threadnum * TEA_STATE_SIZE/sizeof(states[0]);
46}
47
48static inline void free_tea_states(unsigned int* states)
49{
51}
52
53// How many rounds of the mixing function to run for one encryption
54#define TEA_ROUNDS 8
55
56// Run the encryption mixing function using and updating the given internal state. For use as a PRNG, you can
57// set arg[0] to the random-number seed, then read out the value of arg[0] after each call to this function.
58static inline void encrypt_tea(unsigned int *arg)
59{
60 const unsigned int key[] = { 0xa341316c, 0xc8013ea4, 0xad90777d, 0x7e95761e };
61 unsigned int v0 = arg[0], v1 = arg[1];
62 unsigned int sum = 0;
63 unsigned int delta = 0x9e3779b9;
64 for(int i = 0; i < TEA_ROUNDS; i++)
65 {
66 sum += delta;
67 v0 += ((v1 << 4) + key[0]) ^ (v1 + sum) ^ ((v1 >> 5) + key[1]);
68 v1 += ((v0 << 4) + key[2]) ^ (v0 + sum) ^ ((v0 >> 5) + key[3]);
69 }
70 arg[0] = v0;
71 arg[1] = v1;
72}
73
74static inline float tpdf(unsigned int urandom)
75{
76 float frandom = (float)urandom / (float)0xFFFFFFFFu;
77
78 return (frandom < 0.5f ? (sqrtf(2.0f * frandom) - 1.0f) : (1.0f - sqrtf(2.0f * (1.0f - frandom))));
79}
80
81#endif // DT_PIXEL_TEA_H
82
83// clang-format off
84// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
85// vim: shiftwidth=2 expandtab tabstop=2 cindent
86// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
87// clang-format on
const float delta
char * key
#define dt_pixelpipe_cache_alloc_align_cache(size, id)
#define dt_pixelpipe_cache_free_align(mem)
static float tpdf(unsigned int urandom)
Definition tea.h:74
#define TEA_ROUNDS
Definition tea.h:54
static unsigned int * get_tea_state(unsigned int *const states, int threadnum)
Definition tea.h:43
static void free_tea_states(unsigned int *states)
Definition tea.h:48
#define TEA_STATE_SIZE
Definition tea.h:33
static unsigned int * alloc_tea_states(size_t numthreads)
Definition tea.h:34
static void encrypt_tea(unsigned int *arg)
Definition tea.h:58